zoukankan      html  css  js  c++  java
  • [PAT]A+B Format[简单]

    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

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<stdlib.h>
    #include<algorithm>
    using namespace std;
    char s[30];
    int main()
    {
       int a,b;
       cin>>a>>b;
       a=a+b;
       int start=0;
        int temp,len=0;
        b=a;
        if(a<0)a=-a;
        if(a==0)cout<<0;
       while(a){
           temp=a%10;
           a/=10;
    
            len++;
            if(len>3&&len%3==1)
                s[start++]=',';
            s[start++]=temp+'0';
       }
       if(b<0)
            cout<<'-';
    
       for(int i=start-1;i>=0;i--)
            cout<<s[i];
        return 0;
    }

    //就是求a+b的和,并且输出。非常简单。并且和都不会溢出int.第一次提交的时候只得了19分,发现是因为在输入两个0时,程序没有输出。之后对两者和进行判断,若和为0,直接输出0即可。

  • 相关阅读:
    熟悉常用的HDFS操作
    爬虫大作业-爬取B站弹幕
    数据结构化与保存
    使用正则表达式,取得点击次数,函数抽离
    爬取校园新闻首页的新闻
    网络爬虫基础练习
    综合练习:词频统计
    理解MapReduce
    熟悉常用的HBase操作
    熟悉常用的HDFS操作
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/9313696.html
Copyright © 2011-2022 走看看