slry666 @ 2023-08-28 17:28:09
代码如下
#include<iostream>
#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
long long step,l,n,f;
string m,a;
void one(){
for(int i=0;i<l;++i)a[l-i-1]=m[i];
l+=2;
for(int i=0;i<l;i++){
m[i]+=a[i];
if(m[i]>=n) {
m[i+1]++;
m[i]-=n;
}
}while(!m[l-1]) --l;
}int check(){
for(int i=0;i<l;i++){
if(m[i]!=m[l-i-1]) return true;
}return false;
}
int main(){
cin>>n>>m;
l=m.length();
for(int i=0;i<l;i++){
if(m[i]>='0'&&m[i]<='9') m[i]-='0';
else m[i]=m[i]-'A'+10;
}while(check()){
++step;
if(step>30){
f=1;
break;
}one();
}if(f==0) cout<<"STEP="<<step;
else printf("Impossible!");
return 0;
}
by wunaidedanjuan @ 2023-08-28 17:54:36
@slry666 建议如下:
by wunaidedanjuan @ 2023-08-28 17:55:03
@slry666 附本人代码 O v O
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m,r,k;
char d[10008];
int a[10008],b[10008],c[10008],step;
bool ch()
{
for(int i=1;i<=r/2;i++)
if(a[i]!=a[r-i+1])
return 0;
return 1;
}
int main()
{
cin>>n>>d;
for(int i=0;i<strlen(d);i++)
{
if(d[i]=='A')
a[i+1]=10;
else if(d[i]=='B')
a[i+1]=11;
else if(d[i]=='C')
a[i+1]=12;
else if(d[i]=='D')
a[i+1]=13;
else if(d[i]=='E')
a[i+1]=14;
else if(d[i]=='F')
a[i+1]=15;
else a[i+1]=d[i]-'0';
}
r=strlen(d);
while(!ch()&&step<=30)
{
for(int i=1;i<=r;i++)
{
b[i]=a[r-i+1];
}
for(int i=1;i<=r;i++)
{
c[i]=c[i]+a[i]+b[i];
if(c[i]>n-1)
{
c[i]%=n;
c[i+1]++;
if(i==r)
k=r+1;
}
}
r=k;
for(int i=1;i<=r;i++)
{
a[i]=c[i];
c[i]=0;
}
step++;
}
if(ch())
cout<<"STEP="<<step;
else cout<<"Impossible!";
}
by slry666 @ 2023-08-28 17:55:38
谢谢帮助
by TIS_Minecraft_CNAI @ 2023-09-01 20:08:43
@wunaidedanjuan 没有超出string的最大程度。 AC code:
#include <bits/stdc++.h>
using namespace std;
int a[10008],b[10008],c[10008],step,n,m,r,k;
string d;
bool phw() {
for(int i=1; i<=r/2; i++)
if(a[i]!=a[r-i+1])
return 0;
return 1;
}
int main() {
cin>>n>>d;
for(int i=0; i<d.size(); i++) {
if(d[i]=='A')
a[i+1]=10;
else if(d[i]=='B')
a[i+1]=11;
else if(d[i]=='C')
a[i+1]=12;
else if(d[i]=='D')
a[i+1]=13;
else if(d[i]=='E')
a[i+1]=14;
else if(d[i]=='F')
a[i+1]=15;
else a[i+1]=d[i]-'0';
}
r=d.size();
while(!phw() && step<=30) {
for(int i=1; i<=r; i++)
b[i]=a[r-i+1];
for(int i=1; i<=r; i++) {
c[i]=c[i]+a[i]+b[i];
if(c[i]>n-1) {
c[i]%=n;
c[i+1]++;
if(i==r)
k=r+1;
}
}
r=k;
for(int i=1; i<=r; i++) {
a[i]=c[i];
c[i]=0;
}
step++;
}
if(phw())
cout<<"STEP="<<step;
else
cout<<"Impossible!";
}