PAT甲级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 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

题目链接:点击打开链接

我先不翻译了,先把一些生词写出来:

Calculate  V.计算;

Standard n.标准,水平;尺度;榜样;

Format n.安排,布局,设计,样式v.(尤作计算机用语)把…编排成格式,格式化;

Digits  n.数字;位;

separated into 分为;

groups of  成群的;

contains  V.包含;容纳;包含某字符串n.包含;

commas  逗号 comma的复数;

有些不会的单词,大概就是这些了。

接下来,说一下题意,计算a+b的值并输出其标准形式,标准格式也就是从左往右每三位分为一组,每一组之间用逗号隔开,除非他们的和位数小于四。题目很简单,但是,我第一次真的没读出来,即使单词都会了,突然有点沮丧。首先,那个每三位分为一组,我一直以为是分为三组,还有一点我现在都不理解,为什么是从右到左,英文里好像没这个吧,但是看别人的题意分析就是这样,我就方了。我第一次就是从左往右,只能得14分吧,后来找别人的的代码,才写对。这个地方,如果考试也这样,我真的未必能明白过来。

只要能理解题意,这个题就不难了。我的方法是,把这个数字转化为字符字串,然后输出就行了。

附代码:

#include<stdio.h>
int main()
{
	int a, b;
	scanf("%d %d",&a,&b);
	int sum=a+b;
	if(sum<0)
	{
		sum=-sum;
		printf("-");
	}
	if(sum==0)
	printf("0");
	char f[100];
	int i=0;
	while(sum>0)
	{
		f[i++]=sum%10+'0';
		sum=sum/10;
	}
	int k=0;
	if(i<4)
	{
		for(int j=i-1;j>=0;j--)printf("%c",f[j]);
	}
	else 
	for(int j=i-1;j>=0;j--)
	{
		printf("%c",f[j]);
		if((i-k-1)%3==0&&j!=0)
		printf(",");
		k++;
	}
	
	return 0;
 } 

睡了睡了,不能猝死。



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值