zoukankan      html  css  js  c++  java
  • 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 <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

    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
    ");
    		else printf("No
    ");
    	}
    }
    #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
    ");
    		else printf("No
    ");
    	}
    }
  • 相关阅读:
    element多选下拉框的坑
    滚动条样式全局修改
    element表格自带排序的坑
    c++学习
    HTTP与TCP的区别和联系
    JSP整理
    学习目标
    mysql操作
    JAVA Date、String、Calendar类型之间的转化
    后台数据类型接收问题总结
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12788876.html
Copyright © 2011-2022 走看看