Yemengying @ 2023-07-19 08:04:27
思路是先把字符串里的数字提取出来进行操作后取模, 代码如下:
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
int a[20],j=0,c=0;
for(int i=0;i<s.length()-2;i++){
if(s[i]-'0'>=0&&s[i]-'0'<=9){
a[j]=s[i]-'0';
j++;
}
}
for(int i=0;i<j;i++) c+=a[i]*(i+1);
if(c%11!=s[s.length()-1]-'0'&&c%11!=10) s[s.length()-1]=c%11+'0';
else if(c%11==10&&s[s.length()]!='X') s[s.length()-1]='X';
else{cout<<"Right";return 0;}
for(int i=0;i<s.length();i++) cout<<s[i];
return 0;
}
by 2012GFKKKZ @ 2023-07-19 08:12:11
#include <bits/stdc++.h>
using namespace std;
int main(){
char a,b,c,d,e,f,g,h,i,j;
int x;
scanf("%c-%c%c%c-%c%c%c%c%c-%c",&a,&b,&c,&d,&e,&f,&g,&h,&i,&j);
x=((int(a)-48)*1+(int(b)-48)*2+(int(c)-48)*3+(int(d)-48)*4+(int(e)-48)*5+(int(f)-48)*6+(int(g)-48)*7+(int(h)-48)*8+(int(i)-48)*9)%11;
if((x==10)&&(j=='X'))
{
printf("Right");
return 0;
}
if((x==10)&&(j!='X'))
{
printf("%c-%c%c%c-%c%c%c%c%c-%c",a,b,c,d,e,f,g,h,i,'X');
return 0;
}
if(x==int(j)-48)
{
printf("Right");
return 0;
}
else
{
printf("%c-%c%c%c-%c%c%c%c%c-%d",a,b,c,d,e,f,g,h,i,x);
return 0;
}
return 0;
}
by bookchong @ 2023-07-19 08:25:46
你的代码错在这里
else if(c%11==10&&s[s.length()]!='X')
最后一位应该是s.length()-1,也就是说应该改成这样
if(c%11!=s[s.length()-1]-'0'&&c%11!=10)
把s.length()改成s.length()-1
完整代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
int a[20],j=0,c=0;
for(int i=0;i<s.size()-2;i++){
if(s[i]-'0'>=0&&s[i]-'0'<=9){
a[j]=s[i]-'0';
j++;
}
}
for(int i=0;i<j;i++) c+=a[i]*(i+1);
if(c%11!=s[s.length()-1]-'0'&&c%11!=10) s[s.length()-1]=c%11+'0';
else if(c%11==10&&s[s.length() - 1]!='X') s[s.length()-1]='X';
else{cout<<"Right";return 0;}
cout << s << endl;
return 0;
}
by bookchong @ 2023-07-19 08:26:18
@Yemengying
by bookchong @ 2023-07-19 08:27:39
sorry,那一行错的代码应该改成这样
else if(c%11==10&&s[s.length() - 1]!='X')
by Yemengying @ 2023-07-19 11:01:11
好好好, 谢谢