1003: All Your Base

Time Limit: 1 Sec      Memory Limit: 128 MB
Submit: 4      Solved: 2

Description

Premise: Given a specification for a “base” (well, actually a mixed radix number system), take in pairs of numbers written in our “base”, perform a specified operation on them and output the result in our base.

The Base: A number system where the right-most digit (digit 1) can be a counting number between 0 and 1, the second right-most digit (digit 2) can be a counting number between 0 and 2 and, more generally, each digit n (as labeled from the right) can have values between 0 and n. After 9, upper case letters are used, starting with A and going through Z. After the highest digit (which can be 0-Z), no further digits are possible; any numbers which go past that digit are invalid. Negative numbers are prefixed with a single “-” sign. Numbers never have leading zeros, with the exception of zero itself, which is represented by a single “0” character.

Operations: Addition (+) and subtraction (-): The numbers are added or subtracted as normal (including carrying, borrowing, etc).

Input

  • The first line of input is the number (base 10) of operations that need to be performed.
  • Each following line will be at most 1000 bytes and will consist of a variable-radix number, a space, a single character indicating the operation (+, or -), a space, another variable-radix number, and a newline (LF).
  • Either number for any operation (and also the result) may be negative.

Output

  • For each operation in the input, a single line of output should be produced containing either the result (a variable-radix number) or the string “Invalid”) (without quotes) followed by a newline (LF).
  • If either of the input numbers or the resulting number is not valid in the number system, or an error is encountered while performing the operation, the result is invalid.

Sample Input

3
3 + 5
9987654321 + 1
-A000000000 - 1

Sample Output

Invalid
A000000000
-A000000001

HINT

 

Source

 
C语言差点写炸了,/(ㄒoㄒ)/~~,还是基本功不牢。
熟悉一下java,要注意的是:把每一位的权值计算出来,是 d[i] = d[i-1]*i;
把String 转成大整数,相加减得到结果,再返回去,去掉前导零。怎么得到数字,只要除以权值d[i];
//package Main;

import java.util.*;

import java.math.*;;

public class Main {

    BigInteger[] digit = new BigInteger[40];
BigInteger A, B, C;
boolean flag = true; void Getdigit() {
digit[1] = BigInteger.ONE;
for(int i=2;i<40;i++) {
digit[i] = digit[i-1].multiply(BigInteger.valueOf(i));
}
} int GetVal(char c) {
if(c<='9'&&c>='0')
return c-'0';
else return c-'A'+10;
} char RetVal(BigInteger i) {
if(i.intValue()<10)
return (char)(i.intValue()+'0');
else return (char)(i.intValue()-10+(int)'A');
} BigInteger Check(String num) {
BigInteger ret = BigInteger.ZERO;
for(int i=num.length()-1,base = 2;i>=0;i--,base++) {
if(num.charAt(i)=='-') break;
int tm = GetVal(num.charAt(i));
if(tm>=base)
{
flag = false;
return ret;
}
ret = ret.add(digit[base-1].multiply(BigInteger.valueOf(tm)));
}
if(ret.compareTo(digit[36])>=0) {
flag = false;
return ret;
}
if (num.charAt(0)=='-') {
ret = BigInteger.ZERO.subtract(ret);
}
return ret;
} void Print(BigInteger num) {
if(num.compareTo(BigInteger.ZERO)<0)
{
System.out.printf("-");
num = BigInteger.ZERO.subtract(num);
}
int i = 35;
for(;i>1;i--) {
if(num.divide(digit[i]).compareTo(BigInteger.ZERO)>0)
break;
}
for(;i>=1;i--) {
System.out.printf("%c", RetVal(num.divide(digit[i])));
num = num.mod(digit[i]);
}
System.out.println(); } void main() {
Getdigit();
String num1, num2, op;
Scanner cin = new Scanner(System.in);
int cas = cin.nextInt();
for (int i = 0; i < cas; i++) { flag = true;
num1 = cin.next();
op = cin.next();
num2 = cin.next(); A = Check(num1);
B = Check(num2); if (!flag) {
System.out.println("Invalid");
continue;
}
if (op.charAt(0) == '+') {
C = A.add(B);
} else {
C = A.subtract(B);
}
if (C.abs().compareTo(digit[36]) >= 0) {
System.out.println("Invalid");
continue;
} else {
Print(C);
} }
} public static void main(String[] args) {
new Main().main();
} } /**************************************************************
Problem: 1003
User: YinJianZuiShuai
Language: Java
Result: Accepted
Time:196 ms
Memory:8424 kb
****************************************************************/

CSUFT 1003 All Your Base的更多相关文章

  1. 【笔记】Unix 平台标准

    POSIX 表示可移植操作系统接口(Portable Operating System Interface ,缩写为 POSIX ),POSIX标准定义了操作系统应该为应用程序提供的接口标准,是IEE ...

  2. 我眼中的Linux设备树(六 memory&chosen节点)

    六 memory&chosen节点根节点那一节我们说过,最简单的设备树也必须包含cpus节点和memory节点.memory节点用来描述硬件内存布局的.如果有多块内存,既可以通过多个memor ...

  3. Linux设备树(六 memory&chosen节点)

    六 memory&chosen节点 根节点那一节我们说过,最简单的设备树也必须包含cpus节点和memory节点.memory节点用来描述硬件内存布局的.如果有多块内存,既可以通过多个memo ...

  4. Linux就这个范儿 第19章 团结就是力量 LSB是Linux标准化基地(Linux Standards Base)的简称

    Linux就这个范儿 第19章 团结就是力量  LSB是Linux标准化基地(Linux Standards Base)的简称 这个图片好可爱,它是LSB组织的图标.你肯定会问:“图标这么设计一定有说 ...

  5. 1003: [ZJOI2006]物流运输 最短路+dp

    https://www.lydsy.com/JudgeOnline/problem.php?id=1003 数据范围很小,怎么瞎搞都行,n方dp,然后跑出最短路暴力转移,需要注意的是不能使用的可能有多 ...

  6. 小白解决CENTOS7错误:Cannot find a valid baseurl for repo: base/7/x86_6

    刚入手的MacBook想着学点东西,本汪还是决定玩玩CentOS服务器,安装好了VirtualBox + CentOS. 打开一看,懵逼了!命令行! 行吧,先装个图形界面: $sudo yum gro ...

  7. 分布式系列文章——从ACID到CAP/BASE

    事务 事务的定义: 事务(Transaction)是由一系列对系统中数据进行访问与更新的操作所组成的一个程序执行逻辑单元(Unit),狭义上的事务特指数据库事务. 事务的作用: 当多个应用程序并发访问 ...

  8. base的应用

    ------------父类   public class Person   {       public Person(string name,int age)    {       this.Na ...

  9. C# base 64图片编码解码

    使用WinForm实现了图片base64编码解码的 效果图: 示例base 64编码字符串: /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKD ...

随机推荐

  1. PHP——生成随机数和日期时间

    在PHP里面两个常用的函数 rand();    生成随机数   当括号内无参数时 系统会以当前时间为种子进行随机数的生成 rand(1,10);  括号里面是生成随机数的范围,在形成的随机数在1~1 ...

  2. break和continue的区别以及标签label的使用

    break表示直接跳出当前循环,break只能运用于switch--case语句以及循环之中 continue则表示跳出当次循环,继续执行下一次循环 label标签则可以选择break,或者conti ...

  3. The Havel-Hakimi Algorithm

    1.If any di>=n then fail 2.If there is an odd number of odd degrees then fail 3.If there is a di& ...

  4. using gulp

    原 荐 gulp构建前端工程 半张一块 发布时间: 2016/07/27 16:22 阅读: 895 收藏: 4 点赞: 4 评论: 2 摘要 Gulp 是一个自动化工具,前端开发者可以使用它来处理常 ...

  5. C++之路进阶——bzoj1030(文本生成器)

    F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  hyxzc Logout 捐赠本站 Notice:由于本OJ建立在 ...

  6. CCSprite的使用方法大全

    一.精灵创建及初始化 1.从图片文件创建: CCSprite *sprite = [CCSprite spriteWithFile:@"ImageFileName.png"]; 默 ...

  7. CSS_03_01_CSS组合选择器

    CSS组合选择器 第01步:创建css:with.css @charset "utf-8"; /* 组合选择器,用","隔开 */ .a,.b,div span ...

  8. Union与union all区别

    Union因为要进行重复值扫描,所以效率低.如果合并没有刻意要删除重复行,那么就使用Union All  两个要联合的SQL语句 字段个数必须一样,而且字段类型要“相容”(一致): 如果我们需要将两个 ...

  9. JVM中启用逃逸分析

    -XX:+DoEscapeAnalysis 逃逸分析优化JVM原理我们知道java对象是在堆里分配的,在调用栈中,只保存了对象的指针.当对象不再使用后,需要依靠GC来遍历引用树并回收内存,如果对象数量 ...

  10. PAT乙级 1009. 说反话 (20)

    1009. 说反话 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一句英语,要求你编写程序,将句中 ...