王道1026 又一版 A+B
来源:互联网 发布:知学学院简介 编辑:程序博客网 时间:2024/06/05 19:33
输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数。
题目描述:
- 输入:
- 输入格式:测试输入包含若干测试用例。每个测试用例占一行,给出m和A,B的值。
当m为0时输入结束。
- 输出:
- 输出格式:每个测试用例的输出占一行,输出A+B的m进制数。
- 样例输入:
8 1300 482 1 70
- 样例输出:
2504
1000
思路:求和,用和依次除以m然后倒叙输出余数,知道和小于m。倒叙嘛可以用栈来存储余数,也可以用一个数组存完倒叙输出。
值得注意的是要考虑和是零的情况,还有要注意ab的大小,用long long类型来存储。
代码(数组存储)
#include <iostream>#include<stdio.h>using namespace std;int tr[100],idx;void exchange(int m,long long n){ idx=0; while(n!=0){ tr[idx++]=n%m; n=n/m; } int i; for(i=idx-1;i>=0;i--){ printf("%d",tr[i]); } printf("\n");}int main(){ int m; long long sum,a,b; while(scanf("%d",&m)&&m!=0){ scanf("%lld%lld",&a,&b); sum=a+b; if(sum==0)printf("0\n"); else exchange(m,sum); } return 0;}
代码(stack)#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <stack>using namespace std;stack<int>s;void itoanthor(long long n,int m){ while(n){ s.push(n%m); n/=m; }}int main(){ int m; long long a,b,re; while(scanf("%d",&m)&&m!=0){ scanf("%lld%lld",&a,&b); re=a+b; while(!s.empty())s.pop(); if(re==0) s.push(0); else itoanthor(re,m); while(!s.empty()){ printf("%d",s.top()); s.pop(); } printf("\n"); } return 0;}
0 0
- 王道1026 又一版 A+B
- 又一版 A+B
- 又一版 A+B
- 又一版 A+B
- 又一版A+B
- 又一版A+B
- 又一版 A+B
- 题目1026:又一版 A+B
- 题目1026:又一版 A+B
- 题目1026:又一版 A+B
- 题目1026:又一版 A+B
- 43-题目1026:又一版 A+B
- 题目1026:又一版 A+B 学习
- 题目1026:又一版 A+B
- 题目1026:又一版 A+B
- 题目1026:又一版 A+B
- 九度1026:又一版 A+B
- 题目1026:又一版 A+B
- MapReduce的模式、算法和用例
- 安卓 the zipalign tool was not found in the sdk
- Sort Colors
- 并查集最简单应用_不要抽烟
- 不写js仅用css实现手风琴和选项卡效果
- 王道1026 又一版 A+B
- highcharts 图表操作实例
- 22、复合方法
- 仿网易彩票代码实现
- Linux之Makefile(变量)
- VS系列远程调试
- 忘记虚拟机密码
- 深度测试(消隐)demo
- 音视频二次开发 IP组播技术