算法逻辑题

目录

统计一个txt文本文件中字母和数字的数量

获取数值数组中第二大的数的索引值,例如:数值数组为  {2,5,8,8,3,7,9,4,1,6,9}, 结果为 2

两个有序数据合并排序


统计一个txt文本文件中字母和数字的数量

思路:1、按行读取这个文件

           2、遍历每一行数据

           3、将一行的数据的每一个数据转换为char类型

           4、根据char字符判断是数字或者字母,若是将数量加一

           5、关闭流

示例如下:

public static void main(String[] args) {
		try {
			File file = new File("C:/tools/aaa.txt");
			FileReader fr = new FileReader(file);
			BufferedReader bf = new BufferedReader(fr);
			String str = bf.readLine();
			int num = 0;
			int word = 0;
			while (str != null) {
				for (int i = 0; i < str.length(); i++) {
					char s = str.charAt(i);
					if (('A' <= s && s <= 'Z') || ('a' <= s && s <= 'z')) {
						word++;
					}
					if ('0' <= s && s <= '9') {
						num++;
					}
				}
				str = bf.readLine();
			}
			bf.close();
			fr.close();
			System.out.println("字母的数量: "+word+", 数字的数量:"+num);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
}

获取数值数组中第二大的数的索引值,例如:数值数组为  {2,5,8,8,3,7,9,4,1,6,9}, 结果为 2

思路:

假设前两个数为最大数和第二大数

循环数组依次拿出数值和前两个数进行 比较

若是小于等于第二大数,不做改变,再往下获取值比较

若是等于最大值也不做改变

若是小于最大值,将此时的值与第二大值和索引进行交换

若是大于最大值,将原来最大值与原第二大值交换,将原最大值与此时值交换

    /**
     * 获取第二大的数的索引
     * @param array
     * @return
     */
    public static int getIndex(int[] array) {
        int big = array[0] > array[1] ? array[0]:array[1];//定义最大值
        int second = array[0] > array[1] ? array[1]:array[0];//定义第二大值
        int sindex = array[0] > array[1] ? 1:0;//定义第二大值索引
        int bindex = array[0] > array[1] ? 0:1;//定义最大值索引
        for (int i = 2; i <array.length; i++) {//循环获取每个数值,与最大数,第二大数比较
            if (array[i] <= second) {

            } else {
                if (array[i] == big) {

                }
                if (array[i] < big) {
                    second = array[i];
                    sindex = i;
                }
                if (array[i] > big) {
                    second = big;
                    big = array[i];
                    sindex = bindex;
                    bindex = i;
                }
            }
        }
        return sindex;
    }

    public static void main(String[] args) {
        int[] array = {2,5,8,8,3,7,9,4,1,6,9};
        
        int result = getIndex(array);
        
        System.out.println(result);
    }

两个有序数据合并排序

    public static void main(String[] args) {
        int[] a = {1,3,5,9,10,15};
        int[] b = {0,4,7,19};
        List<Integer> list = new ArrayList<>();
        getSortedJoin(a,b,list);
        }
    /**
     * 两个有序数组合并排序
     * @param a
     * @param b
     * @param list
     */
    private static void getSortedJoin(int[] a, int[] b, List<Integer> list) {
        int i = 0;//a数组索引
        int j = 0;//b数组索引
        int compare = 0;//a b数组两两比较后比较大的值
        //1、若a、b数组都没有将数据全部取出
        while (i<a.length && j<b.length) {
            if (a[i]>b[j]) {
                list.add(b[j]);
                compare = a[i];
                j++;
            } else {
                list.add(a[i]);
                compare = b[j];
                i++;
            }

        }

        //2、若是a数据已经将所有的数据放入了list中,b数组没有全部取出
        while (i==a.length && j<b.length) {
            list.add(b[j]);
            j++;
        }
        //3、若是b数据已经将所有的数据放入了list中,a数组没有全部取出
        while (i<a.length && j==b.length) {
            list.add(a[i]);
            i++;
        }
        System.out.println(list);
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章