找數


Submit solution

Points: 1
Time limit: 1.0s
Memory limit: 64M

Author:
Problem type
Allowed languages
Java 19, Java 8

題目說明

請產生一個array{1,3,5,2,10,32,12,26,70,67}, 排序後輸出到螢幕(由大排到小) 請使用者輸入要搜尋的值,你的程式將該數字在排序後array的索引數值列印在螢幕上(請勿用array.sort,搜尋不到請輸出-1)

sample input1

1

sample output1

9

sample input2

30

sample output2

-1


Comments


  • 0
    scu12156135  commented on Dec. 21, 2023, 8:34 a.m.

    import java.util.*; public class h_3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner keyboard = new Scanner(System.in);
        int a[] = {1,3,5,2,10,32,12,26,70,67};
    
        for(int i=0;i<a.length;i++){
            for(int j=0;j<a.length;j++) {
                if(a[i]>a[j]) {
                    int temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
    
                }
            }
        }
        for(int i=0;i<a.length;i++) {
            System.out.print(a[i]+" ");
        }
        System.out.println("");
    
        System.out.println("請輸入要搜尋的值");
        int num = keyboard.nextInt();
        int c = 0;
    
        for(int i=0;i<a.length-1;i++) {
            if(a[i]==num) {
                c++;
                System.out.println(i);
            }
        }
        if(c==0) {
            System.out.println(-1);
        }
    }

    }