个人学习记录,代码难免不尽人意。
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数组。