PAT (Advanced Level) Practice 1132 Cut Integer (20分) (atoi、stoi区别、stringstream使用)
1.题目
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
2.题目分析
1.atoi、stoi的区别;
atoi是用来将char*转换成int,stoi是将string转换成int
2.stringstream使用
重复使用要用ss.str(""); ss.clear();清空
3.代码
#include<iostream>
#include<string>
#include<cstring>
#include<sstream>
using namespace std;
int main()
{
int n;
long long a, b, c;
stringstream ss;
string temp;
cin >> n;
for (int i = 0; i<n; i++)
{
cin >> temp;
ss << temp;
ss >> a;
ss.str(""); ss.clear();
ss << temp.substr(0,temp.length()/2);
ss >> b;
ss.str(""); ss.clear();
ss << temp.substr(temp.length() / 2, temp.length() / 2);
ss >> c;
ss.str(""); ss.clear();
if (b*c!=0&&a%( b*c)==0)
printf("Yes\n");
else printf("No\n");
}
}
#include<iostream>
#include<string>
#include<cstring>
#include<sstream>
using namespace std;
int main()
{
int n;
long long a, b, c;
string temp;
cin >> n;
for (int i = 0; i<n; i++)
{
cin >> temp;
a = stoi(temp);
b = stoi(temp.substr(0, temp.length() / 2));
c = stoi(temp.substr(temp.length() / 2, temp.length() / 2));
if (b*c!=0&&a%( b*c)==0)
printf("Yes\n");
else printf("No\n");
}
}