PAT1001 A+B Format

在一行中输入整数 a 和 b,用空格隔开( -1000000 <= a, b <= 1000000 )

输出a+b的值,用千分位格式输出


Sample Input

-1000000 9


Sample Output

-999,991

 

python版本代码如下

 

 

 

import re
import sys

def fun(num):
    if num >= 1000:
        fun(num/1000)
        sys.stdout.write(",%03d" % (num%1000))
    else:
        sys.stdout.write(num)

def test():
    str = raw_input() 
    s =  re.split(" ",str) 
    sum = int(s[0]) + int(s[1])
    if sum < 0:
        sys.stdout.write("-")
        sum = sum * (-1)
    fun(sum)
   
if __name__ == '__main__':
    test()
 

 

 

import re
import sys
str = raw_input() 
s =  re.split(" ",str) 
sum = int(s[0]) + int(s[1])
if sum < 0:
    sys.stdout.write("-")
    sum = sum * (-1)
str = repr(sum)
mod = len(str)%3
num = len(str)/3

if mod > 0:
    if num == 0:
        sys.stdout.write(str[0:mod])
        sys.exit()
    else:
        sys.stdout.write(str[0:mod])
        sys.stdout.write(",")

for i in range(num -1):
    sys.stdout.write(str[mod + 3*i: mod + 3*i + 3])
    sys.stdout.write(",")
sys.stdout.write(str[mod + 3*(num-1): mod + 3*num])
 

 

C++版本代码如下

#include 
#include 
#include 
using namespace std;

int main()
{
	int a, b;
	cin>>a>>b;
	int sum = a + b;
	if (sum < 0)
	{
		cout<<"-";
	}
	sum = abs(sum);
	stringstream strStream;
	strStream< 0)
	{
		if (num == 0)
		{
			cout<

你可能感兴趣的:(PAT,PAT)