1002 ( A + B Problem II )

  1 #include <iostream> 
  2 #include <string> 
  3 using namespace std; 
  4 
  5 void Add(string a,string b,char sum[],int& count) 
  6 {//大数加法
  7     int len1 = a.length();//数a的长度
  8     int len2 = b.length();//数b的长度
  9     int i = len1-1,j = len2-1,temp = 0,carryIn = 0;//初始进位为
 10     count = 0; 
 11     //从最后一位开始做加法
 12     while(i>=0&&j>=0) 
 13     { 
 14         temp = a[i]-'0'+b[j]-'0'+carryIn;//计算当前位
 15         sum[count++] = temp%10+'0'; 
 16         carryIn = temp/10;//计算进位
 17         --i; 
 18         --j; 
 19     } 
 20     //第一个数还有剩余
 21     if(i>=0) 
 22     { 
 23         //利用进位继续做
 24         while(i>=0) 
 25         { 
 26             temp = a[i]-'0'+carryIn; 
 27             sum[count++] = temp%10+'0'; 
 28             carryIn = temp/10; 
 29             --i; 
 30         } 
 31     } 
 32     //第二个数还有剩余
 33     if(j>=0) 
 34     { 
 35         while(j>=0) 
 36         { 
 37             temp = b[j]-'0'+carryIn; 
 38             sum[count++] = temp%10+'0'; 
 39             carryIn = temp/10; 
 40             --j; 
 41         } 
 42     } 
 43     //最高位特殊考虑下
 44     if(carryIn>0) 
 45     { 
 46         sum[count++] = '1'; 
 47     } 
 48 } 
 49 
 50 void reversePrint(char arr[],int len) 
 51 {//逆向输出
 52     for(int i=len-1;i>=0;--i) 
 53     {
 54         cout<<arr[i]; 
 55     }
 56     cout<<endl; 
 57 } 
 58 
 59 int main() 
 60 { 
 61     string a,b; 
 62     char sum[2000];//
 63     memset(sum,'0',2000); 
 64     int nCount = 0; 
 65     int caseNum,curCase=0; 
 66     cin>>caseNum; 
 67     do 
 68     { 
 69         curCase++; 
 70         cin>>a>>b; 
 71         Add(a,b,sum,nCount); 
 72         cout<<"Case "<<curCase<<":"<<endl; 
 73         cout<<a<<" + "<<b<<" = "; 
 74         reversePrint(sum,nCount); 
 75         if(curCase<caseNum) 
 76         {
 77             cout<<endl; 
 78         }
 79     }while(curCase<caseNum); 
 80     return 0; 
 81 } #include <iostream> 
 82 #include <string> 
 83 using namespace std; 
 84 
 85 void Add(string a,string b,char sum[],int& count) 
 86 {//大数加法
 87     int len1 = a.length();//数a的长度
 88     int len2 = b.length();//数b的长度
 89     int i = len1-1,j = len2-1,temp = 0,carryIn = 0;//初始进位为
 90     count = 0; 
 91     //从最后一位开始做加法
 92     while(i>=0&&j>=0) 
 93     { 
 94         temp = a[i]-'0'+b[j]-'0'+carryIn;//计算当前位
 95         sum[count++] = temp%10+'0'; 
 96         carryIn = temp/10;//计算进位
 97         --i; 
 98         --j; 
 99     } 
100     //第一个数还有剩余
101     if(i>=0) 
102     { 
103         //利用进位继续做
104         while(i>=0) 
105         { 
106             temp = a[i]-'0'+carryIn; 
107             sum[count++] = temp%10+'0'; 
108             carryIn = temp/10; 
109             --i; 
110         } 
111     } 
112     //第二个数还有剩余
113     if(j>=0) 
114     { 
115         while(j>=0) 
116         { 
117             temp = b[j]-'0'+carryIn; 
118             sum[count++] = temp%10+'0'; 
119             carryIn = temp/10; 
120             --j; 
121         } 
122     } 
123     //最高位特殊考虑下
124     if(carryIn>0) 
125     { 
126         sum[count++] = '1'; 
127     } 
128 } 
129 
130 void reversePrint(char arr[],int len) 
131 {//逆向输出
132     for(int i=len-1;i>=0;--i) 
133     {
134         cout<<arr[i]; 
135     }
136     cout<<endl; 
137 } 
138 
139 int main() 
140 { 
141     string a,b; 
142     char sum[2000];//
143     memset(sum,'0',2000); 
144     int nCount = 0; 
145     int caseNum,curCase=0; 
146     cin>>caseNum; 
147     do 
148     { 
149         curCase++; 
150         cin>>a>>b; 
151         Add(a,b,sum,nCount); 
152         cout<<"Case "<<curCase<<":"<<endl; 
153         cout<<a<<" + "<<b<<" = "; 
154         reversePrint(sum,nCount); 
155         if(curCase<caseNum) 
156         {
157             cout<<endl; 
158         }
159     }while(curCase<caseNum); 
160     return 0; 
161 } 

看来做OJ还是不用Java了

少了指针有时候不方便

技术不够

转载于:https://www.cnblogs.com/manhua/archive/2013/05/05/3060592.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值