寒假作业第二篇随笔(A+B)

Github链接:https://github.com/heihuifei/object-oriented
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

首先说下这个题目写的时候出现了让我很头大的事情:又发生了看错题目意思的问题,所以我才好几天才写这篇随笔。

题目大致意思:算A+B和的值,然后用一种标准的形式(这里我不得不说太坑了)输出结果。标准形式:一个数从右边开始每三位一个逗号,除非少于4位数。

大致思路及过程:其实如果知道了题目的意思的话,写起来还算简单的,用和数分别除以1000,1000000的余数得到的不到三位的补0前面满三位这个思路就可以写出来。至于说解此题的过程是经历了一段奇葩之路,开始一直以为是和数最后从左往右每三位一个逗号,然后就会发现结果明明一直是对的,可是就是一直有bug。最后熬了几天之后,实在不相信自己哪里还错了,咨询了我的班导,她和我说出了此题的正确意思。之后我解题的速度就比较正常了。

#include<stdio.h>
int main()
{
  int b,c,temp,rem,cmo=1,contral=0;
  int a[10],i,n;
  scanf("%d %d",&b,&c);
  temp=b+c;
  if(temp<0)
  {
    contral=1;
    temp=-temp;
  }
  for(i=0;temp!=0;i++)
  {
    rem=temp%10;
    temp/=10;
    a[i]=rem;
    n=i;
  }
  if(contral==1)
      printf("-");
  for(i=n;i>=0;i--,cmo++)
  {
    printf("%d",a[i]);
    if(cmo%3==0)
      printf(",");
  }
  return 0;
}

这两张是在最开始理解错了题目意思的情况下(也就是输出从左往右逗号),最后发现了输出结果会在最后还输出一个逗号。

#include<stdio.h>
int main()
{
  int b,c,temp,rem,cmo=1,contral=0;
  int a[10],i,n;
  scanf("%d %d",&b,&c);
  temp=b+c;
  if(temp<0)
  {
    contral=1;
    temp=-temp;
  }
  for(i=0;temp!=0;i++)
  {
    rem=temp%10;
    temp/=10;
    a[i]=rem;
    n=i;
  }
  if(contral==1)
      printf("-");
  for(i=n;i>=0;i--,cmo++)
  {
    printf("%d",a[i]);
    if(cmo%3==0&&cmo<n-1)
      printf(",");
  }
  return 0;
}


经过修改后提交发现还是有错,然后我自己又继续寻找bug,发现如果和数为0的时候,会出现没有输出的情况。

int main()
{
  int b,c,temp,rem,cmo=1,cmonum,contral=0;
  int a[20],i,n;
  scanf("%d %d",&b,&c);
  temp=b+c;
  if(temp<0)
  {
    contral=1;
    temp=-temp;
  }
  if(temp==0)
    printf("0");
  for(i=0;temp!=0;i++)
  {
    rem=temp%10;
    temp/=10;
    a[i]=rem;
    n=i;
  }
  if(contral==1)
      printf("-");
  cmonum=n/3;
  for(i=n;i>=0;i--,cmo++)
  {
    printf("%d",a[i]);
    if(cmo%3==0&&cmonum!=0)
    {
      printf(",");
      cmonum--;
    }
  }
  printf("
");
  return 0;
}

然后我把所有的都改好了之后,可是提交上去还是只有13分(因为我的题目意思理解错了),经过几天的挣扎后,问过班导之后才发现是这个问题。

#include<stdio.h>
#include<math.h>
int main()
{
  int a,b,c,p,q,t,C;
  scanf("%d %d",&a,&b);
  c=a+b;
  C=abs(c);
  if(C<1000&&C>=0)
    printf("%d",c);
  else if(C>=1000&&C<1000000)
  {
    p=c%1000;
    q=c/1000;
    printf("%d,%03d",q,p);
  }
  else if(C>=1000000&&C<=2000000)
  {
    p=c%1000;
    q=(c/1000)%1000;
    t=c/1000000;
    printf("%d,%03d,%03d",t,q,p);
  }
  printf("
");
  return 0;
}

之后知道题目正确的意思之后就我打了一遍,发现第一个测试点没过,经过几轮的排查之后,知道了测试数据没有过,是我的取余数的时候负号算错了,没有加绝对值。测试结果如下:

还有这个是不知道什么时候在哪个代码里发现的bug:

之后经过修改后终于修成正果:

#include<stdio.h>
#include<math.h>
int main()
{
  int a,b,c,p,q,t,C;
  scanf("%d %d",&a,&b);
  c=a+b;
  C=abs(c);
  if(C<1000&&C>=0)
    printf("%d",c);
  else if(C>=1000&&C<1000000)
  {
    p=abs(c%1000);
    q=c/1000;
    printf("%d,%03d",q,p);
  }
  else if(C>=1000000&&C<=2000000)
  {
    p=abs(c%1000);
    q=abs(c/1000)%1000;
    t=c/1000000;
    printf("%d,%03d,%03d",t,q,p);
  }
  return 0;
}


总的提交列表(如下)一看,发现过程真是艰难啊,一开始编译器测试显示编译错误,之后的波折上面也已经做出陈述。

总结:虽然这次作业比较简单,但是因为各种问题所以收获还是很大的,也说明了在经过期末考试之后我的编程题做得是很少的。另外还是要对英文题多加练习,多在老师学提供的各种平台上进行练习。

原文地址:https://www.cnblogs.com/heihuifei/p/6341294.html