由a+b问题想到的

来源:互联网 发布:天猫盒子无法连接网络 编辑:程序博客网 时间:2024/05/18 12:02
 

打开各种online judge,第一道题目肯定不是让你输出一个hello world,而是求a+b的和。往往该题都是很简单的,两个int类型相加即可,POJ上甚至给出了此题各种版本的代码,正和学习任何一种语言首先打印出hello world一样,这是一道经典的入门题,我想很少有OJ上把这第一道题就弄成两个高精度整数相加的,编程啦是个例外。

很显然,这道题可以有很多变化,除了最简单的只有两个数以外,也可以用while(cin>>a>>b)或者
while(scanf("%d %d",&a,&b)!=EOF)来进行多组数据的录入
最简单的代码如下

#include<stdio.h>int main(){int a,b;while(scanf("%d %d",&a,&b)!=EOF){ printf("%d\n",a+b); return 0;}}



如果用户先输入要输入的a+b组数,用一个for循环也很容易,如果用两个特定的数表示输入结束,也只需要加上一个简单的判断即可。
更进一步的情况是,每次输入很多数让你计算出总和,这也无非是进行多次相加而已,当然,可能int类型无法保存了,用long是必要的。

现在更进一步,考虑C++中的流操作,用户不输入组数,也不告诉你一组中有多少个数相加,让你自己判断。例如输入为
1 2 3 4
1 2 3 4 5
很显然用C语言考虑起来就要麻烦点了,按照行进行数据读入,保存成字符串,然后再从字符串中提取出数字并且转换,再相加,遇到不是一位的转换还需要用个循环,比较费时。如果用C++中的流操作,那就省事了,看的出来C++的强大,不过由于学识有限,我可能不知道C也可以实现同样的流操作。代码如下:

#include<iostream>#include<sstream>#include<string>using namespace std;int main(){    int val,sum;    string str;    while(getline(cin, str))    {        sum=0;  stringstream is(str);  while (is >> val)         sum += val;         cout<<sum<<endl;    }    return 0;}


上面的问题再BT一下,可以让两个数之间不止一个空格,甚至在每一行输入结束之前也输入N多无效的空格,用C++来处理代码不变,但是用C语言来处理时如果像上题一样按照间隔只为一个空格的思想,那可能就要改些地方了。所以流操作还是挺有用的。

最后给出高精度a+b的代码,用数组实现的,思想还是比较简单的,但是比起最简单两三行的a+b问题还是大巫见小巫了。

#include <stdio.h>#include <string.h>int main(){    void add(char a[],char b[],char result[]);    void plus(char a[],char b[],char result[]);    char a[1000]={0},b[1000]={0},temp1[1000]={0},temp2[1000]={0};    char result[1000]={0},zancun[1000]={0};    int m,i,n,t;    int number;    scanf("%d",&number);    for(t=1;t<=number;t++)    {    scanf("%s%s",a,b);        if(a[0]=='-'&&b[0]=='-')           {m=0;n=0;           for(i=1;a[i]!='\0';i++)               temp1[m++]=a[i];               temp1[m]='\0';               for(i=1;b[i]!='\0';i++)                 temp2[n++]=b[i];                 temp2[n]='\0';                 add(temp1,temp2,result);                 printf("-%s\n",result);                 goto abc;           }           if(a[0]!='-'&&b[0]!='-')           {                 add(a,b,result);                   printf("%s\n",result);                   goto abc;           }         if(a[0]=='-'&&b[0]!='-')           {   m=0;              n=0;              for(i=1;a[i]!='\0';i++)                 temp2[m++]=a[i];                 temp2[m]='\0';              for(i=0;b[i]!='\0';i++)                 temp1[n++]=b[i];                 temp1[n]='\0';                 if(m<n)                 {plus(temp1,temp2,result); printf("%s\n",result);                 goto abc;                 }                 if(m>n)                 {   strcpy(zancun,temp2);                     strcpy(temp2,temp1);                     strcpy(temp1,zancun);                     plus(temp1,temp2,result);                      printf("-%s\n",result);                      goto abc;                 }                 if(m==n)                 {  if(strcmp(temp1,temp2)==0)                     {printf("0\n");                     goto abc;                     }                     if(strcmp(temp1,temp2)<0)                       {strcpy(zancun,temp2);                     strcpy(temp2,temp1);                     strcpy(temp1,zancun);                           plus(temp1,temp2,result);                        printf("-%s\n",result);                        goto abc;                       }                       if(strcmp(temp1,temp2)>0)                        { plus(temp1,temp2,result);                          printf("%s\n",result);                          goto abc;                        }                 }           }           if(a[0]!='-'&&b[0]=='-')           { m=0;              n=0;              for(i=0;a[i]!='\0';i++)                 temp1[m++]=a[i];                 temp1[m]='\0';              for(i=1;b[i]!='\0';i++)                 temp2[n++]=b[i];                 temp2[n]='\0';                  if(m>n)                 {                     plus(temp1,temp2,result);                 printf("%s\n",result);                 goto abc;                 }                 if(m<n)                 {  strcpy(zancun,temp2);                     strcpy(temp2,temp1);                     strcpy(temp1,zancun);                     plus(temp1,temp2,result);                      printf("-%s\n",result);                      goto abc;                 }                 if(m==n)                 {  if(strcmp(temp1,temp2)==0)                    { printf("0\n");                     goto abc;                    }                     if(strcmp(temp1,temp2)<0)                       {strcpy(zancun,temp2);                     strcpy(temp2,temp1);                     strcpy(temp1,zancun);                           plus(temp1,temp2,result);                        printf("-%s\n",result);                        goto abc;                       }                       if(strcmp(temp1,temp2)>0)                        { plus(temp1,temp2,result);                          printf("%s\n",result);                          goto abc;                        }                 }           }  abc:   memset(a,0,sizeof(a));     memset(b,0,sizeof(b));     memset(result,0,sizeof(result));      memset(temp1,0,sizeof(temp1));     memset(temp2,0,sizeof(temp2));     memset(zancun,0,sizeof(zancun));    }    return 0;}void add(char a[],char b[],char result[]){    int la,lb,lresult;    int s,c,t;    int i;    char temp;    la=strlen(a)-1;    lb=strlen(b)-1;    c=0;    t=0;    while(la >= 0 || lb >= 0){        if(la < 0) s=b[lb--]-48;        else if(lb < 0) s=a[la--]-48;        else s=a[la--]-48+b[lb--]-48;        result[t++]=(s+c)%10+48;        c=(s+c)/10;    }    if(c != 0) {result[t]=c+48;lresult=t;}    else lresult=t-1;    for(i=0;i <= lresult/2;i++){        temp=result[i];        result[i]=result[lresult-i];        result[lresult-i]=temp;    }}void plus(char temp1[],char temp2[],char result[]){ int la,lb,lresult;     int s,c,t=0;     int i,j;     char temp[1000]={0};     char temp3[1000]={0},temp4[1000];     int length;    memset(temp4,'0',sizeof(temp4));     la=strlen(temp1);/*����大��������*/     lb=strlen(temp2);     c=0;s=0;     for(i=la-1;i>=0;i--)       temp3[c++]=temp1[i];     for(i=lb-1;i>=0;i--)        temp4[s++]=temp2[i];     temp[la]='0';    for(i=0;i<la;i++)      {          if(temp3[i]<temp4[i])   {temp[i]=temp3[i]+10-temp4[i]+'0';            temp3[i+1]--;           }         if(temp3[i]==temp4[i])            temp[i]='0';          if(temp3[i]>temp4[i])            temp[i]=temp3[i]-temp4[i]+'0';      }      for(j=i;j>0;j--)      {  if(temp[j]=='0')     continue;  else     break;      }      for(i=j;i>=0;i--)        result[t++]=temp[i];        result[t]='\0';}


 

 


 

原创粉丝点击