PAT (Advanced Level) Practice — 1132 Cut Integer (20 分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37345402/article/details/87942794

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805347145859072

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

!注意:如果不判断两个分离数相乘的结果是否为0,则会出现浮点错误。

#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
int main(){
	int n;
	char a[11];
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a;
		int len=strlen(a);
		ll m=0,m1=0,m2=0,m3=0;
		for(int i=0;i<len;i++){
			m=m*10+(a[i]-'0');
		}
		for(int i=0;i<len/2;i++){
			m1=m1*10+(a[i]-'0');
		}
		for(int i=len/2;i<len;i++){
			m2=m2*10+(a[i]-'0');
		}
		m3=m1*m2;
		if(m3!=0&&m%m3==0){
			printf("Yes\n");
		} else{
			printf("No\n");
		}
	}
	
	return 0;
}

利用stoi :字符串转数值

#include<iostream>
#include<string>
using namespace std;
typedef long long ll;
int main(){
	int n;
	cin>>n;
	string s;
	for(int i=0;i<n;i++){
		cin>>s;
		int len=s.length();
		ll m1,m2,m3,m;
		m=stoi(s);
		m1=stoi(s.substr(0,len/2));
		m2=stoi(s.substr(len/2,len)); 
		m3=m1*m2;
		if(m3!=0&&m%m3==0){
			printf("Yes\n"); 
		}else{
			printf("No\n");
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37345402/article/details/87942794
今日推荐