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
计算 a + b 并以标准格式输出总和 - 也就是说,数字必须用逗号分隔成三组(除非少于四位数字)。
输入规范:
每个输入文件包含一个测试用例。每种情况都包含一对整数 a 和 b,其中
−10≤a,b≤10
数字用空格分隔。
输出规格:
对于每个测试用例,您应该在一行中输出 a 和 b 的总和。总和必须以标准格式写入。
开始只有15分,然后一直不知道问题出在哪
#include<iostream>
int dp[10] = { 0 };
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
int c = a + b;
int flag = 0;
if (c == 0)
{
cout << 0;
return 0;
}//别忘记健全性!还有等于0的情况.
if (c < 0)
{
c = -c;
cout << "-";
}
while (c)
{
dp[++flag] = c % 1000;
c /= 1000;
}
while (flag)
{
cout << dp[flag];
if (flag != 1)
cout << ',';
flag--;
//最后一个不输出,
}
//1.问题出在取模负数时有多个负号,所以要设条件
//14分,不知道哪里错了,所以以后输出格式转成字符为佳,不要数字取整取模!
//会有未知错误的风险!
return 0;
}
后来改用了字符串输出
#include <iostream>
#include <string>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
string str = to_string(a + b);
int len = str.length();
if (str[0] == '-') {
for (int pos = len - 3; pos > 1; pos -= 3) {
str.insert(pos, ",");
}
}
//插入是在当前的下标直接插入,因为下标从0开始与长度不一样
//如最大下标是6,而len是7,所以要在4下标插入即-3
//而正数没有+-号,所以少了一个符号就要重新判断
else {
for (int pos = len - 3; pos > 0; pos -= 3) {
str.insert(pos, ",");
}
}
cout << str << endl;
return 0;
}
回顾原先直接整数的解法
其实实际上是行不通的,会出现很多问题,一下算法不完整不是AC当作笔记了
//问题就是开头确定好","位置,结尾不输出","即可
#include<iostream>
int dp[20] = { 0 };
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
int c = a + b;
int flag = 0;
if (c == 0)
{
cout << 0;
return 0;
}//别忘记健全性!还有等于0的情况.
if (c < 0)
{
c = -c;
cout << "-";
}
while (c)
{
dp[++flag] = c % 10;
c /= 10;
}
int begincnt = flag % 3;//记录开始插入","的位置
int cnt = 0;//当前输出数字个数
while (flag)
{
cout << dp[flag];
if (cnt == begincnt)
{
cout << ',';
cnt = 0;//重新开始记录
flag--;//跳转新的下标
while (flag)
{
cout << dp[flag];
if (cnt % 3 == 0 && flag != 1)
{
cout << ',';
}
cnt++;
flag--;
if (flag == 1)
return 0;//出口
}
}
cnt++;
flag--;
//最后一个不输出,
//最后一个不输出,
// 因为是倒序输出,前面不管你是什么数,只要你本身有三位数字以上,前面都是可以直接打印","的,然后一直3位数加","
// 直到最后三个数不加.
// 本质上是因为你把每个部分都看成3个数字组成才能直接打印","的效果,一旦不能看作三个部分组成,你就失去了直接输出","
// 的能力!
}
//我终于知道了,如果取整取模的时候如c=1000000时,分割字符串的时候000是不会被分割成想要的结果的
//在整型的表达中dp是0,而不是000!
//所以最终,你想要分割三位数如果是001,002,030这样的数,还得是用char型,int型是不健全的!
//方法没问题,想法是没问题的,但整型储存结果本身是有问题的,这是程序默认的结果,没有办法
//所以唯一的使用分割不用字符串的方法就是不要一次取整三个而是1一个
// 包括之前的分割回文年份的分割也是如此,除非是明确非0,如果有0,你就只能一个个数字分割才能保证完全正确!
//太繁琐了,有太多算法漏洞要弥补,也可以卡数据分情况如0-3,4-5,6分情况设置方法
//但过于啰嗦复杂放弃维护了。
//不用char硬用数字分割吃力不讨好,数据范围又小还bug多,不要再维护和救这个lj算法了
return 0;
}
另解,使用容器
#include<iostream>
#include<vector>
using namespace std;
//使用容器的另一种AC
int main() {
int a, b;
cin >> a >> b;
int c = a + b;
if (c == 0)
{
puts("0");
return 0;
}
int cnt = 0;
vector<char> num;
if (c < 0) {
c = abs(c);
cout << "-";
}
while (c)
{
cnt++;
num.push_back((c % 10)+'0');//先对整数取模,然后+'0'转化为字符存入.
if (cnt % 3 == 0)num.push_back(',');
c /= 10;
}
int n = num.size();
for (int i = n - 1; ~i; i--) {
/*cout << ~i << endl;*///~i的作用等价于i!=-1,
if (i == n - 1 && num[i] == ',')continue;
//开始存放是从末尾开始,所以倒序输出,存放的时候我们能保证一开始存入结尾的时候
//一定不会有'0',但是不能保证开头,所以如果开头存入了','我们就不输出
cout << num[i];
}
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)