PAT 1060 Are They Equal(25点)(試験点4と6エラーについて)極端試験サンプル

6593 ワード

If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123×10​5​​ with simple chopping. Now given the number of significant digits on a machine and two float numbers, you are supposed to tell if they are treated equal in that machine.

Input Specification:


Each input file contains one test case which gives three numbers N, A and B, where N (<100) is the number of significant digits, and A and B are the two float numbers to be compared. Each float number is non-negative, no greater than 10​100​​, and that its total digit number is less than 100.

Output Specification:


For each test case, print in a line  YES  if the two numbers are treated equal, and then the number in the standard form  0.d[1]...d[N]*10^k  ( d[1] >0 unless the number is 0); or  NO  if they are not treated equal, and then the two numbers in their standard form. All the terms must be separated by a space, with no extra space at the end of a line.
Note: Simple chopping is assumed without rounding.

Sample Input 1:

3 12300 12358.9

Sample Output 1:

YES 0.123*10^5

Sample Input 2:

3 120 128

Sample Output 2:

NO 0.120*10^3 0.128*10^3

Nを与えて、それから更に2つの非負の数を与えて、2つの正数をN位の小数を含む科学の記数法の数に変換することを要求して、(尾を除いて法を採用します).2つの科学記数が等しいかどうかを比較し、等しい場合はYESを出力し、結果を出力し、等しくない場合はNOを出力し、2つの数を出力する.
考え方:1.2つの数の小数点を探します.整数の場合、小数点は整数の後の1桁です.小数点を記す足はposと表記する.
           2.問題の意味に従って、前から後ろに1位を探して0ではなく、小数点の数ではありません.脚注を記録します.その足のマークはnotZeroです.
3、状況を分けてnotZero=pos notZeroposの時に科学計数法の小数点の移動する桁数countに変換することを求めます;
           4.2つの数を0に変換します.xxxxの形式は、変換後の長さがNより小さい場合は末尾を0で揃える.
           5.変換後の数の上位n+2(小数点を含む1番目の0)ビット文字を比較し、2つのcountが等しく、n+2ビット数も等しい場合yesを出力します.そうでない場合はnoを出力します.
ピットポイント:1.考えているときはあまり欲張らないで、コードをどのように簡素化するかを考えないで、一歩一歩考えて、二歩と一歩歩いてはいけません.(卵を引っ張る).
           2.極端なサンプルを考慮すると、入力された数は、サンプル6 00012と12の比較のような規範的な非負の数ではない可能性がある.サンプル4の0.00と0の比較.
整理されたコード(AC):
#include
#include
#include
using namespace std;
// 
string::size_type findDot(string &number) {
	string::size_type pos = number.find('.');
	if (pos == string::npos)
		pos = number.size();
	return pos;
}
// 0 
void findNotZero(string &number,unsigned int &notZero) {
	unsigned int i = 0;
	for (; i < number.size(); i++) {
		if (number[i] != '0' && number[i] != '.') {
			break;
		}
	}
	notZero = i;
}
// 0.xxxx , 
void changeNumber( int &count, string &number, unsigned int notZero,string::size_type pos) {
	if (notZero < pos) {
		count = pos - notZero;
		number = "0." + number.substr(notZero, pos - notZero/* 00012 12 */) + ((pos < number.size() ? number.substr(pos + 1) : ""));
	}
	else if (notZero == number.size()) {
		count = 0;
		number = "0.0";
	}
	else {
		count = pos - notZero + 1;
		number = "0." + number.substr(notZero);
	}
}
// n 
void appendZero(string &number,unsigned  int n) {
	while (number.size() <= n + 2) {
		number.append("0");
	}
	number = number.substr(0, n + 2);
}
int main() {
	unsigned  int n;
	string number1, number2;
	cin >> n >> number1 >> number2;
    // 
	string::size_type pos1 = findDot(number1);
	string::size_type pos2 = findDot(number2);
    // 0 
	unsigned int notZero1, notZero2;
	findNotZero(number1, notZero1);
	findNotZero(number2, notZero2);
    // 0.xxxx , 
	 int count1, count2;
	changeNumber(count1, number1, notZero1, pos1);
	changeNumber(count2, number2, notZero2, pos2);
    // n 
	appendZero(number1, n);
	appendZero(number2, n);
	// 
	if (number1 == number2 && count1 == count2) {
		cout << "YES" << " " << number1 << "*10^" << count1 << endl;
	}
	else {
		cout << "NO" << " " << number1 << "*10^" << count1 << " " << number2 << "*10^" << count2 << endl;
	}
}

整理前のコード(AC):
#include
#include
#include
using namespace std;
int main() {
	int n;
	string number1, number2;
	cin >> n >> number1 >> number2;
	int len1 = number1.size(), len2 = number2.size();
	string::size_type pos1 = number1.find('.');
	string::size_type pos2 = number2.find('.');
	if (pos1 == string::npos)
		pos1 = number1.size();
	if (pos2 == string::npos)
		pos2 = number2.size();
	int notZero1, notZero2;
	int i = 0;
	for (;i < number1.size();i++) {
		if (number1[i] !='0' && number1[i] != '.') {
			break;
		}
	}
	notZero1 = i;
	for (i = 0;i < number2.size();i++) {
		if (number2[i] !='0' && number2[i] != '.') {
			break;
		}
	}
	notZero2 = i;
	int count1, count2;
	if (notZero1 < pos1) {
		count1 = pos1-notZero1;
		number1 = "0." + number1.substr(notZero1, pos1-notZero1) + ((pos1 < len1 ? number1.substr(pos1 + 1) : ""));
	}
	else if (notZero1 ==len1) {
		count1 = 0;
		number1 = "0.0";
	}
	else {
		count1 = pos1 - notZero1 + 1;
		number1 = "0." + number1.substr(notZero1);
	}
	if (notZero2 < pos2) {
		count2 = pos2 - notZero2;
		number2 = "0." + number2.substr(notZero2, pos2-notZero2) + ((pos2 < len2 ? number2.substr(pos2 + 1) : ""));
	}
	else if (notZero2==len2) {
		count2 = 0;
		number2 = "0.0";
	}
	else {
		count2 = pos2 - notZero2 + 1;
		number2 = "0." + number2.substr(notZero2);
	}
	while (number1.size() <= n + 2) {
		number1.append("0");
	}
	while (number2.size() <= n + 2) {
		number2.append("0");
	}
	number1 = number1.substr(0, n + 2);
	number2 = number2.substr(0, n + 2);
	if (number1 == number2&&count1 == count2) {
		cout << "YES" << " " << number1 << "*10^" << count1 << endl;
	}
	else {
		cout << "NO" << " " << number1 << "*10^" << count1 << " " << number2 << "*10^" << count2 << endl;
	}

}