PAT (Advanced Level) Practice 1132 Cut Integer (20 分) 凌宸1642

PAT (Advanced Level) Practice 1132 Cut Integer (20 分) 凌宸1642

题目描述:

Cutting an integer means to cut a K digits lone integer Z into two integers of (K/2) digits long integers A and B. For example, after cutting Z = 167334, we have A = 167 and B = 334. It is interesting to see that Z can be devided by the product of A and B, as 167334 / (167 × 334) = 3. Given an integer Z, you are supposed to test if it is such an integer.

译:切整数就是把一个K位的单整数Z切为(K/2)位长整数A和B的两个整数,比如切Z = 167334后,我们有A = 167和B = 334,有意思 看到 Z 可以除以 A 和 B 的乘积,如 167334 / (167 × 334) = 3。给定一个整数 Z,你应该测试它是否是这样的整数。


Input Specification (输入说明):

Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 20). Then N lines follow, each gives an integer Z (10 ≤ Z <231). It is guaranteed that the number of digits of Z is an even number..

译:每个输入文件包含一个测试用例。 对于每种情况,第一行给出一个正整数 N (≤ 20)。 然后是 N 行,每行给出一个整数 Z(10 ≤ Z <231)。 保证Z的位数为偶数。


output Specification (输出说明):

For each case, print a single line Yes if it is such a number, or No if not.

译:对于每种情况,如果是这样的数字,则打印Yes,如果不是,则打印No


Sample Input (样例输入):

3
167334
2333
12345678

Sample Output (样例输出):

Yes
No
No

The Idea:

  • 简单的进行模拟即可 , 不过要注意除数不能为 0
  • 可以用 sscanf()函数将字符串转为数字,也可以反过来,使用to_string()函数将数字转为字符串。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
string z ;
int a , b , z1 ;
int n , len ; 
int main(){
	cin >> n ;
	while(n --){
		cin >> z ;
		len = z.size() / 2 ;
		sscanf(z.c_str() , "%d" , &z1) ;
		sscanf(z.substr(0 , len).c_str() , "%d" , &a) ;
		sscanf(z.substr(len , len).c_str() , "%d" , &b) ;
		if(a == 0 || b == 0) cout << "No" << endl ; // 如果 a 或者 b 全是0
		else{
			double res = z1 * 1.0 / a / b ;
			if(res - floor(res) == 0) cout << "Yes" << endl ; 
			else cout << "No" << endl ; 
		}
	}
	return 0 ;
}

posted @ 2021-08-17 00:21  凌宸1642  阅读(26)  评论(0编辑  收藏  举报