题目描述
算法设计+题目分析
分析题目:
1、逐个字符,读入题目给的9个数字和1个识别码;
2、将9个数字和1位识别码转化为数字,并按照题目设定,求出真实的识别码;
3、将两个识别码进行比较,相同打印Right,不同按照格式打印正确的数字。
注意点+记录点
无
代码(100分)
环境
Visual C++6.0
#include <stdio.h>
int main()
{
int record[9];//9位数字
char m;
for(int i=0;i<9;i++)//读入9位数字,遇到-就跳过
{
char temp=getchar();//逐个字符读入
if (temp!='-') record[i]=temp-'0';
else i--;
}
getchar();
m=getchar();//读入的识别码
int x=0;
for(int ii=0;ii<9;ii++)
{
x+=(ii+1)*record[ii];
}
int answer=x % 11;
int answer_give;
if (m!='X'){
answer_give= m-'0';
}
else answer_give=10;
if (answer==answer_give) printf("Right");//判断计算的结果和题目的结果是否相同
else {
if (answer==10) printf("%d-%d%d%d-%d%d%d%d%d-X",record[0],record[1],record[2],record[3],record[4],record[5],record[6],record[7],record[8]);
else printf("%d-%d%d%d-%d%d%d%d%d-%d",record[0],record[1],record[2],record[3],record[4],record[5],record[6],record[7],record[8],answer);
}
return 0;
}
感谢链接
无