PAT Advanced 1001

1001 A+B Format (20 分)

Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where ?10?6??≤a,b≤10?6??. The numbers are separated by a space.

Output Specification:

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input:

-1000000 9

Sample Output:

-999,991

这道题刚开始脑袋木了一下没有理解题意,其实题目还是很简单的。提供两种思路1.(通用方法)直接将数字转化为字符串进行操作
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main(){
	long long int a,b;
	string c;
	cin>>a>>b;
	a += b;
	stringstream s1;
	s1<<a;
	s1>>c;
	int num = c.length();
	for( int i = 0; i < num; i++){
		cout<<c[i];
		if(c[i] == ‘-‘)
			continue;
		else if((num-i-1)%3 == 0&& (num-i-1) != 0)
			cout<<",";
	}
	return 0;
}

  2.第二种方法是在网上无意中看到的,感觉也非常好,分享一下。

    鉴于题目的数据范围已经给定,其实最多只有7位数,那么就分以下几种情况:

    1.结果小于1000,无需,

    2.结果大于 1000 小于 1,000,000,需要一个逗号,

    3.结果大于 1,000,000,需要两个逗号。

    

 1 #include<iostream>
 2 #include<math.h>
 3 #include<string>
 4 using namespace std;
 5
 6 int main(){
 7     long long int a,b;
 8     cin>>a>>b;
 9     a+=b;
10     if( a < 0 ){
11         cout<<"-";
12         a = fabs(a);
13     }
14     if( a < 1000)
15         cout<<a;
16     else if( a < 1000000)
17         printf("%d,%03d",a/1000,a%1000);
18     else
19         printf("%d,%03d,%03d",a/1000000,a%1000000/1000,a%1000);
20     return 0;
21 }

注意:输出时因为两个逗号之间经过处理后的数可能为0,所以要用%03d输出,保证数位正确。

原文地址:https://www.cnblogs.com/yxp400/p/10340256.html

时间: 2024-11-10 14:48:09

PAT Advanced 1001的相关文章

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all hi

Pat(Advanced Level)Practice--1016(Phone Bills)

Pat1016代码 题目描述: A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a lon

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c

Pat(Advanced Level)Practice--1060(Are They Equal)

Pat1060代码 题目描述: If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123*105 with simple chopping. Now given the number of significant digits on a machine and two flo