CCF(Java)系列之ISBN码(_20131202)

3 篇文章 0 订阅

CCF(Java)系列之ISBN码(_20131202)

插入链接与图片

100分

import java.util.Scanner;

public class _20131202 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        String str1 = str.replace("-", "");
        char[] ch = str1.toCharArray();
        int sum = 0;
        char c='0';
        for (int i = 0; i < 9; i++) {
            sum += (ch[i] - 48) * (i + 1);
        }
        sum = sum % 11;
        if(sum==10){
            c='X';
        }else{
            c=(char)(sum+48);
        }
        if (c== ch[ch.length-1]) {
            System.out.println("Right");
        }else {
            System.out.println(str.substring(0, 12) +c);
        }

    }

70分

这个是我一开始做得代码很啰嗦,但是不知道为什么是错误的,求指教
import java.util.Scanner;

public class _20131202 {
 
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        String[] content = input.split("-");
        int one = Integer.parseInt(content[0]);
        int two = Integer.parseInt(content[1]);
        int three = Integer.parseInt(content[2]);
        int sum = one;
        int count = 10;
        for (int i = 4; i > 1; i--) {
            sum += two % count * i;//698=6*1+9*2+8*3
            two = two / count;
        }
        for (int j = 9; j > 4; j--) {
            sum += three % count * j;
            three = three / count;
        }
        int last1 = sum % 11;
        String out = input.substring(0, 12);
        int last=0;
        //这边处理有点啰嗦
        if (!content[3].equalsIgnoreCase("X")) {
            last = Integer.parseInt(content[3]);
            if (last==10){
                System.out.println(out + "X");
            }else if (last==last1){
                System.out.println("Right");
            }else {
                System.out.println(out + String.valueOf(last1));
            }
        }else {
            if (last1==10){
                System.out.println("Right");
            }else {
                System.out.println(out + String.valueOf(last1));
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值