zoukankan      html  css  js  c++  java
  • PAT 甲级 1001 A+B Format (20)(20 分)

    1001 A+B Format (20)(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

    Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

    Output

    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


    注意几个输出点:-991 991 1001 -1001

     1 #include<iostream>
     2 #include <sstream>
     3 #include<string>
     4 #include<stack>
     5 #include<typeinfo>
     6 
     7 using namespace std;
     8 
     9 int main()
    10 {
    11   int a,b,flag=0,temp=0;
    12   
    13   cin>>a>>b;
    14   
    15   a+=b;
    16   
    17   if(a<0)
    18   {
    19       flag=1;
    20       cout<<"-";
    21   }
    22   
    23   stringstream ss;
    24   ss<<a; 
    25   
    26   string str=ss.str();
    27   
    28   stack<char> s;
    29   
    30   for(int i=str.length()-1;i>=flag;--i)
    31   {
    32       s.push(str[i]);
    33       ++temp;
    34       
    35       if(temp%3==0 && i-1>=flag)
    36           s.push(',');
    37   }
    38   
    39   while(!s.empty())
    40   {
    41       cout<<s.top();
    42       s.pop();
    43   }
    44   
    45   cout<<endl;
    46   
    47   return 0;
    48 }
  • 相关阅读:
    A*算法的原理 <转>
    Unity性能优化之 Draw Call原理<转>
    关于XML中:XmlNode和XmlElement的涵义及不同之处
    MySql 数据库连接池
    代码中批量执行Oracle SQL语句
    科密指纹考勤机B329采集
    VB网络编程(webbrowser+Inet+抓包封包+经验)
    Lambda表达式
    网络编程
    多线程
  • 原文地址:https://www.cnblogs.com/cdp1591652208/p/9354123.html
Copyright © 2011-2022 走看看