A1132. Cut Integer

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

#include<iostream>
#include<cstdio>
using namespace std;
long long Z;
int num2Cut(long long Z){
    long long temp[500], bit, Z2 = Z;
    int pt = 0;
    do{
        bit = Z % 10;
        Z = Z / 10;
        temp[pt++] = bit;
    }while(Z != 0);
    long long a = 0, b = 0;
    for(int i = pt - 1; i >= pt / 2; i--){
        a = a * 10 + temp[i];
    }
    for(int i = pt / 2 - 1; i >= 0; i--){
        b = b * 10 + temp[i];
    }
    if(a*b == 0)
        return 0;
    if(Z2 % (a*b) == 0)
        return 1;
    else return 0;
}
int main(){
    int N;
    scanf("%d", &N);
    for(int i = 0; i < N; i++){
        scanf("%lld", &Z);
        int tag = num2Cut(Z);
        if(tag == 0)
            printf("No\n");
        else printf("Yes\n");
    }
    cin >> N;
    return 0;
}
View Code

总结:
1、题意:将一个位数为偶数的数字分成两半,看看这两半的乘积能不能被原数整除。
2、注意依次取一个数的各个位,得到的数组中是倒着的,由高位到低位。
2、在验证能否整除时,要注意被除数为0的情况。如1000被分成10和00.
posted @ 2018-08-29 13:16  ZHUQW  阅读(241)  评论(0编辑  收藏  举报