时间限制: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 ;
}

每日一九度之题目1016:火星A+B的更多相关文章

  1. 九度oj 题目1016:火星A+B

    题目描述:     读入两个不超过25位的火星正整数A和B,计算A+B.需要注意的是:在火星上,整数不是单一进制的,第n位的进制就是第n个素数.例如:地球上的10进制数2,在火星上记为“1,0”,因为 ...

  2. 每日一九度之 题目1076:N的阶乘

    时间限制:3 秒 内存限制:128 兆 特殊判题:否 提交:7601 解决:2749 题目描述: 输入一个正整数N,输出N的阶乘. 输入: 正整数N(0<=N<=1000) 输出: 输入可 ...

  3. 每日一九度之 题目1043:Day of Week

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7336 解决:2563 题目描述: We now use the Gregorian style of dating in Russia. ...

  4. 每日一九度之 题目1042:Coincidence

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3007 解决:1638 题目描述: Find a longest common subsequence of two strings. 输入 ...

  5. 每日一九度之 题目1041:Simple Sorting

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4883 解决:1860 题目描述: You are given an unsorted array of integer numbers. ...

  6. 每日一九度之 题目1040:Prime Number

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6732 解决:2738 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...

  7. 每日一九度之 题目1038:Sum of Factorials

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2109 解决:901 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...

  8. 每日一九度之 题目1039:Zero-complexity Transposition

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3372 解决:1392 题目描述: You are given a sequence of integer numbers. Zero-co ...

  9. 每日一九度之 题目1033:继续xxx定律

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5502 解决:1351 题目描述:     当n为3时,我们在验证xxx定律的过程中会得到一个序列,3,5,8,4,2,1,将3称为关键数, ...

随机推荐

  1. iOS 设置导航栏的颜色和导航栏上文字的颜色

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  2. RequireJS初探

    什么是RequireJS? /* --- RequireJS 是一个JavaScript模块加载器.它非常适合在浏览器中使用, 它非常适合在浏览器中使用,但它也可以用在其他脚本环境, 就像 Rhino ...

  3. my.cnf详解

    [client] port = 3306 socket = /tmp/mysql.sock [mysqld] port = 3306 socket = /tmp/mysql.sock basedir ...

  4. UILabel详解

    // ----------------------UILabel--------------------------- UILabel *label = [[UILabel alloc] initWi ...

  5. Java基础之泛型——使用泛型链表类型(TryGenericLinkedList)

    控制台程序 定义Point类: public class Point { // Create a point from its coordinates public Point(double xVal ...

  6. 系统性能调优CPU与内存

    CPU相关术语 处理器:插到系统插槽或者处理器版上的物理芯片,以核或者硬件线程的方式包含了一块或者多块CPU. 核:一颗多核处理器上的一个独立CPU实例.核的使用时处理器扩展的一种方式,有称为芯片级多 ...

  7. PostgreSQL Replication之第十四章 扩展与BDR

    在这一章中,将向您介绍一个全新的技术,成为BDR.双向复制(BDR),在PostgreSQL的世界里,它绝对是一颗冉冉升起的新星.在不久的将来,许多新的东西将会被看到,并且人们可以期待一个蓬勃发展的项 ...

  8. ActionController::InvalidAuthenticityToken 解决办法(第二种尤其有效)

    第一种:  Ror代码 class FooController < ApplicationController       protect_from_forgery :except => ...

  9. char 和 varchar2 区别

    char 与 varchar2 区别 a:char长度固定而varchar2长度可变 b:char的遍历效率要比varchar2的效率稍高 c:char 浪费空间节省时间 varchar2浪费时间节省 ...

  10. 关于内存 GetMemory( ) 笔试分析

    1. #include<stdio.h>#include<string.h>void GetMemory(char *p){ p=(char *)malloc(100); }i ...