尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

【PAT甲级真题】- A+B Format(20)

【PAT甲级真题】- A+B Format(20) 题目来源AB Format题目描述点击链接自行查看注意点首位不要加逗号DiscriptionCalculatea b ababand 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 integersa aaandb bbwhere− 10 6 ≤ a , b ≤ 10 6 −10^6≤a,b≤10^6−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 9Sample Output:-999,991题目大意输入两个整数a , b a,ba,b计算a b abab并按照规格化整数格式输出规格化的格式即每三个位加一个逗号分隔思路简介20分的题目还是很简单的先计算a b abab的值然后转换成字符串讨论一下正负的情况对于正数我们每数三个数就用insert()插入一个逗号注意第一位不要算因为对于100,000的情况是不用在最前面加逗号的对于负数去掉负号后跟正数是一样的正数是第一位不算那负数就是前两位不算str.insert(pos, inserted);会在原字符串的pos位置 插入第二个参数指定的字符串其他的string可以查看外菜鸟教程遇到的问题无一遍过代码/** * https://pintia.cn/problem-sets/994805342720868352/exam/problems/type/7?problemSetProblemId994805528788582400 * 模拟 */#includebits/stdc.husingnamespacestd;voidsolve(){inta,b;cinab;intsab;string strto_string(s);//coutstr\n;intlenstr.size();intcnt0,start(str[0]-?1:0);for(intilen-1;istart;--i){if(cnt2){str.insert(i,,);cnt0;}else{cnt;}}coutstr\n;}intmain(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);//fstream in(in.txt,ios::in);cin.rdbuf(in.rdbuf());intT1;//cinT;while(T--){solve();}return0;}
返回列表