首页 > 其他 > 详细

PAT A1132 Cut Integer (20 分)

时间:2019-02-23 18:02:26      阅读:167      评论:0      收藏:0      [点我收藏+]

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.

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 <2?31??). It is guaranteed that the number of digits of Z is an even number.

Output Specification:

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

Sample Input:

3
167334
2333
12345678

Sample Output:

Yes
No
No
 
技术分享图片
 1 #include <stdio.h>
 2 #include <string>
 3 #include <iostream>
 4 using namespace std;
 5 int s2n(string s){
 6   int res=0;
 7   for(int i=0;i<s.length();i++){
 8     res = 10*res + s[i]-0;
 9   }
10   return res;
11 }
12 int main(){
13   string s;
14   int n;
15   scanf("%d",&n);
16   for(int i=0;i<n;i++){
17     cin>>s;
18     int pos=s.length()/2;
19     string s1,s2;
20     int n1,n2;
21     s1=s.substr(0,pos);
22     s2=s.substr(pos,s.length()-pos);
23     n1=s2n(s1);
24     n2=s2n(s2);
25     if((n1==0 || n2==0) || s2n(s)%(n1*n2)!=0)printf("No\n");
26     else printf("Yes\n");
27   }
28 }
View Code

注意点:后面两个测试点是其中一半为0的情况,要额外判断。显示浮点错误说明除数遇到了0。

PAT A1132 Cut Integer (20 分)

原文:https://www.cnblogs.com/tccbj/p/10423443.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!