题目传送门 昨天有个大佬把java甩我脸上,告诉我说这道题C++麻烦死了,又没有大数。一开始我还以为真的没有,直到acm群里一个大佬说C++有大数啊,是__int128。于是乎,我今天就用把C++的大数ac了这个题~跟大家分享:
#include "bits/stdc++.h"
using namespace std;
int main(){
__int128 a =0,b;
string s;cin>>s;
int hash[10]={0};
for (int i = 0; i < s.length(); ++i) {
hash[s[i]-'0']++;
if (i!=0) a*=10;
a+=s[i]-'0';
}
a = b = 2*a;
while (b){
hash[b%10]--;
b/=10;
}
s.clear();
while (a){
char d = a%10+'0';
s+=d;
a/=10;
}
for (int i = 0; i < s.length()/2; ++i) swap(s[i],s[s.length()-i-1]);
for (int i = 0; i < 10; ++i) {
if (hash[i]){
cout<<"No"<<endl<<s;
return 0;
}
}
cout<<"Yes"<<endl<<s;
}
这个__int128呢基本上不会爆掉,int有的操作它基本都有。唯一的麻烦就是不能直接读入或者输出,需要用string自己写方法互相转换~
希望文章对你有帮助。
|