PAT A1001 A+B Format

PAT A1001 A+B Format

题目描述:

  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 /****************************************************
 2 PAT A1001 A+B Format
 3 ****************************************************/
 4 #include <iostream>
 5 #include <string>
 6
 7 using namespace std;
 8
 9 int main() {
10     int num1 = 0, num2 = 0, sum = 0;
11
12     cin >> num1 >> num2;
13
14     sum = num1 + num2;
15
16     if (sum < 0) {
17         cout << ‘-‘;
18         sum *= -1;
19     }
20
21     string sumString = to_string(sum);
22     for (int i = 0; i < sumString.size(); ++i) {
23         cout << sumString[i];
24         if ((i + 1) % 3 == sumString.size() % 3 && i != sumString.size() - 1) {
25             cout << ‘,‘;
26         }
27     }
28
29     return 0;
30 }

注意事项:

  无。

原文地址:https://www.cnblogs.com/mrdragon/p/11412264.html

时间: 2024-10-09 03:18:31

PAT A1001 A+B Format的相关文章

PAT 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 Each input file contains one test case. Each case contains a pair of integer

A1001. 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 Each input file contains one test case. Each case contains a pair of integer

PAT 1001. A+B Format(水题)

#include<cstdio> #include<cstring> using namespace std; char s[10]; int main() { int a,b; while(scanf("%d%d",&a,&b)==2) { memset(s,0,sizeof(s)); a=a+b; if(a==0) { printf("0\n"); continue; } int aa=a; b=0; if(a<0)

PAT 1001. A+B Format

#include<stdio.h> int a, b; int sum; int main(){ while (scanf("%d %d", &a, &b)!=EOF){ sum = a + b; if (sum < 1000&&sum>-1000)printf("%d\n", sum); else{ int sum1 = sum % 1000; int sum2 = sum / 1000; int sum3

PTA A1001&amp;A1002

从今天起每天刷1-2题PAT甲级 A1001 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 c

PAT甲级考试题库1001 A+B Format 代码实现及相关知识学习

准备参加九年九月份的PAT甲级证书考试,对网站上的题目进行总结分析: 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). 计算a+b的值并以一定格式输出其和sum(数字需要

PAT Advanced level 1001 A+B Format

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 pa

PAT甲级 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 Each input file contains one test case. Each case contains a pair of i

1001. A+B Format (20) ——PAT (Advanced Level) Practise

题目信息: 1001. A+B Format (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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