1132. Cut Integer (20)

xiaoxiao2021-02-28  35

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

解题 计算整数的位数,划分成两个数A,B,最后看能不能被A*B整除。

code: #include<iostream> #include<cstdio> using namespace std; int len(int a) { int count = 0; int b = a % 10; a = a / 10; while (true) { if (a == 0 && b == 0) break; b = a % 10; a = a / 10; count++; } return count; } void cut(int &a,int &b) { int l = len(a); l /= 2; b = 0; int ll = l; //分离两数 while (l-- >0) { b *= 10; b += a % 10; a = a / 10; } //分离出的b恰好相反,故进行倒序处理 int c = 0; while (ll-- > 0) { c *= 10; c += b % 10; b = b / 10; } b = c; } bool isT(int in) { int a = in; int b = 0; cut(a, b); //两个测试点,有浮点错误(除0了) if (a == 0 || b == 0) return false; if (in%a) return false; else if ((in / a) % b) return false; else return true; } int main() { int c = 0; int v = 0; cin >> c; for (int i = 0; i < c; i++) { cin >> v; if (isT(v)) cout << "Yes" << endl; else cout << "No" << endl; } system("pause"); return 0; }
转载请注明原文地址: https://www.6miu.com/read-2619696.html

最新回复(0)