PAT 1132 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<cstdio>
#include<string>
#include<iostream> 
#include<algorithm>
#include<cmath>
using namespace std;
// const double eps=1e-8;
// bool equ(double a,double b){
 
//     if(fabs(a-b)<eps) return true;
//     else return false;
// }
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		string num;
		cin>>num;
		int number,a,b;
		sscanf(num.c_str(),"%d",&number);
		int digit=num.size()/2;
		sscanf(num.substr(0,digit).c_str(),"%d",&a);
		sscanf(num.substr(digit,num.size()).c_str(),"%d",&b);
        if(number%a==0){
            number=number/a;
            if(b!=0){
                if(number%b==0){
                cout << "Yes" << endl;
            }else cout << "No" <<endl;
            }
            else cout << "No" <<endl;
        }
        else cout << "No" <<endl;
		
	}
  
}

这道题很简单,但是挺坑的,因为题目给的样例正好是个特例,两个数位为3,得到的商也正好是3,让我一开始误以为最后得到的结果必须是两个除数的位数(汗),然后后面我改过来之后最后两个测试点显示“浮点错误”,我才反应过来是除数可能为0,比如给的数字是“123000”这样子,所以必须再判断一下。最后测试点全部通过。这道题花了我很多时间,烦。
ps:我用了sscanf来将char数组转换成int,需要注意不能用string,需要将string转成char数组。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值