3037. 十六进制加法

LUYB.s

用Python作弊…

T = int(input())
i = 0
while i < T:
print(“case #%d:” % i)
a_s, b_s = input().split()
a = int(a_s, 16)
b = int(b_s, 16)
print(“%x”.upper() % (a + b))
i += 1

cww970329

优雅优雅

#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;
const int N = 233;
struct bigNum{
    int a[N];

    bigNum(){
        memset(a,sizeof(a),0);
        for (int i=0;i<N;i++)a[i] = 0;
    }

    void print(){
        for (int i = a[0]; i>0; i--){
            printf("%X",a[i]);
        }
        puts("");
    }

    bigNum operator + (const bigNum &b){
        bigNum c;
        c.a[0] = max(a[0], b.a[0]);
        int x = 0;
        for (int i=1;i<=c.a[0];i++){
            //printf("b[i] = %d", b.a[i]);
            x += a[i] + b.a[i];
            c.a[i] = x % 16;
            x /= 16;
        }
        if (x) c.a[++c.a[0]] = x;
        return c;
    }

}a, b;
//您妈,java题

int qd(char x){
    if ('0' <= x && x <= '9')return x - '0';
    return x - 55;
}

bigNum jd(string st){
    bigNum ans;
    //printf("!!!!!%d\n",ans.a[0]);
    ans.a[0] = st.length();
    for (int i=1; i <= ans.a[0]; i++){
        ans.a[i] = qd(st[ans.a[0] - i]);
    }
    return ans;
}

int main(){
    //freopen("in.txt","r",stdin);
    int T;scanf("%d", &T);
    string st1, st2;
    for (int cas = 0;cas < T;cas++){
        printf("case #%d:\n", cas);
        cin >> st1 >> st2;
        a = jd(st1);
        b = jd(st2);
        bigNum c = a + b;
        c.print();
    }
    return 0;
}
CatFlowers

注意0+0=0这一组数据

Neltharion

有没有完整的测试数据啊。

10152130230

求测试案例
[em:02]

Commando
#include <bits/stdc++.h>

using namespace std;

int todigit(char c) {
    if (isdigit(c)) return c - '0';
    return c - 'A' + 10;
}

char tohexch(int c) {
    if (c < 10) return c + '0';
    return c - 10 + 'A';
}

void addCarry(int& i, int& carry, const string& s, string& res) {
    while (i < s.size()) {
        int val = todigit(s[i]) + carry;
        carry = val / 16;
        val %= 16;
        res = tohexch(val) + res;
        ++i;
    }
}

int main() {
    int T;
    cin >> T;
    for (int c = 0; c < T; ++c) {
        int carry = 0, i;
        string n, m;
        cin >> n >> m;
        reverse(n.begin(), n.end()), reverse(m.begin(), m.end());
        string res;
        for (i = 0; i < n.size() && i < m.size(); ++i) {
            int val = todigit(n[i]) + todigit(m[i]) + carry;
            carry = val / 16;
            val %= 16;
            res = tohexch(val) + res;
        }
        addCarry(i, carry, n, res);
        addCarry(i, carry, m, res);
        if (carry != 0) res = tohexch(carry) + res;
        printf("case #%d:\n%s\n", c, res.data());
    }
}
10152130230

瑞哥送答案来啦

include

include

include

int main()
{
int n,i=0,m,v,j,k,z;
char s[205],t[205],p[16]={‘0’,‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’,’A’,’B’,’C’,’D’,’E’,’F’},q[205];
scanf(“%d”,&v);
getchar();
while(v-i)
{
memset(s,0,sizeof(s));
memset(t,0,sizeof(t));
memset(q,0,sizeof(q));
m=0;
n=0;
z=0;
scanf(“%s %s”,s,t);
m=strlen(s);
n=strlen(t);
for(j=0;j=’A’&&s[m-j-1]<=’F’)
s[m-j-1]=s[m-j-1]-‘A’+10;
else if(s[m-j-1]>=‘0’&&s[m-j-1]<=‘9’)
s[m-j-1]=s[m-j-1]-‘0’;
}
for(j=0;j=’A’&&t[n-j-1]<=’F’)
t[n-j-1]=t[n-j-1]-‘A’+10;
else if(t[n-j-1]>=‘0’&&t[n-j-1]<=‘9’)
t[n-j-1]=t[n-j-1]-‘0’;
}
for(j=0;j<m&&jn)
for(j=n;j<m;j++)
{
q[j]=p[(s[m-j-1]+z)%16];
z=(s[m-j-1]+z)/16;
}
else if(m0&&m!=n) q[j]=p[z]; else if(z>0&&m==n)
q[n]=p[z];
printf(“case #%d:\n”,i);
i++;
k=strlen(q);
for(j=1;j<=k;j++)
printf(“%c”,q[k-j]);
printf(“\n”);
}
return 0;
}
[em:13]

Li Dao

高精度加法,改成%16而已

include

using namespace std;
typedef vector BIG;
int T;

BIG create(const string& strs)
{
BIG ret;
int ll=strs.length();
for(int i=ll-1;i>=0;i–)
{
int xx;
if(isdigit(strs[i])) xx=strs[i]-‘0’;
else xx=strs[i]-‘A’+10;
ret.push_back(xx);
}
return ret;
}

BIG operator+(const BIG& aa,const BIG& bb)
{
BIG ret;
for(int i=0;ibb.size())
for(int i=bb.size();i<aa.size();i++) ret.push_back(aa[i]);
else
for(int i=aa.size();i<bb.size();i++) ret.push_back(bb[i]);
for(int i=0;i<aa.size()-1;i++)
{
ret[i+1]+=ret[i]/16;
ret[i]%=16;
}

while(ret.back()>=16)
{
ret.push_back(ret.back()/16);
ret[ret.size()-2]%=16;
}
return ret;
}

void print(const BIG& xx)
{
for(int i=xx.size()-1;i>=0;i–)
if(xx[i]>=10) putchar(‘A’+xx[i]-10);
else cout<<xx[i];
cout<<endl;
return;
}

void solve()
{
string aa,bb;
cin>>aa>>bb;
BIG xx=create(aa),yy=create(bb);
BIG zz=xx+yy;
print(zz);
return;
}
int main()
{
scanf(“%d”,&T);
for(int step=0;step<T;step++)
{
printf(“case #%d:\n”,step);
solve();
}
return 0;
}

你当前正在回复 博客/题目
存在问题!