标签:
最近突然想往算法方向走走,做了做航电acm的几道题
二话不说,开始
航电acm 1002 题主要是处理长数据的问题,算法原理比较简单,就是用字符数组代替int,因为int太短需要处理的数据较长
下面是问题描述:
下面列出我的代码 参考http://blog.csdn.net/odaynot/article/details/8049632
#include<stdio.h> #include<string.h> int toInt(char c){ return c-‘0‘; } int main(){ int i, n,j=1,al,bl,ml,t; char a[1000], b[1000];//存储输入的两个数 int count[1001]; scanf("%d",&n); while(n--){ if(j!=1)printf("\n"); scanf("%s",a); al=strlen(a);//返回字符串结束符之前的字符个数 scanf("%s",b); bl=strlen(b); ml=(al>bl)?al:bl;//获取较长的字符长度 t=ml; for(i=0;i<=ml;i++)count[i]=0;//初始化数组 for(ml;al>0&&bl>0;ml--){ count[ml]+=toInt(a[--al])+toInt(b[--bl]); if(count[ml]/10){ //处理进位问题 count[ml-1]++; count[ml]=count[ml]%10; } } while(al>0){ count[ml--]+=toInt(a[--al]); if(count[ml+1]/10){ count[ml]++; count[ml+1]%=10; } } while(bl>0){ count[ml--]+=toInt(b[--bl]); if(count[ml+1]/10){ count[ml]++; count[ml+1]%=10; } } printf("Case %d:\n%s + %s =",j++,a,b); for(i=0;i<=t;i++){ if(i==0&&count[i]==0){ i++; } printf("%d",count[i]); } printf("\n"); } return 0; }
欢迎批评,疑问请留言
标签:
原文地址:http://www.cnblogs.com/Alan-Wei/p/5180192.html