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
    ");
    	}
    }
  • 相关阅读:
    linux常用命令
    mysql 开发基础系列20 事务控制和锁定语句(上)
    sql server 性能调优之 资源等待 CXPACKET
    mysql 开发基础系列19 触发器
    mysql 开发基础系列18 存储过程和函数(下)
    mysql 开发基础系列17 存储过程和函数(上)
    sql server 性能调优之 资源等待PAGEIOLATCH
    mysql 开发基础系列16 视图
    mysql 开发基础系列15 索引的设计和使用
    sql server 性能调优之 当前用户请求分析 (1)
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12788876.html
Copyright © 2011-2022 走看看