题目1026:又一版 A+B

题目1026:又一版 A+B

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:18122

解决:4902

题目描述:

输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数。

输入:
输入格式:测试输入包含若干测试用例。每个测试用例占一行,给出m和A,B的值。
当m为0时输入结束。
输出:
输出格式:每个测试用例的输出占一行,输出A+B的m进制数。
样例输入:
8 1300 48
2 1 7
0
样例输出:
2504
1000
来源:
2008年浙江大学计算机及软件工程研究生机试真题

笔记:每次都会因为一些细节出错,这个题中,否则如果两个数都为0的话,也就会使本次无输出。这也就说明,要针对特例情况充分考虑到,否则因为一个特例会导致全盘皆输。
#include <stdio.h>
#include <iostream>
#include <stack>
#include <string.h>
#include <queue>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <string>
using namespace std;
typedef long long LL;
#define MAX 1000001
void fun(LL a, int m)
{
    if(a == 0){
        cout << "0" << endl;
        return ;
    }
    stack<int> s1;
    while(a != 0){
        s1.push(a % m);
        a /= m;
    }
    while(!s1.empty()){
        cout << s1.top();
        s1.pop();
    }
    cout << endl;
}
 
int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    LL m, a, b;
    while(scanf("%ld", &m) != EOF){
        if(m == 0){
            break;
        }
        scanf(" %ld %ld", &a, &b);
        LL tem = a + b;
        fun(tem, m);
        //cout << fun(a, m) << endl;
    }
    return 0;
 
}
 
 
 
/**************************************************************
    Problem: 1026
    User: Crazy_man
    Language: C++
    Result: Accepted
    Time:20 ms
    Memory:24956 kb
****************************************************************/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值