hdu2100(大数加)

http://acm.hdu.edu.cn/showproblem.php?pid=2100

一个26进制的大数加法,

ContractedBlock.gif ExpandedBlockStart.gif View Code

#include < iostream >
#include
< cmath >
#define M 201
using namespace std;
char ch1[M],ch2[M];
int a[M] ,b[M] ;
int c[M];
int i,j,k;
int main()
{

while (cin >> ch1 >> ch2)
{
int L1 = strlen(ch1);
int L2 = strlen(ch2);
int flag = 0 ;

/* *********************
// 将字符型转换为整型
********************
*/
for (i = 0 ;i < L1;i ++ )
a[i]
= ch1[i] - ' A ' ;
for (i = 0 ;i < L2;i ++ )
b[i]
= ch2[i] - ' A ' ;


k
= 0 ;
if (L1 > L2)
{
for ( i = L1 - 1 ,j = L2 - 1 ; i >= L1 - L2,j >= 0 ; i -- , j -- )
{
c[k]
= (a[i] + b[j] + flag) % 26 ;

flag
= (a[i] + b[j] + flag) / 26 ;
k
++ ;
}
for (i = L1 - L2 - 1 ;i >= 0 ; i -- )
{
c[k]
= (a[i] + flag) % 26 ;
flag
= (a[i] + flag) / 26 ;
k
++ ;

}
while (flag)
{
c[k
++ ] = flag % 26 ;
flag
/= 26 ;
}
}
else if (L1 == L2)
{
for ( i = L1 - 1 , j = L2 - 1 ; i >= 0 , j >= 0 ; i -- , j -- )
{
c[k
++ ] = (a[i] + b[j] + flag) % 26 ;
flag
= (a[i] + b[j] + flag) / 26 ;
}
while (flag)
{
c[k
++ ] = flag % 26 ;
flag
/= 26 ;
}
}
else
{
for ( i = L2 - 1 , j = L1 - 1 ; i >= L2 - L1 , j >= 0 ; i -- , j -- )
{
c[k]
= (b[i] + a[j] + flag) % 26 ;
flag
= (b[i] + a[j] + flag) / 26 ;
k
++ ;
}
for (i = L2 - L1 - 1 ;i >= 0 ;i -- )
{
c[k]
= (b[i] + flag) % 26 ;
flag
= (b[i] + flag) / 26 ;
k
++ ;
}
while (flag)
{
c[k
++ ] = flag % 26 ;
flag
/= 26 ;
}
}
int mark = k - 1 ;
int sign = 0 ;
for (i = k - 1 ;i >= 0 ;i -- )
{
if (c[i]) { mark = i ; break ;}
if (c[i] == 0 ) sign ++ ;
}
if (sign == k) cout << " A " << endl;
else
{
for (i = mark;i >= 0 ;i -- )
printf(
" %c " ,c[i] + ' A ' );
printf(
" \n " );
}
}
return 0 ;
}
写的有点多了,以后优化。。。。。

转载于:https://www.cnblogs.com/FCWORLD/archive/2011/04/25/2028508.html

你可能感兴趣的:(hdu2100(大数加))