A1132 Cut Integer (20 point(s))

截取字符串长度

1. 原文

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:

1
2
3
4
3
167334
2333
12345678

Sample output:

1
2
3
Yes
No
No

2. 解析

截取长度

  • for (int i = 0; i < len/2; ++i)
  • for (int i = len/2; i < len; ++i)

判断 if (a*b!=0&&total%(a*b)==0)

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<cstdio>
#include<cstring>
char str[100];
int main()
{
int n;
scanf("%d",&n);
for (int i = 0; i < n; ++i)
{
scanf("%s",str);
int len=strlen(str);
int a=0; int b=0; int total=0;
for (int i = 0; i < len/2; ++i)
{
total=total*10+str[i]-'0';
a=a*10+str[i]-'0';
}
for (int i = len/2; i < len; ++i)
{
total=total*10+str[i]-'0';
b=b*10+str[i]-'0';
}
if (a*b!=0&&total%(a*b)==0)
{
printf("Yes\n");
}else{
printf("No\n");
}

}
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/02/A1132/
  • 发布时间: 2019-09-02 22:37
  • 更新时间: 2021-10-29 14:10
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!