PAT1001

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

Calculate a + b and output the sum in standard format

计算a+b并且输出标准形式的和

-- 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.

对于每一个输入文件包含一个测试用例。每个测试用例包含一对数a和b,-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.

对于每个测试用例,你需要在一行中输出ab的和。这个和必须以标准的形式被写出。

Sample Input

-1000000 9

Sample Output

-999,991
 
简单题目,主要问题在于格式。
然后有两个比较好的技巧,一个是提前处理0,防止除数为0。还有一个是提前处理负数的请求。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>

using namespace std;

int main()
{
    char ch[20];//用于存放最终输出的数据
    int a,b,c;
    int i=0,j=0;
    cin>>a>>b;
    c=a+b;
    //提前处理0的请求
    if(c==0)
    {
        cout<<0;
    }
    else
    {
        //化负数为正数,提前处理请求
        if(c < 0)
        {
            cout<<"-";
            c=-c;
        }
        while (c>=10)
        {
            ch[i] = c%10 + ‘0‘;
            c/=10;
            i++;
            //用J来记录访问位数为3的逗号
            if((i-j)%3 == 0)
            {
                ch[i] = ‘,‘;
                j++;
                i++;
            }
        }
        ch[i] = c + ‘0‘;

        //循环输出最后的结果
        for (;i>=0;i--)
            cout<<ch[i];
    }
    return 0;
}
 
网上还看到一个比较取巧的方法。
#include<stdio.h>
int main()
{
  int a,b;
  int sum;
  while(scanf("%d%d\n",&a,&b) != EOF){
        sum = a+b;
    if(sum < 0){
    printf("-");
    sum = -sum;
    }
    if(sum>=1000000){
        printf("%d,%03d,%03d\n",sum/1000000, (sum/1000)%1000, sum%1000);
    }
    else if(sum >= 1000){
        printf("%d,%03d\n",sum/1000,sum%1000);
    } else{
        printf("%d\n", sum);
    }
  }
  return 0;
}
时间: 2024-08-06 14:43:01

PAT1001的相关文章

(甲)PAT-1001

1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 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 fi

PAT-1001 采花生

题目描述 鲁宾逊先生有一只宠物猴,名叫多多.这天,他们两个正沿着乡间小路散步,突然发现路边的告示牌上贴着一张小小的纸条:“欢迎免费品尝我种的花生!——熊字”. 鲁宾逊先生和多多都很开心,因为花生正是他们的最爱.在告示牌背后,路边真的有一块花生田,花生植株整齐地排列成矩形网格.有经验的多多一眼就能看出,每棵花生植株下的花生有多少.为了训练多多的算术,鲁宾逊先生说:“你先找出花生最多的植株,去采摘它的花生;然后再找出剩下的植株里花生最多的,去采摘它的花生;依此类推,不过你一定要在我限定的时间内回到路

pat1001. Battle Over Cities - Hard Version 解题报告

/**题目:删去一个点,然后求出需要增加最小代价的边集合生成连通图思路:prim+最小堆1.之前图中未破坏的边必用,从而把两两之间可互达的点集合 合并成一个点2.求出不同点集合的最短距离,用prim+最小堆求出最小生成树 kruskal1.之前图中未破坏的边必用,全部加到图中2.途中被破坏的边按照边权从小到大的顺序依次加入图中,直到图变为连通图 两个方法的对应一个点的最小生成树的复杂度都是nlogm,第二个方法较好写 优化:1.未破坏的边直接加入图中2.当有n-2条边(当前删去一个点后,图中n-

PAT1001. A+B Format (20)

1001. A+B Format (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 cont

PAT1001. 害死人不偿命的(3n+1)猜想 (15

卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去,最后一定在某一步得到n=1.卡拉兹在1950年的世界数学家大会上公布了这个猜想,传说当时耶鲁大学师生齐动员,拼命想证明这个貌似很傻很天真的命题,结果闹得学生们无心学业,一心只证(3n+1),以至于有人说这是一个阴谋,卡拉兹是在蓄意延缓美国数学界教学与科研的进展…… 我们今天的题目不是证明卡拉兹猜想,而是对给定的任一不超过1000的正整数n,简单地数一下