第一题、查找给定数

来源:互联网 发布:sql server denty_rank 编辑:程序博客网 时间:2024/06/05 02:42

这是我第一次写博客,希望能够与大家多多交流!


/*
 * 本题要求从输入的N个整数中查找给定的X。如果找到,输出X的位置(从0开始数);如果没有找到,输出“Not Found”。
 *
 * 输入格式:
 * 输入在第1行中给出2个正整数N(<=20)和X,第2行给出N个整数。数字均不超过长整型,其间以空格分隔。
 * 输出格式:
 * 在一行中输出X的位置,或者“Not Found”。
 *
 * 输入样例1:
 * 5 7
 * 3 5 7 1 9
 * 输出样例1:
 * 2
 * 输入样例2:
 * 5 7
 * 3 5 8 1 9
 * 输出样例2:
 * Not Found
 */

import java.util.Scanner;

public class O {

    private static Scanner sc;

    public static void main(String[] args) {
        
        sc = new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        boolean b=false;
        
        int[] a=new int[n];
        
        for(int i=0;i<a.length;i++){
            a[i]=sc.nextInt();
        }

        for(int i=0;i<a.length;i++){
            if(a[i]==m){
                b=true;
                System.out.println(i);
            }        
        }
        
        if(!b){
            System.out.println("Not Found");
        }
    }

}

0 0
原创粉丝点击