fjnu 1178 Base 9 Calculator

本文介绍了一种解决九进制数相加的问题,输入为两个九进制数,输出为它们相加后的九进制结果。通过将九进制转换为十进制进行运算,并最终返回九进制形式的答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Normally, you use base 10 to do arithmetic. In computer science, you also deal with binary (base 2), octal (base 8), and hex (base 16). In this problem, we'll worry about base 9, which uses the digits 0..8.

Input

each line of input will contain two numbers separated by a space. Each number is in base 9. If converted to base 10, the numbers would be greater than 0, and smaller than 65000.

Output

For each input, your program should add the two numbers together and print the result (in base 9).

Sample Input

148 765
111 888
8734 8345

Sample Output

1024
1110
18180
KEY:这题就是求和,只是他是9进制的就是了
Source:

#include
<iostream>
using namespace std;

void Add(char str1[],char str2[])
{
    
int la=strlen(str1),lb=strlen(str2);
    
int a[15],b[15];
    
int i,j;
    
for(i=0;i<=10;i++)
        a[i]
=b[i]=0;
    
for(i=0;i<la;i++)
        a[i
+1]=str1[i]-'0';
    
for(i=0;i<lb;i++)
        b[i
+1]=str2[i]-'0';
    
if(la>=lb)
    
{
        i
=la;
        j
=lb;
        
while(j>0)
        
{
            a[i
--]+=b[j--];
        }

        
for(i=la;i>0;i--)
        
{
            
int t;
            t
=a[i];
            a[i
-1]+=t/9;
            a[i]
=t%9;
        }

        
if(a[0]==0) i=1;
        
else i=0;
        
for( ;i<=la;i++)
            cout
<<a[i];
        cout
<<endl;
    }

    
else
    
{
        i
=la;
        j
=lb;
        
while(i>0)
        
{
            b[j
--]+=a[i--];
        }

        
for(j=lb;j>0;j--)
        
{
            
int t;
            t
=b[j];
            b[j
-1]+=t/9;
            b[j]
=t%9;
        }

        
if(b[0]==0) j=1;
        
else j=0;
        
for( ;j<=lb;j++)
            cout
<<b[j];
        cout
<<endl;
    }

}


int main()
{
//    freopen("fjnu_1178.in","r",stdin);
    char str1[10],str2[10];
    
while(cin>>str1>>str2)
    
{
        Add(str1,str2);
    }

    
return 0;
}







 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值