zoukankan      html  css  js  c++  java
  • PAT 1132 Cut Integer[简单]

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

     题目大意:给出一个数n,它的位数是K(输入的一定是偶数),那么将前K/2位与后K/2位分开表示为m和t,判断n/(m*t)之后是不是整数。

     //看完这道题就感觉简单极了,但是做了提交之后出现了两个错误

    //这个是错误理解:给出一个数n,它的位数是K(输入的一定是偶数),那么将前K/2位与后K/2位分开表示为m和t,判断n/m/t==K/2,不能出现浮点数的情况哦。

     //看完这道题就感觉简单极了,但是做了提交之后出现了两个错误

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main() {
        int n;
        cin>>n;
        int x;
        for(int i=0;i<n;i++){
            cin>>x;
            int m=x,ct=0;
            while(m!=0){
                m/=10;
                ct++;
            }
            ct/=2;
            vector<int> vt;
            m=x;
            for(int j=0;j<ct;j++){
                vt.push_back(m%10);
                m/=10;
            }
            int k=vt[vt.size()-1];
            for(int j=vt.size()-2;j>=0;j--){
                k=k*10+vt[j];
            }
            if(k==0){
                cout<<"No"<<'
    ';continue;
            }
            double t=1.0*x/k/m;
            if(t==1.0*ct)
                cout<<"Yes"<<'
    ';
            else
                cout<<"No"<<'
    ';
        }
        return 0;
    }/**
    1
    678900,
    这个用例就不行,因为它是/后四舍五入导致的表面上的答案正确。
    **/
    View Code

    1.浮点错误,搜索了一下发现是,/0,或者%0.

    2.解决了之后,再次提交全部样例是答案错误。

    //看完讲解,忽然发现自己理解错题意了,

    现在答案正确了:我的AC:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main() {
        int n;
        cin>>n;
        int x;
        for(int i=0;i<n;i++){
            cin>>x;
            int m=x,ct=0;
            while(m!=0){
                m/=10;
                ct++;
            }
            ct/=2;
            vector<int> vt;
            m=x;
            for(int j=0;j<ct;j++){
                vt.push_back(m%10);
                m/=10;
            }
            int k=vt[vt.size()-1];
            for(int j=vt.size()-2;j>=0;j--){
                k=k*10+vt[j];
            }
            if(k==0){
                cout<<"No"<<'
    ';continue;
            }
            //double t=1.0*x/k/m;
            if(x%(k*m)==0)
                cout<<"Yes"<<'
    ';
            else
                cout<<"No"<<'
    ';
        }
        return 0;
    }/**
    1
    678900,
    这个用例就不行,因为它是/后四舍五入导致的表面上的答案正确。
    **/
    View Code

    代码来自:https://www.liuchuo.net/archives/4090

    #include <iostream>
    using namespace std;
    int main() {
        int n, num;
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            scanf("%d", &num);
            string s = to_string(num);//直接使用tosting函数,厉害了。
            int len = s.length();
            int a = stoi(s.substr(0, len/2));//直接将字符串转换为int 厉害了。
            int b = stoi(s.substr(len/2));
            if (a * b != 0 && num % (a * b) == 0)
                printf("Yes
    ");
            else
                printf("No
    ");
        }
        return 0;
    }

    //真是太简洁了。

    1.可以使用so_string函数,直接将int转化为string

    2.可以使用stoi直接将string转换为int.

    //厉害了。 

  • 相关阅读:
    三台机子配置免密码登录
    cookie,session,token之间的联系与区别
    服务端技术栈
    ConcurrentHashMap
    Integer比较
    meidi
    MySQL字符串中数字排序的问题
    表格td、th强制换行
    mysql 清空表 Truncate及delete区别
    html锚点
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9561415.html
Copyright © 2011-2022 走看看