Java实现 洛谷 P1055 ISBN号码

Java实现 洛谷 P1055 ISBN号码_第1张图片

import java.util.Scanner;
public class Main{
     
	public static void main(String[] args) {
     
		Scanner scanner = new Scanner(System.in);
		String s = scanner.next();
		if (cheak(deal(s)) == -1) {
     
			System.out.println("Right");
		} else {
     
			restore(s, cheak(deal(s)));
		}
	}

	// 处理控制台输入的数据,去掉'-',转换成int类型数组,注意X的处理
	public static int[] deal(String s) {
     
		int[] arr = new int[10];
		int j = 0;
		for (int i = 0; i < s.length(); i++) {
     
			if (s.charAt(i) != '-') {
     
				arr[j] = s.charAt(i) - 48;
				j++;
			}
		}
		return arr;
	}

	// 检测识别码是否正确
	public static int cheak(int[] arr) {
     
		int c = 1, sum = 0, sbm;
		for (int i = 0; i < arr.length - 1; i++) {
     
			sum = sum + arr[i] * c;
			c++;
		}
		sbm = arr[9];
		if (sbm == sum % 11 || (sbm == 40 && sum % 11 == 10)) {
     
			return -1;
		} else {
     
			return sum % 11;
		}
	}

	// 如果识别码不对,修改识别码
	public static void restore(String s, int i) {
     
		String s2 = s.substring(0, s.length() - 1);
		if (i == 10) {
     
			System.out.println(s2 + "X");
		} else {
     
			System.out.println(s2 + i);
		}

	}
}

你可能感兴趣的:(洛谷)