#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
char a[15];
int b[10],m=0,sum=0;
for(int i=0;i<13;i++){
cin>>a[i];
if(a[i]!='-' && a[i]!='X'){
b[m]=int(a[i]);
m++;
}
}
for(int i=0;i<9;i++)
sum=b[i]*(i+1)+sum;
if(sum%11==10 && a[12]=='X')
cout<<"Right"<<endl;
else if(sum%11!=10 && a[12]!='X' && sum%11==int(a[12]))
cout<<"Right"<<endl;
else{
for(int i=0;i<12;i++)
cout<<a[i];
if(sum%11==10)
cout<<'X'<<endl;
else
cout<<sum%11<<endl;
}
return 0;
}
The first magnetic field is wrong, because I used the data type conversion under python thinking!
Consider the following example:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
cout<<int('2')<<endl;
return 0;
}
Output:
50
The reason is obvious: when you try to use int for type conversion, you can actually get the corresponding ascii code, not the integer 2!
Ah, I suddenly forgot the '9'-'0'=9 Dafa that I have seen many times? In other words, the integer character minus '0' becomes the corresponding integer, just fine-tune the code according to this idea:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
char a[15];//储存输入的字符串
int b[10],m=0,sum=0;
for(int i=0;i<13;i++){
cin>>a[i];
if(a[i]!='-' && a[i]!='X'){
b[m]=a[i]-'0';//字符型转换为整型
m++;
}
}
for(int i=0;i<9;i++)
sum+=b[i]*(i+1);
if(sum%11==10 && a[12]=='X')
cout<<"Right"<<endl;//单独考虑余数为10
else if(sum%11!=10 && a[12]!='X' && sum%11==a[12]-'0')
cout<<"Right"<<endl;
else{
for(int i=0;i<12;i++)
cout<<a[i];
if(sum%11==10)
cout<<'X'<<endl;//实现紧凑输出
else
cout<<sum%11<<endl;
}
return 0;
}