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
    ");
    	}
    }
  • 相关阅读:
    vaddin使用技巧
    数据库连接&数据库进程&数据库操作
    gcc编译器参数使用及解决
    smtp服务器搭建(实现本地通讯)
    jdk+tomcat+mysql搭建网站无法打开
    ie8此加载项无法恢复&网站还原错误问题解决=lr成功打开ie成功录制脚
    loadrunner打不开ie&ie默认浏览器设置方法
    判断iis是否已经安装
    loadrunner常见问题
    代码中字符串比较长时可以分行写
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12788876.html
Copyright © 2011-2022 走看看