1001 A+B Format
Posted psogos
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 pair of integers a and b where −106≤a,b≤106. 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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
comma: 逗号
-
代码
#include<iostream> #include<vector> #include<string> #include<math.h> #include<algorithm> #include<stack> using namespace std; stack<char> st; int main() int n,m; cin >> n >> m; **string s = to_string(n + m); //把相加结果转换为字符串** int cnt = 0;//用来计算数字的个数 for(int i = s.length() - 1; i >= 0; i--) st.push(s[i]); if (isdigit(s[i])) //判断为是数字 cnt++; if (cnt != 0 && cnt % 3 == 0 && i != 0 && isdigit(s[i-1])) st.push(','); while(!st.empty()) cout << st.top(); st.pop(); return 0;
-
总结
挺简单一题,关键是把数字转换为字符串,同时记得这个栈里的元素要设为字符类型。
format格式
{}表示字段
‘1{},2{},3{}‘.format(‘a‘,‘b‘,‘c‘)
‘{0},I {1} {2}‘.format("Hi","miss","you")
‘{a},I {b} {c}‘.format(a="Hi","b=miss",c="you")
‘{0},I {a} {b}‘.format("Hi",b="miss",c="you")
注意默认的放在定义的前边
‘{{0}}‘.format("无输出")
输出 ‘{0}‘
"{0:.1f}{1}".format(27.658,‘GB‘)#没有%号
相当于‘{%.2f}{%s}‘%(2.778,‘cm‘)
m.n m最小域宽,n为小数点数,采用四舍五入
– 左对齐
+ 正值加正号
# 显示八进制或十六进制前标‘0o‘,‘0x‘
"%#x" % 10
"%#o" % 10
0 空位补零
"%010d"% 10
显示字符
"%c" % 97
"%c %c %c" % 97,98,99
以上是关于1001 A+B Format的主要内容,如果未能解决你的问题,请参考以下文章