题解:B3981 [信息与未来 2024] 图灵完备
zlg2011111 · · 题解
本人第一次写题解,大家将就看哈。
通过尝试我们发现, +!+[] 是1,这样我们可以通过叠加做出60分(洛谷只有20分)代码。
#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
cin>>n;
while(n--){
cout<<"+!+[]";
}
return 0;
}
那么如何处理超过1000的数字呢?
通过尝试,我们发现用 [] 将字符串包起来是字符, +[] 则是整数。
所以,我们可以将输入的每一位剥离出来,然后把每一个数用中括号裹起来,这个数便变成了字符类型。数位之间用 + 隔开。最后,用 +[] 把整个字符串变成整数类型。
上代码。
#include<bits/stdc++.h>
using namespace std;
int n,x,cnt;
string ans[107];//表示每个数位。
int main(){
cin>>n;
cout<<"+[";
while(n){
x=n%10;
cnt++;
for(int i=1;i<=x;i++) ans[cnt]+="+!+[]";
if(!x) ans[cnt]="+[]";//注意0要特判。
n/=10;
}
for(int i=cnt;i>=1;i--){//答案要反向输出。
cout<<"[";
cout<<ans[i];
cout<<"]";
if(i!=1) cout<<"+";
}
cout<<"]";
return 0;
}
大佬们写的可能会更简短,勿喷哈!