首页 技术 正文
技术 2022年11月17日
0 收藏 317 点赞 4,905 浏览 2334 个字

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:5346

解决:1464

题目描述:
    读入两个不超过25位的火星正整数A和B,计算A+B。需要注意的是:在火星上,整数不是单一进制的,第n位的进制就是第n个素数。例如:地球上的10进 制数2,在火星上记为“1,0”,因为火星个位数是2进制的;地球上的10进制数38,在火星上记为“1,1,1,0”,因为火星个位数是2进制的,十位 数是3进制的,百位数是5进制的,千位数是7进制的……
输入:
    测试输入包含若干测试用例,每个测试用例占一行,包含两个火星正整数A和B,火星整数的相邻两位数用逗号分隔,A和B之间有一个空格间隔。当A或B为0时输入结束,相应的结果不要输出。
输出:
    对每个测试用例输出1行,即火星表示法的A+B的值。
样例输入:
1,0 2,1
4,2,0 1,2,0
1 10,6,4,2,1
0 0
样例输出:
1,0,1
1,1,1,0
1,0,0,0,0,0

一开始看到题目,其实我是蒙 bility的

后来看到大神的讲解之后才明白是个怎么的解法。

其实和大整数的加法差不多,只是进制是不停在变的。最开始是用的字符串做的,前面两组数据过了,但是,后面那组出现了问题,没有考虑到两位数字的情况。

但是如果要考虑两个数字,那样用字符串做便显得很麻烦,于是就用数组做了,真的简单很多。。

代码如下:

//Asimple
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <string>
#include <queue>
#define INF 100000
using namespace std;
const int maxn = ;
typedef long long ll ;
int n, m;
int p[]={,,,,,,,,,,,,,,,,,,,,,,,,,};
int a[],b[],sum[];int main(){
int m,n,i,j,k,add,x,y,max;
char c;
while( true ){
for(i=;i<=;i++){
a[i]=;
b[i]=;
sum[i]=;
}
for(i=;i<=;i++){
scanf("%d",&a[i]);
scanf("%c",&c);
if(c==' ') break;
}m=i;
for(i=;i<=;i++){
scanf("%d",&b[i]);
scanf("%c",&c);
if(c=='\n') break;
}n=i;
if(a[]==&&b[]==)break;
k=m>n?m:n;
for(i=,add=,max=k;i<=max;i++){
if(m>=)x=a[m--];
else x=;
if(n>=)y=b[n--];
else y=;
sum[k--]=(x+y+add)%p[i];
add=(x+y+add)/p[i];
}
sum[]=add;
if(sum[]!=) printf("%d,",sum[]);
for(i=;i<max;i++) printf("%d,",sum[i]);
printf("%d\n",sum[max]);
}
return ;
}

贴上我用字符串做的代码,想改的可以尝试下:

//Asimple
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <string>
#include <queue>
#define INF 100000
using namespace std;
const int maxn = ;
typedef long long ll ;
int n, m;
char s1[maxn], s2[maxn];
int a[] = {,,,,,,,,,
,,,,,,,,
,,,,,,,};
int sum[maxn];int main(){
while( ~ scanf("%s %s",s1,s2) ){
int len1 = strlen(s1);
int len2 = strlen(s2);
if( len1 == && s1[] == '' && len2 == && s2[] == '' ) break;
n = ;
int c = ;
int i = len1-;
int j = len2-;
for(; i>=&&j>=; i--,j--){
if( isdigit(s1[i]) && isdigit(s2[j]) ){//改这里 :改成能够判断多位数字
sum[n] = (s1[i]-'') + (s2[j]-'') + c;
if( (s1[i]-'') + (s2[j]-'') + c >= a[n] ){
c = ;
sum[n] -= a[n];
} else {
c = ;
}
n ++ ;
}
}
for(; i>=; i--){
if( isdigit(s1[i]) ){//改这里 :改成能够判断多位数字
sum[n] = s1[i] - '' + c;
if( s1[i] - '' + c >= a[n] ){
c = ;
sum[n] -= a[n];
} else {
c = ;
}
n ++ ;
}
}
for(; j>=; j--){
if( isdigit(s2[j]) ){//改这里 :改成能够判断多位数字
sum[n] = s2[j] - '' + c;if( s2[j] - '' + c >= a[n] ){
c = ;
sum[n] -= a[n];
} else {
c = ;
}
n ++ ;
}
}
if( c == ){
sum[n] = ;
n ++ ;
}
for(int k=n-; k>=; k--){
printf(k==?"%d\n":"%d,",sum[k]);
}
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903