BNU All Your Base (Regionals 2011, North America - South Central USA) - from lanshui_Yang

[PDF Link]

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

Input Output Notes
3
Number of operations; no output for this line
3 + 5 Invalid Neither number is valid
9987654321 + 1 A000000000
-A000000000 - 1 -A000000001

  题目大意:有一种数制(假设为数制base ),从右往左算起,第一位数字(即个位)的范围是0~1 ,第二位数字的范围是 0~2 ,第三位是0~3,……,第十位是 0~A , ……,一直到第35位的数字范围是0~Z。现在给你一个算式,形式是A + B 或 A - B, 其中A、B均是数,并且可正可负。让你判断给出的算式中的两个数A 和 B是否是数制base 的数 ,如果有一个不是则输出Invalid ,如果A 、B合法,则计算出算式的结果,并判断结果是否是数制base的数,如果是,则输出结果,否则,输出Invalid。

        解题思路:这是一道数制方面的题,如果会java 的话,可以先把A、B转换成十进制下的数,相加或相减,算出结果后在转换回数制base下的数。我在这里用的是手动模拟数制加减,写了400多行,这是自我学编程以来写的最长的程序,纪念下,呵呵。

        请看代码:

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<set>
#include<vector>
#define mem(a , b) memset(a , b , sizeof(a))
using namespace std ;
inline void RD(int &a)
{
    a = 0 ;
    char t ;
    do
    {
        t = getchar();
    }
    while(t < '0' || t > '9') ;
    a = t - '0';
    while((t = getchar()) >= '0' && t <= '9')
        a = a * 10 + ( t - '0' );
}
inline void OT(int a)
{
    if(a >= 10)
        OT(a / 10) ;
    putchar(a % 10 + '0') ;
}
const int MAXN = 1111 ;
const int MAXN2 = 40 ;
int pan[MAXN2] ;
char sx[MAXN] ;
char sy[MAXN] ;
char st[MAXN] ;
char op ;
int fx ;
int fy ;
int lenx , leny ;
int fa ; // 记录结果符号
void chu()  // 记录每一位数字的最大值
{
    int i ;
    for(i = 0 ; i < MAXN2 ; i ++)
    {
        pan[i] = i + 1 ;
    }
}
void init()
{
    mem(sx , 0) ;
    mem(sy , 0) ;
    mem(st , 0) ;
    scanf("%s" , st) ;
    lenx = strlen(st) ;
    int i ;
    for(i = 0 ; i < lenx ; i ++)
    {
        sx[i] = st[lenx - i - 1] ;
    }
    if(st[0] == '-')
    {
        fx = -1 ;
        lenx -- ;
        sx[lenx] = '\0' ;
    }
    else
    {
        fx = 1 ;
    }
    cin >> op ;
    scanf("%s" , st) ;
    leny = strlen(st) ;
    for(i = 0 ; i < leny ; i ++)
    {
        sy[i] = st[leny - i - 1] ;
    }
    if(st[0] == '-')
    {
        fy = -1 ;
        leny -- ;
        sy[leny] = '\0' ;
    }
    else
    {
        fy = 1 ;
    }
}
int shu(char s)
{
    int t ;
    if(s >= 'A' && s <= 'Z')
    {
        t = (s - 'A' + 10) ;
    }
    else
        t = (s - '0') ;
    return  t ;
}
int val(char *s , int len)
{
    int i ;
    for(i = 0 ; i < len ; i ++)
    {
        if(shu(s[i]) > pan[i])
        {
            return 0 ;
        }
    }
    return 1 ;
}
int A[MAXN] ;
int cnt = 0 ;
char zf(int b)
{
    if(b >= 0 && b <= 9)
        return b + '0' ;
    return b - 10 + 'A' ;
}
void print()
{
    if(A[35] != 0)
        puts("Invalid") ;
    else
    {
        if(fa == -1)
            putchar('-') ;
        int i ;
        bool first = false ;
        for(i = 34 ; i > 0 ; i --)
        {
            if(A[i] > 0 && !first)
            {
                first = true ;
            }
            if(first)
            {
                printf("%c" , zf(A[i])) ;
            }
        }
        printf("%d\n" , A[0]) ;
    }
}
int comp(char *s1 , int len1 , char *s2 , int len2)
{
    if(len1 < len2)
    {
        return -1 ;
    }
    if(len1 > len2)
    {
        return 1 ;
    }
    else
    {
        int i ;
        for(i = len1 - 1 ; i >= 0 ; i --)
        {
            if(shu(sx[i]) > shu(sy[i]))
            {
                return 1 ;
            }
            else if(shu(sx[i]) < shu(sy[i]))
            {
                return -1 ;
            }
        }
        return 0 ;
    }
}
void SUB(char *s1 , int len1 , char * s2 , int len2)
{
    cnt = 0 ;
    mem(A , 0) ;
    int carry = 0 ;
    int i ;
    for(i = 0 ; i < len2 ; i ++)
    {
        int t1 = shu(s1[i]) + carry ;
        int t2 = shu(s2[i]) ;
        if(t1 >= t2)
        {
            A[cnt ++] = t1 - t2 ;
            carry = 0 ;
        }
        else
        {
            t1 += (pan[i] + 1) ;
            A[cnt ++] = t1 - t2 ;
            carry = -1 ;
        }
    }
    for(i = len2 ; i < len1 ; i ++)
    {
        int t1 = shu(s1[i]) + carry ;
        int t2 = 0 ;
        if(t1 >= t2)
        {
            A[cnt ++] = t1 - t2 ;
            carry = 0 ;
        }
        else
        {
            t1 += (pan[i] + 1) ;
            A[cnt ++] = t1 - t2 ;
            carry = -1 ;
        }
    }
}
void solve1() // 正 + 正
{
    cnt = 0 ;
    mem(A , 0) ;
    int carry = 0 ;
    if(lenx <= leny)
    {
        int i ;
        for(i = 0 ; i < lenx ; i ++)
        {
            int tmp = shu(sx[i]) + shu(sy[i]) + carry ;
            if(tmp > pan[i])
            {
                A[cnt ++] = tmp % (pan[i] + 1);
                tmp /= (pan[i] + 1) ;
                carry = tmp ;
            }
            else
            {
                A[cnt ++] = tmp ;
                carry  = 0 ;
            }
        }
        for(i = lenx ; i < leny ; i ++)
        {
            int tmp = shu(sy[i]) + carry ;
            if(tmp > pan[i])
            {
                A[cnt ++] = tmp % (pan[i] + 1);
                tmp /= (pan[i] + 1) ;
                carry = tmp ;
            }
            else
            {
                A[cnt ++] = tmp ;
                carry  = 0 ;
            }
        }
        if(carry > 0)
            A[cnt ++] = carry ;
    }
    else
    {
        int i ;
        for(i = 0 ; i < leny ; i ++)
        {
            int tmp = shu(sx[i]) + shu(sy[i]) + carry ;
            if(tmp > pan[i])
            {
              A[cnt ++] = tmp % (pan[i] + 1);
                tmp /= (pan[i] + 1) ;
                carry = tmp ;
            }
            else
            {
                A[cnt ++] = tmp ;
                carry  = 0 ;
            }
        }
        for(i = leny ; i < lenx ; i ++)
        {
            int tmp = shu(sx[i]) + carry ;
            if(tmp > pan[i])
            {
               A[cnt ++] = tmp % (pan[i] + 1);
                tmp /= (pan[i] + 1) ;
                carry = tmp ;
            }
            else
            {
                A[cnt ++] = tmp ;
                carry  = 0 ;
            }
        }
        if(carry > 0)
            A[cnt ++] = carry ;
    }
    print() ;
}
void solve2() // 正 - 正
{
    int cmp = comp(sx , lenx , sy , leny) ;
    if(cmp == -1)  // sx < sy
    {
        SUB(sy , leny , sx , lenx) ;
        fa = -1 ;
        print() ;
    }
    else if(cmp == 0)
    {
        puts("0") ;
        fa = 1 ;
    }
    else
    {
        SUB(sx , lenx , sy , leny) ;
        fa = 1 ;
        print() ;
    }
}
void solve3() // 负 + 正
{
    int cmp = comp(sx , lenx , sy , leny) ;
    if(cmp == -1)  // sx < sy
    {
        SUB(sy , leny , sx , lenx) ;
        fa = 1 ;
        print() ;
    }
    else if(cmp == 0)
    {
        puts("0") ;
        fa = 1 ;
    }
    else
    {
        SUB(sx , lenx , sy , leny) ;
        fa = -1 ;
        print() ;
    }
}
void solve()
{
    if(!val(sx , lenx) || !val(sx , leny))
    {
        puts("Invalid") ;
    }
    else
    {
        mem(A , 0) ;
        int flag = 1 ;
        if(fx == 1 && fy == 1)
        {
            if(op == '+')
            {
                fa = 1 ;
                solve1() ;
            }
            else
            {
                solve2() ;
            }
        }
        if(fx == 1 && fy == -1)
        {
            if(op == '+')
            {
                solve2() ;
            }
            else
            {
                fa = 1 ;
                solve1() ;
            }
        }
        if(fx == -1 && fy == 1)
        {
            if(op == '+')
            {
                solve3() ;
            }
            else
            {
                fa = -1 ;
                solve1() ;
            }
        }
        if(fx == -1 && fy == -1)
        {
            if(op == '+')
            {
                fa = -1 ;
                solve1() ;
            }
            else
            {
                solve3() ;
            }
        }

    }
}
int main()
{
    chu() ;
    int T ;
    scanf("%d" , &T) ;
    while (T --)
    {
        init() ;
        solve() ;
    }
    return 0 ;
}



1. 用户与权限管理模块 角色管理: 学生:查看实验室信息、预约设备、提交耗材申请、参与安全考核 教师:管理课题组预约、审批学生耗材申请、查看本课题组使用记录 管理员:设备全生命周期管理、审核预约、耗材采购与分发、安全检查 用户操作: 登录认证:统一身份认证(对接学号 / 工号系统,模拟实现),支持密码重置 信息管理:学生 / 教师维护个人信息(联系方式、所属院系),管理员管理所有用户 权限控制:不同角色仅可见对应功能(如学生不可删除设备信息) 2. 实验室与设备管理模块 实验室信息管理: 基础信息:实验室编号、名称、位置、容纳人数、开放时间、负责人 功能分类:按学科(计算机实验室 / 电子实验室 / 化学实验室)标记,关联可开展实验类型 状态展示:实时显示当前使用人数、设备运行状态(正常 / 故障) 设备管理: 设备档案:名称、型号、规格、购置日期、单价、生产厂家、存放位置、责任人 全生命周期管理: 入库登记:管理员录入新设备信息,生成唯一资产编号 维护记录:记录维修、校准、保养信息(时间、内容、执行人) 报废处理:登记报废原因、时间,更新设备状态为 "已报废" 设备查询:支持按名称、型号、状态多条件检索,显示设备当前可用情况 3. 预约与使用模块 预约管理: 预约规则:学生可预约未来 7 天内的设备 / 实验室,单次最长 4 小时(可设置) 预约流程:选择实验室→选择设备→选择时间段→提交申请(需填写实验目的) 审核机制:普通实验自动通过,高危实验(如化学实验)需教师审核 使用记录: 签到 / 签退:到达实验室后扫码签到,离开时签退,系统自动记录实际使用时长 使用登记:填写实验内容、设备运行情况(正常 / 异常),异常情况需详细描述 违规管理:迟到 15 分钟自动取消预约,多次违规限制预约权限 4. 耗材与安全管理模块 耗材管理: 耗材档案:名称、规格、数量、存放位置、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值