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
    ");
    	}
    }
  • 相关阅读:
    性能测试
    领域驱动设计(DDD)的实际应用
    Js模块模式
    Roslyn and NRefactory
    前端与后端分离的架构实例3
    angular + easyui 做界面验证
    java.lang.OutOfMemoryError: unable to create new native thread(转)
    oracle client server那点事
    一次处理ORA-07445的历险记(转)
    一条执行4秒的sql语句导致的系统问题 (转)
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12788876.html
Copyright © 2011-2022 走看看