PAT (Advanced Level) Practice 1060 Are They Equal (25 points) Ling Chen 1642

Posted by roygbiv on Sat, 25 Dec 2021 14:13:04 +0100

PAT (Advanced Level) Practice 1060 Are They Equal (25 points) Ling Chen 1642

Title Description:

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×105 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.

If a machine can only store 3 significant digits, floating-point numbers 12300 and 12358.9 are considered equal because they are both saved to 0.123 by a simple chopper × 105. Now, given the number of significant digits and two floating-point numbers on a machine, you should judge whether they are treated equally 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 10100, and that its total digit number is less than 100.

Each input file contains A test case and gives three numbers N, A and B, where N (< 100) is the significant digits and A and B are the two floating-point numbers to be compared. Each floating-point number is non negative, no more than 10100, and the total number of digits 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.

For each test case, if the two numbers are considered equal, print YES in one line and then 0 in the standard form d[1]... D [n] * 10 ^ k (d [1] > 0 unless the number is 0); or NO if they are not treated equally, then two numbers in the standard form. All terms must be separated by spaces and there must be NO extra spaces at the end of the line.

Note: a simple chopper without rounding is assumed.

Sample Input1:

3 12300 12358.9

Sample Output1:

YES 0.123*10^5

Sample Input2:

3 120 128

Sample Output2:

NO 0.120*10^3 0.128*10^3

The Idea:

The meaning of the title describes a simple scientific counting method to retain N significant digits. First, according to the thinking of normal people, assuming that all inputs are good inputs in line with our common sense, test point 4 and test point 6 can't pass. Finally, think again and again about whether there is leading 0 data in the test data, such as 00123, 00.001, so after input, remove the leading 0 first. Then test point 4 passed and test point 6 still didn't. At this time, according to the meaning of the question, the test data 0.000 is input. Good guy, NO is output, so test point 6 should be a special judgment on the input of 0.

The Codes:

#include<bits/stdc++.h>
using namespace std ;
typedef pair<string , int> PSI ;
string a , b ;
int n ;
PSI deal(string s) {
	string res = "0." ;
	int exp = 0 ;
	if( s.find(".") == string::npos){ // Integer, no decimal point 
		exp = s.size() ;
		if(exp >= n){   // Number of significant digits > = number of digits that can be saved 
			res += s.substr(0 , n) ;  // Truncation required 
		}else{  // Number of significant digits < number of digits that can be saved 
			res += s ; 	// After splicing all significant numbers 
			for(int j = exp ; j < n ; j ++) res += "0" ;  // Add 0 at the end 
			if(s.size() == 1 && s[0] == '0') exp = 0 ; 	// If it is an integer 0, judge it
		}
	}else{
		if(s[0] == '0'){  // Is 0 Xxxxx form
			int i = 2 ;
			for( ; i < s.size() ; i ++) if(s[i] != '0') break ;
			if(i != s.size()){
				exp = 2 - i ;
				int l = s.size() - i ;
				if(l >= n) res += s.substr(i , n) ;
				else{
					res += s.substr(i , l) ;
					for(int j = l ; j < n ; j ++) res += "0" ;  // Add 0 at the end 
				}
			}else{ // s is 0.000 · 000
				exp = 0 ;
				for(int j = 0 ; j < n ; j ++) res += "0" ;  // All 0 
			}
		}else{
			exp = s.find(".") ;
			if(exp >= n) res += s.substr(0 , n) ;  // Truncation required 
			else if(s.size() - 1 >= n){
				res += s.substr(0 , exp) ; // Splice exp digits before decimal point 
				res += s.substr(exp + 1 , n - exp ) ; // Splice n - exp digits before decimal point 
			}else{
				res += s.substr(0 , exp) ; 	// Splice exp digits before decimal point 
				res += s.substr(exp + 1 , s.size() - exp - 1) ; // Splice size before decimal point () - exp - 1 digit 
				for(int i = s.size() - 1 ; i < n ; i ++) res += "0" ;  // Add 0 at the end 
			}
		}
		
	} 
	return make_pair(res , exp) ;
}
string delPre0(string s) {
	int i = 0 ;
	for(; i < s.size() - 1 ; i ++){
		if(s[i] =='0' && s[i+1] == '0') continue ;
		else break ;
	}
	if(s[i + 1] != '.') return s.substr(i + 1 , s.size() - i - 1) ;
	return s.substr(i  , s.size() - i ) ;
}
int main(){
	cin >> n >> a >> b ;
	if(a[0] == '0')a = delPre0(a) ;
	if(b[0] == '0')b = delPre0(b) ;
	PSI psia = deal(a) ;
	PSI psib = deal(b) ;
	if(psia.first == psib.first && psia.second == psib.second)
		printf("YES %s*10^%d\n" , psia.first.c_str() , psia.second );
	else
		printf("NO %s*10^%d %s*10^%d\n" , psia.first.c_str() , psia.second , psib.first.c_str() , psib.second) ;
	return 0 ;
}

Topics: C++ PAT-A