PAT 1132. Cut Integer (20)

Cutting an integer means to cut a K digits long 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 x 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<=231). 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
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
#include <cstdio>
#include <cstring>

int toInt(char *z, int s, int e) {
int num = 0;
for (int i = s; i <= e; i++) {
num = num * 10 + z[i] - '0';
}
return num;
}

int main() {
int n = 0;
scanf("%d", &n);
char z[30];
for (int i = 0; i < n; i++) {
scanf("%s", z);
int znum = toInt(z, 0, strlen(z) - 1);
int a = toInt(z, 0, strlen(z) / 2 - 1);
int b = toInt(z, strlen(z) / 2, strlen(z) - 1);
if ((a * b != 0) && znum % (a * b) == 0) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}