前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【PAT甲级】Cut Integer

【PAT甲级】Cut Integer

作者头像
喜欢ctrl的cxk
发布2019-11-08 14:22:27
9400
发布2019-11-08 14:22:27
举报
文章被收录于专栏:Don的成长史

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_42449444/article/details/89449189

Problem Description:

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:

代码语言:javascript
复制
3
167334
2333
12345678

Sample Output:

代码语言:javascript
复制
Yes
No
No

解题思路:

输入string型的数字str,数字的位数为K(题目保证了K是偶数),然后通过stoi()函数把string型的数字str强制转换成int型的数字Z,通过substr()函数把数字str分成A和B,最后判断数字Z能否整除(数字A×数字B)即可。需要注意的是:除数不能为0!A×B不能为0,我第一次提交的时候就有俩个测试点出现了"Float Point Exception",浮点错误。

AC代码:

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int N;
    cin >> N;
    while(N--)
    {
        string str;
        cin >> str;
        int K = str.length();  //数字Z的位数,题目保证了K是偶数
        int Z = stoi(str);   //string型强制转换成int型
        int A = stoi(str.substr(0,K/2));   //A是数字Z的前K/2位数字
        int B = stoi(str.substr(K/2));     //B是数字Z的后K/2位数字
        if((A*B != 0) && (Z%(A*B) == 0))   //当A*B=0时,编译器会报错"Float Point Exception",浮点错误
        {
            cout << "Yes" << endl;
        }
        else
        {
            cout << "No" << endl;
        }
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/04/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Description:
  • Input Specification:
  • Output Specification:
  • Sample Input:
  • Sample Output:
  • 解题思路:
  • AC代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档