B1048 数字加密

15/20

#include<bits/stdc++.h>
using namespace std;
stack<int> s;
char a[3]={'J','Q','K'};
int main(){
    long long A,B;
    cin>>A>>B;
    int i=1;
    while(A>0&&B>0){
        if(i%2==1){
            s.push((A%10+B%10)%13);
        }else{
            int x=B%10-A%10;
            if(x<0)x+=10;
            s.push(x);
        }
        A/=10;B/=10;
        i++;
    }
    if(A>0)cout<<A;
    if(B>0)cout<<B;
    while(!s.empty()){
        if(s.top()<=9)cout<<s.top();
        else cout<<a[s.top()-10];
        s.pop();
    }
    return 0;
}

后来觉得题目理解有问题,B的每一位数字都要处理。
15/20

#include<bits/stdc++.h>
using namespace std;
stack<int> s;
char a[3]={'J','Q','K'};
int main(){
    long long A,B;
    cin>>A>>B;
    int i=1;
    while(B>0){
        if(i%2==1){
            s.push((A%10+B%10)%13);
        }else{
            int x=B%10-A%10;
            if(x<0)x+=10;
            s.push(x);
        }
        if(A>0)A/=10;
        B/=10;
        i++;
    }
    while(!s.empty()){
        if(s.top()<=9)cout<<s.top();
        else cout<<a[s.top()-10];
        s.pop();
    }
    return 0;
}

然而还是不行,看了其他人的博客,发现是因为没考虑这种A比B长的情况。
1084440-20190319125703757-1426646221.png
真是太奇葩了,明明和题目要求不符了嘛。再改。

#include<bits/stdc++.h>
using namespace std;
stack<int> s;
char a[3]={'J','Q','K'};
int main(){
    long long A,B;
    cin>>A>>B;
    int i=1;
    while(B>0||A>0){
        if(i%2==1){
            s.push((A%10+B%10)%13);
        }else{
            int x=B%10-A%10;
            if(x<0)x+=10;
            s.push(x);
        }
        if(A>0)A/=10;
        if(B>0)B/=10;
        i++;
    }
    while(!s.empty()){
        if(s.top()<=9)cout<<s.top();
        else cout<<a[s.top()-10];
        s.pop();
    }
    return 0;
}

改完了,17/20,还有两个测试点没过。

转载于:https://www.cnblogs.com/MarkKobs-blog/p/10557854.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值