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

    1001. A+B Format (20)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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
    

    思路:

    注意查看题目的意思,一个数分为三组,当然大于等于1e6一组(注意等号),介于小于1e6并且大于等于1e3的是一组(注意等号),小于1e3的是一组。

    中间数值的输出注意用03d,这个举例,比如:1,000

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    const int moda = 1e6;
    const int modb = 1e3;
    int main() {
    	int a,b,sum;
    	scanf("%d %d",&a,&b);
    	sum=a+b;
    	if(sum<0) {
    		printf("-");
    		sum=-sum;
    	}
    	if(sum>=moda) {
    		printf("%d,%03d,%03d
    ", sum/moda,(sum%moda)/modb,sum%modb);
    	} else if(sum>=modb) {
    		printf("%d,%03d
    ", sum/modb,sum%modb);
    	} else {
    		printf("%d
    ", sum);
    	}
    	return 0;
    }


  • 相关阅读:
    制作一个螺旋矩阵
    通过C++修改系统时间代码
    绝对值最小
    compile cmdow
    2017-10-04清北模拟赛
    2017-10-03清北模拟赛
    2017-10-01清北模拟赛
    HTML容易遗忘内容(二)
    SSM框架关于后台返回JSON数据中显示很多不需要的字段为NULL
    关于Unix时间戳转北京时间的问题
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7775970.html
Copyright © 2011-2022 走看看