• pat 1132


    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 <). 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



    题意:把一个偶数位的数z截成等长度的两段数a和b,问这个数z是否整除a*b
    思路:char数组读入数字,然后截取,转换成整数。
    注意:有时候a*b=0,要特判一下,否则产生浮点错误(测试点2、3)

    代码如下:
    #include<cstdio>
    #include<cstring>
    void fun(char num[]){
        int len=strlen(num);
        long long a=0;
        long long b=0;
        long long z=0;
        for(int i=0;i<len/2;i++){
            a=a*10+num[i]-'0';
        }
        for(int i=len/2;i<len;i++){
            b=b*10+num[i]-'0';
        }
        for(int i=0;i<len;i++)
            z=z*10+num[i]-'0';
        if(a*b==0)
            printf("No");
        else{
            if(z%(a*b)==0)
                printf("Yes");
            else
                printf("No");
        }    
    
    }
    int main(){
    
        int n;
        scanf("%d",&n);
        char num[15];
        for(int i=0;i<n;i++){
            scanf("%s",num);
            fun(num);
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    dfs介绍
    UVA11149 矩阵快速幂
    UVA1476 三分法
    漂亮的表达式!(不断补充)
    BC Round#33 B(10的18次方,快速乘法+快速幂取余)
    UVA 1639(组合数学)
    UVA 10612(数论找规律)
    小模板
    1589象棋(大模拟)
    bnuoj 29368 Check the Identity(栈)
  • 原文地址:https://www.cnblogs.com/foodie-nils/p/13269006.html
Copyright © 2020-2023  润新知