原题
P1898 缘分计算
AC代码
C++#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 10010;
char s[N];//读入的人名缩写字符串
int st,idx;//st:读入的转换数字 idx:存进a数组要用的下标
int a[N],b[N];//用来计算的数组
int change(char c){//将字母用数字取代的函数 哎呀我靠 你好像没学
return (int)(c - 'A' + st);
}
int f(int x){//计算一个数位数的函数 哎呀我靠 你好像没学
int cnt = 0;
while(x) x /= 10,cnt ++;
return cnt;
}
bool stop_check(){//循环条件 哎呀我靠 你好像没学
if(idx > 3) return 1;
if(idx == 3 && a[1] == 1 && a[2] == 0 && a[3] == 0) return 0;
if(idx == 2) return 0;
return 1;
}
signed main(){
cin >> s >> st;
for(int i = 0 ; i < strlen(s) ; i ++){
//int tt = change(s[i]),len = f(tt);
int tt = (int)(s[i] - 'A' + st),len = 0,lrx = tt;
//tt:将字母用数字替代 idx:存入a数组的下标
while(lrx) lrx /= 10,len ++;
//下标+=位数
idx += len;
//存入a数组中
for(int j = 0 ; j < len ; j ++)
a[idx - j] = tt % 10,tt /= 10;
}
//while(stop_check()){
while(idx > 2 && !(idx == 3 && a[1] == 1 && a[2] == 0 && a[3] == 0)){
memset(b,0,sizeof(b));//清空b数组
for(int i = 1 ; i < idx ; i ++)//将相邻的两位数相加,并写下和的个位数
b[i] = (a[i] + a[i + 1]) % 10;
for(int i = 1 ; i < idx ; i ++)//再放入a数组中,循环使用
a[i] = b[i];
idx --;//位数--,显然
}
//输出部分,注意判断
if(idx == 3) return cout << 100,0;
if(a[1] == 0) return cout << a[2],0;
cout << a[1] << a[2];
return 0;
}
ST生成器
C++#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 10010;
string s;
int idx;
int a[N],b[N];
int change(char c,int st){
return (int)(c - 'A' + st);
}
int f(int x){
int cnt = 0;
while(x) x /= 10,cnt ++;
return cnt;
}
bool stop_check(){
if(idx > 3) return 1;
if(idx == 3 && a[1] == 1 && a[2] == 0 && a[3] == 0) return 0;
if(idx == 2) return 0;
return 1;
}
signed main(){
cin >> s;
for(int st = 1 ; st <= 10000 ; st ++){
idx = 0;
for(int i = 0 ; i < s.length() ; i ++){
int tt = change(s[i],st),len = f(tt);
idx += len;
for(int j = 0; j < len; j++) {
a[idx - j] = tt % 10;
tt /= 10;
}
}
while(stop_check()){
for(int i = 1 ; i < idx ; i ++)
b[i] = (a[i] + a[i + 1]) % 10;
for(int i = 1 ; i < idx ; i ++)
a[i] = b[i];
idx --;
}
if(idx == 3 && a[1] == 1 && a[2] == 0 && a[3] == 0) cout << st << '\n';
}
return 0;
}