pat 1132 Cut Integer(20 分)
Posted getcharzp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pat 1132 Cut Integer(20 分)相关的知识,希望对你有一定的参考价值。
1132 Cut Integer(20 分)
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 <map> 2 #include <set> 3 #include <queue> 4 #include <cmath> 5 #include <stack> 6 #include <vector> 7 #include <string> 8 #include <cstdio> 9 #include <cstring> 10 #include <climits> 11 #include <iostream> 12 #include <algorithm> 13 #define wzf ((1 + sqrt(5.0)) / 2.0) 14 #define INF 0x3f3f3f3f 15 #define LL long long 16 using namespace std; 17 18 const int MAXN = 2e3 + 10; 19 20 int t, Z, A, B, len, temp; 21 22 char s[MAXN]; 23 24 int my_pow(int x, int n) 25 { 26 int ans = 1; 27 while (n) 28 { 29 if (n & 1) ans *= x; 30 x *= x; 31 n >>= 1; 32 } 33 return ans; 34 } 35 36 int main() 37 { 38 scanf("%d", &t); 39 while (t --) 40 { 41 Z = A = B = 0L; 42 scanf("%s", &s); 43 44 len = strlen(s), temp = len >> 1; 45 for (int i = 0, j = len - 1; i < len; ++ i, -- j) 46 Z += (int)(s[i] - ‘0‘) * my_pow(10, j); 47 for (int i = 0, j = temp - 1; i < temp; ++ i, -- j) 48 A += (int)(s[i] - ‘0‘) * my_pow(10, j); 49 for (int i = temp, j = temp - 1; i < len; ++ i, -- j) 50 B += (int)(s[i] - ‘0‘) * my_pow(10, j); 51 52 if ((A * B) != 0 && Z % (A * B) == 0) printf("Yes "); 53 else printf("No "); 54 } 55 return 0; 56 }
以上是关于pat 1132 Cut Integer(20 分)的主要内容,如果未能解决你的问题,请参考以下文章
PAT Advanced 1132 Cut Integer (20分)