zoukankan      html  css  js  c++  java
  • PAT 甲级 1001 A+B Format

    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

    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <algorithm>
    #include <math.h>
    #include <stdio.h>
    #include <string>
    #include <vector>
    #include <strstream>
    
    using namespace std;
    int a,b;
    int main()
    {
    	while(scanf("%d%d",&a,&b)!=EOF)
    	{
    		int num=a+b;
    		strstream ss;
    		string s;
    		ss << num;
    		ss >> s;
    		int len=s.length();
    		if(num<0)
    			num=(len-1)%3;
    		else
    			num=len%3;
    		for(int i=0;i<num;i++)
    		{
    			cout<<s[i];
    			if(s[i]=='-') num++;
    			if(i==num-1&&len>3)
    				cout<<",";
    		}
    		int cnt=0;
    		for(int i=num;i<len;i++)
    		{
    			cout<<s[i];
    			if(s[i]<='9'&&s[i]>='0')
    				cnt++;
    			if(cnt==3)
    			{
    				if(i!=len-1)
    					cout<<",";
    				cnt=0;
    			}
    		}
    		cout<<endl;
    	}
    }


  • 相关阅读:
    CF1313A Fast Food Restaurant
    模板: zkw线段树
    从5个经典工作开始看语义SLAM
    LeetCode题号[200,299]刷题总结
    2020春招实习总结
    LeetCode题号[100,199]刷题总结
    LeetCode题号[1,99]刷题总结
    HashMap源码详解
    动态规划——楼层扔鸡蛋问题
    图论——迪杰斯特拉算法和最小生成树
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228628.html
Copyright © 2011-2022 走看看