[HOJ 10144] Base 9 Calculator

Base 9 Calculator
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 1035, Accepted users: 898
Problem 10144 : No special judgement
Problem 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(include 0), and smaller than 65000(include 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
Problem Source
UD Contest

AC:

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>   
#include <cstring>

struct N
{
    char a[20]; int s;
} n1, n2, r;

int max(int a, int b) { return a > b ? a : b; }

int read(N& n)
{
    memset(n.a, 0, sizeof(n.a));
    char ch = getchar(); int i = 0;
    while (ch < '0' || ch > '9') { if (ch == -1) return -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { n.a[i++] = ch - '0'; n.s = i; ch = getchar(); }
    return 1;
}

void add(N& n1, N& n2)
{
    int a = 0, o = 0, s = max(n1.s, n2.s);
    r.s = s;
    for (int i = n1.s - 1, j = n2.s - 1; i >= 0 || j >= 0;)
    {
        if (i >= 0) a += n1.a[i--];
        if (j >= 0) a += n2.a[j--];
        o = a % 9;
        a /= 9;
        r.a[--s] = o;
    }
    if (a) printf("%d", a);
    for (int i = 0; i < r.s; ++i) printf("%d", r.a[i]);
    printf("\n");
}

int main()
{
    while (~read(n1) && ~read(n2)) add(n1, n2);
    return 0;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值