(算法练习)——201312-2ISBN号码(CCF模拟)

水题不解释~
AC代码:

#include <stdio.h>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;

int main(){
	string str;
	cin>>str;
	int ans = 0;
	int cnt = 0;       //cnt用来统计出现的数字个数 
	for(int i = 0;i <str.size() - 1;i++){
		if(str[i] - '0' >= 0 && str[i] - '0' <= 9){
			cnt++;
			ans = ans + (str[i] - '0')*cnt;
		}
	}
	ans = ans % 11;
	int linshi;   //最后的识别码 
	if(str[str.size() - 1] == 'X'){
		linshi = 10;
	}
	else{
		linshi = str[str.size() - 1] - '0';
	}
	
	if(ans == linshi){
		printf("Right");	
	}
	else{
		for(int i = 0;i <str.size() - 1;i++){
			printf("%c",str[i]);
		}
		if(ans == 10){
			printf("X");
		}
		else{
			printf("%d",ans);
		}
	}
	
	
}
发布了212 篇原创文章 · 获赞 6 · 访问量 6381

猜你喜欢

转载自blog.csdn.net/weixin_42377217/article/details/104357925
今日推荐