#include <iostream>
using namespace std;
int main()
{
char s[14];
int x;
cin>>s;
x=s[12]-'0';//记录最后一个元素对于0的相对ASCLL值
// cout<<x<<endl;
// cout<<x;
// cout<<s;
int i,j=0,count=0;
for(i=0;i<12;i++)
{
if(s[i]=='-')
{
continue;
}
else
{
j++;
count+=(s[i]-'0')*j;
// cout<<"count="<<count<<endl;
/*
cout<<"i="<<i<<" ";
cout<<"j="<<j<<" ";
cout<<count<<endl;
*/
}
}
if(count%11==10)//当余数是10的时候 特殊处理 将count变为X
{
count='X'-'0';
// cout<<"count1="<<count<<endl;
}
if((count=='X'-'0'&&x==count)||(count%11==x&&count!=40))
{
if(count=='X'-'0'&&x==count)
{
cout<<"Right";
}
if(count%11==x&&count!=40)//除去count==40这种情况
{
// cout<<count%11<<endl;
cout<<"Right";
// cout<<"count2="<<count<<endl;
}
}
else
{
if(count=='X'-'0')
{
s[12]=count+'0';
// cout<<"count3="<<count<<endl;
}
else
{
s[12]=count%11+'0';
// cout<<"count4="<<count<<endl;
}
for(i=0;i<13;i++)
{
cout<<s[i];
}
}
return 0;
}
余数为10的两种情况要特殊考虑
1.输入余数为10
3.输出余数为10
题解:
#include <stdio.h>//代码简洁明了
int main(void){
char a[14], mod[12] = "0123456789X"; //先将mod11后的十一个字符存入数组
gets(a); //输入字符串
int i, j = 1, t = 0;
for(i = 0; i < 12; i++) {
if(a[i] == '-') continue; //字符串为分隔符‘-’时跳过此次循环进入下一次循环
t += (a[i]-'0')*j++; //t储存 第j个 数字 * j 的和
}
if(mod[t%11] == a[12]) printf("Right");
else {
a[12] = mod[t%11]; //若识别码错误,则赋正确的识别码,然后输出
puts(a);
}
return 0;
}
感言:
1.mod数组和mod[t%11]用的很好