大佬帮忙看看吧,这个为什么不行?

P1055 [NOIP2008 普及组] ISBN 号码

Walker_Syw @ 2021-10-10 16:37:04

import java.util.Scanner;

public class book 
{
    public static void main(String[] args)
    {
        String book_number;

        Scanner scan=new Scanner(System.in);
        System.out.print("请输入图书编号:");

        book_number=scan.next();

        int[]a=new int[9];
        int num=0;
        int x=Integer.parseInt(book_number.substring(12));

        a[0]=Integer.parseInt(book_number.substring(0,1));
        a[1]=Integer.parseInt(book_number.substring(2,3));
        a[2]=Integer.parseInt(book_number.substring(3,4));
        a[3]=Integer.parseInt(book_number.substring(4,5));
        a[4]=Integer.parseInt(book_number.substring(6,7));
        a[5]=Integer.parseInt(book_number.substring(7,8));
        a[6]=Integer.parseInt(book_number.substring(8,9));
        a[7]=Integer.parseInt(book_number.substring(9,10));
        a[8]=Integer.parseInt(book_number.substring(10,11));

        for(int i=0;i<9;i++)
        {
            num+=a[i]*(i+1);
        }
        num=num%11;
        if(num==x)
            System.out.print("Right");
        else 
        {
            for(int i=0,j=0;i<13;i++)
            {
                if(i==1||i==5||i==11)
                    System.out.print("-");
                else if(i==12)
                    System.out.print(num);
                else
                {
                    System.out.print(a[j]);
                    j++;
                }
            }
        }
    }
}

by Walker_Syw @ 2021-10-10 16:40:55

book改成Main了还不行


by _l_l_ @ 2021-10-10 16:51:23

删除 System.out.print("请输入图书编号:");


by Walker_Syw @ 2021-10-10 20:55:48

@disangan223 谢谢大佬,现在通过了一个编译器,还有几个显示超时,可以帮忙再看看怎么快点吗?


by Walker_Syw @ 2021-10-10 21:38:49

import java.util.Scanner;

public class book
{
    public static void main(String[] args)
    {
        String book_number;

        Scanner scan=new Scanner(System.in);

        book_number=scan.next();

        int[]a=new int[10];
        int num=0;
        int x;
        if(book_number.substring(12)!="X")
            x=Integer.parseInt(book_number.substring(12));
        else
            x=10;

        a[0]=Integer.parseInt(book_number.substring(0,1));
        a[1]=Integer.parseInt(book_number.substring(2,3));
        a[2]=Integer.parseInt(book_number.substring(3,4));
        a[3]=Integer.parseInt(book_number.substring(4,5));
        a[4]=Integer.parseInt(book_number.substring(6,7));
        a[5]=Integer.parseInt(book_number.substring(7,8));
        a[6]=Integer.parseInt(book_number.substring(8,9));
        a[7]=Integer.parseInt(book_number.substring(9,10));
        a[8]=Integer.parseInt(book_number.substring(10,11));

        num=(a[0]*1+a[1]*2+a[2]*3+a[3]*4+a[4]*5+a[5]*6+a[6]*7+a[7]*8+a[8]*9)%11;
        if(num<10)
        {
            if(num==x)
                System.out.print("Right");
            else 
            {
                for(int i=0,j=0;i<13;i++)
                {
                    if(i==1||i==5||i==11)
                        System.out.print('-');
                    else if(i==12)
                        System.out.print(num);
                    else
                    {
                        System.out.print(a[j]);
                        j++;
                    }
                }
            }
        }
        else if(num==10)
        {
            if(num==x)
                System.out.print("Right");
            else 
            {
                for(int i=0,j=0;i<13;i++)
                {
                    if(i==1||i==5||i==11)
                        System.out.print('-');
                    else if(i==12)
                        System.out.print('X');
                    else
                    {
                        System.out.print(a[j]);
                        j++;
                    }
                }
            }
        }
    }
}

by Walker_Syw @ 2021-10-10 21:39:29

@永远的神 现在代码是这样的,还是只有70分,大佬们,萌新求助


by Walker_Syw @ 2021-10-10 21:55:15

搞出来了,搞出来了,忘记考虑10的情况了。谢谢大佬!

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        String book_number;

        Scanner scan=new Scanner(System.in);

        book_number=scan.next();

        int[]a=new int[10];
        int num=0;

        a[0]=Integer.parseInt(book_number.substring(0,1));
        a[1]=Integer.parseInt(book_number.substring(2,3));
        a[2]=Integer.parseInt(book_number.substring(3,4));
        a[3]=Integer.parseInt(book_number.substring(4,5));
        a[4]=Integer.parseInt(book_number.substring(6,7));
        a[5]=Integer.parseInt(book_number.substring(7,8));
        a[6]=Integer.parseInt(book_number.substring(8,9));
        a[7]=Integer.parseInt(book_number.substring(9,10));
        a[8]=Integer.parseInt(book_number.substring(10,11));

        int x=0;
        if(book_number.substring(12).equals("X"))
            x=10;
        else

            x=Integer.parseInt(book_number.substring(12));

        num=(a[0]*1+a[1]*2+a[2]*3+a[3]*4+a[4]*5+a[5]*6+a[6]*7+a[7]*8+a[8]*9)%11;
        if(num<10)
        {
            if(num==x)
                System.out.print("Right");
            else 
            {
                for(int i=0,j=0;i<13;i++)
                {
                    if(i==1||i==5||i==11)
                        System.out.print('-');
                    else if(i==12)
                        System.out.print(num);
                    else
                    {
                        System.out.print(a[j]);
                        j++;
                    }
                }
            }
        }
        else if(num==10)
        {
            if(num==x)
                System.out.print("Right");
            else 
            {
                for(int i=0,j=0;i<13;i++)
                {
                    if(i==1||i==5||i==11)
                        System.out.print('-');
                    else if(i==12)
                        System.out.print('X');
                    else
                    {
                        System.out.print(a[j]);
                        j++;
                    }
                }
            }
        }
    }
}

|