生活随笔
收集整理的這篇文章主要介紹了
PAT 1048 数字加密
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目鏈接
一開始我還以為以b的長度為基準,因為b是要加密的數(shù)據(jù)啊,看了答案才知道原來要以最長的長度為基準。但是這道題還有個bug,就是當你算出的結果前面有0竟然也可以通過,比如a為1111,b為1111,答案是0202這種情況也可以通過,還以為必須是202呢,當然202也可以通過。這兩種情況竟然都能通過,一個輸入竟然可以有兩個輸出,神奇。
using namespace std;int
main() {string a, b;vector<char>res;vector<char> c = {
'0',
'1',
'2' ,
'3' ,
'4' ,
'5' ,
'6' ,
'7' ,
'8' ,
'9' ,
'J' ,
'Q' ,
'K' };cin >> a >> b;reverse(a.begin(), a.end());reverse(b.begin(), b.end());
if (b.size() > a.size()) {a.append(b.size() - a.size(),
'0');}
else {b.append(a.size() - b.size(),
'0');}int result;int i;
for (i = 0; i < b.size(); i++) {
if (i % 2 == 0) {//奇數(shù)的情況result = (b[i] -
'0' + a[i] -
'0') % 13;res.push_back(c[result]);}
else {//偶數(shù)的情況result = b[i] - a[i];
if (result < 0)result = result + 10;res.push_back(result +
'0');}}reverse(res.begin(), res.end());
for (i = 0; i < res.size(); i++) {
if (res[i] !=
'0')
break;}
for (int j = i; j < res.size(); j++) {cout << res[j];}cout << endl;
return 0;
}
復制代碼
轉載于:https://juejin.im/post/5cb93661f265da038364c05f
總結
以上是生活随笔為你收集整理的PAT 1048 数字加密的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。