zoukankan      html  css  js  c++  java
  • 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 −. 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相加之后,调整格式再输出的问题,用string比较好处理,可以考虑每三个数为一个分界,注意判断边界......
     
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<map>
     6 using namespace std;
     7 int main()
     8 {
     9     int a,b,sum;
    10     cin>>a>>b;
    11     sum=a+b;
    12     string str;
    13     str=to_string(sum);
    14     for(int i=0;i<str.size();i++)
    15     {
    16         if((str.size()-i-1)%3==0&&str[i]!='-'&&i!=str.size()-1)
    17             cout<<str[i]<<",";
    18         else
    19             cout<<str[i];
    20     }
    21     return 0;
    22 }
    大佬见笑,,
  • 相关阅读:
    字符串加密和解密的常类
    vs2013中使用nuget下载cefsharp winform包
    序列积第m小元素 二分答案优化
    贪心 park
    struct结构体 重载运算符
    最小生成树kruskal 知识点讲解+模板
    c++快读与快输模板
    MZOJ #82 总统竞选
    MZOJ #83 位运算
    MZOJ #81 最小得分和
  • 原文地址:https://www.cnblogs.com/xwl3109377858/p/10660646.html
Copyright © 2011-2022 走看看