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 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
看範圍發現,int型是完全ok的,所以用int就行了,相加後再轉為string,來分析什麼時候加逗號,將string與3取餘,將其分成3的段,那麼最前面剩下的就是不夠的,每當i=餘數時,先輸出,號,之後就是每過3個輸出一個逗號,相當於於3取餘後仍是之前的數。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
string c=to_string(a+b);
for(int i=0;i<c.length();i++){
cout<<c[i];
if(c[i]=='-') continue;
if(c.length()%3==(i+1)%3&&i!=c.length()-1) cout<<",";
}
}
相關文章
- [#181024][PAT Practice] A+B FormatORM
- 【PTA甲級、C++簡單解答】1001 A+B Format (20分)C++ORM
- [PAT B] 1011 A+B 和 C
- PAT-B 1016 部分A+B
- PAT-B 1093 字串A+B 【集合】字串
- PAT-B 1011 A+B 和 C
- PAT答案(D進位制的A+B)
- P1001 A+B Problem(整活-dijstra堆最佳化)JS
- PAT-B 1022 D進位制的A+B【進位制】
- [PAT B] 1001 害死人不償命的 (3n+1) 猜想
- 【PAT甲級A1065】A+B and C (64bit) (20分)(c++)C++
- PAT (Basic Level) Practice 1001 害死人不償命的(3n+1)猜想
- PAT-B 1001 害死人不償命的(3n+1)猜想【模擬】
- PAT乙級1001 || 害死人不償命的(3n+1)猜想(C語言)C語言
- PAT B1001 害死人不償命的(3n+1)猜想(簡單模擬)
- A+B
- As Easy As A+B
- Format and un-format money/currency in JavaScriptORMJavaScript
- PAT B1022 D進位制的A+B(進位制轉換板題,簡單模擬)
- A+B問題
- Json formatJSONORM
- 求助:TypeError: unsupported format string passed to NoneType.__format__ErrorORMNone
- hdu 1720 A+B Coming
- set excel formatExcelORM
- COMP1001J Playing Grid
- 問題解決:TypeError: unsupported format string passed to NoneType.__format__ErrorORMNone
- [Algorithm] 1. A+B ProblemGo
- 1016. 部分A+B (15)
- [Ruby]format xml with RubyORMXML
- oracle工具 awr formatOracleORM
- vim Google style formatGoORM
- Python入門:A+B問題Python
- A+B 演算法問題演算法
- 2020/12/27 G小明A+B
- 1016 部分A+B (15 分)JavaJava
- HDU 1040 As Easy As A+B(堆排序)排序
- 1001. 溫度轉換 (Standard IO)
- soj-1001. NFA識別語言