刷题(一)

简介: 刷题(一)

题目:

本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
 
如下的 
10
10 行数据,每行有 
10
10 个整数,请你求出它们的乘积的末尾有多少个零?
 
5650 4542 3554 473 946 4114 3871 9073 90 4329 
2758 7949 6113 5659 5245 7432 3051 4434 6704 3594 
9937 1173 6866 3397 4759 7557 3070 2287 1453 9899 
1486 5722 3135 1170 4014 5510 5120 729 2880 9019 
2049 698 4582 4346 4427 646 9742 7340 1230 7683 
5693 7015 6887 7381 4172 4341 2909 2027 7355 5649 
6701 6645 1671 5978 2704 9926 295 3125 3878 6785 
2066 4247 4800 1578 6652 4616 1113 6205 3264 2915 
3966 5291 2904 1285 2193 1428 2265 8730 9436 7074 
689 5510 8243 6114 337 4096 8199 7313 3685 211

代码:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int[] nums = new int[100];
        int five=0;
        int two=0;
        for(int i=0;i<100;++i) {
            nums[i]=scan.nextInt();
            int t = nums[i];
            for(;t%2!=1;t/=2)
                two++;
            for(;t%5==0;t/=5)
                five++;
        }
        int result=five<two?five:two;
        System.out.println(result);
        scan.close();
    }
}

思路:影响最后结果的10有多少个就是由2和5决定的,这个题解的思路实在是太巧了👍(看题解的).

第二题:



public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        long num = 0;
        long sum = 0;
        long n = scanner.nextLong();
        long []arr = new long[(int) n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }
        for (int i = 0; i < n-1; i++) {
            for (int j = i+1; j <n; j++) {
                num = arr[i]*arr[j];
                sum = sum+num;
            }
 
        }
        System.out.println(sum);
    }
}//这个是错误的代码哈,只能运行30%,没想到还有这种要求
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        //定义一个数组
        Long [] arr = new Long[n];
        Long sum = 0l;
        //对数组赋值
        for( int i = 0 ; i < arr.length ; i++ ){
            arr[i] = sc.nextLong();
            sum += arr[i];  //对数组求和
        }
        //结果
        Long res = 0l;
        for( int i = 0 ; i < arr.length - 1 ; i++ ){
            //依次减去当前索引所指的数
            sum -= arr[i];
            //累加求和
            res += sum * arr[i];
        }
        System.out.println(res);
    }
}

思路:又是看题解的,因为用常规法时间复杂O(N2),所以行不通。

第三题:

小蓝正在学习一门神奇的语言,这门语言中的单词都是由小写英文字母组 成,有些单词很长,远远超过正常英文单词的长度。小蓝学了很长时间也记不住一些单词,他准备不再完全记忆这些单词,而是根据单词中哪个字母出现得最多来分辨单词。
 
现在,请你帮助小蓝,给了一个单词后,帮助他找到出现最多的字母和这 个字母出现的次数。
输入描述:
输入一行包含一个单词,单词只由小写英文字母组成。
对于所有的评测用例,输入的单词长度不超过 1000。
输出描述
输出两行,第一行包含一个英文字母,表示单词中出现得最多的字母是哪 个。如果有多个字母出现的次数相等,输出字典序最小的那个。
第二行包含一个整数,表示出现得最多的那个字母在单词中出现的次数。
输入输出样例
示例 1
输入
lanqiao
输出
a
2
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        String s =scanner.nextLine();
        int[]array =new int[26];
        for (int i = 0; i < s.length(); i++) {
            array[s.charAt(i)-97]++;
        }
        char ch ='a';
        int max = 0;
        for (int i = 0; i < 26; i++) {
            if (array[i]>max){
                max = array[i];
                ch = (char)('a'+i);
            }
 
        }
        System.out.println(ch);
        System.out.println(max);
    }
}

思路:先定义一个数组对应26个字母的位置,然后找出数组中每个字母出现的次数对应在各个数组。题目说如果有多个字母出现的次数相等,输出字典序最小的那个,但是因为各个字母顺序已经从小到大排好了,所以不用担心,直接就可以了。

第四题:

import java.util.Scanner;
import java.lang.*;
 
public class Main {
    public static void main(String[] args) {
        double jige = 0;
        double you = 0;
        Scanner scanner =new Scanner(System.in);
        int a =scanner.nextInt();
        for (int i = 0; i < a; i++) {
            int b =scanner.nextInt();
            if (b>=60){
                jige+=1;
            }
            if (b>=85){
                you +=1;
            }
        }
        System.out.println(Math.round(jige / a*100)+"%");
        System.out.println(Math.round(you / a*100)+"%");
    }
}

思路:这里要注意的就是这个Math.round,我一开始又没看到😢😢.

第五题:

public class Main {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 1; i <= 2020; i++) {
            String S = i+"";
            for (int j = 0; j < S.length(); j++) {
                if (S.charAt(j) == '2'){
                    count++;
                }
            }
        }
        System.out.println(count);
 
    }
}

思路:这一题的话转换成字符串然后一个个取出来比较是否=='2'就可以了。

好了,今天的博客就到此为止,如果哪里有遗漏,哪里有错误缺点,或者题目有其余解发或者更优解,希望大佬们能在评论区指正。还有还有就是大佬们都是怎么编排题目的,我是直接截图题目了,希望大佬能在评论区教教,感谢感谢

目录
相关文章
|
1月前
leetcode20刷题打卡
leetcode20刷题打卡
21 0
|
1月前
|
索引
刷题之Leetcode35题(超级详细)
刷题之Leetcode35题(超级详细)
19 0
|
1月前
|
算法
刷题之Leetcode59题(超级详细)
刷题之Leetcode59题(超级详细)
13 0
|
1月前
刷题之Leetcode160题(超级详细)
刷题之Leetcode160题(超级详细)
22 0
|
1月前
|
索引
刷题之Leetcode209题(超级详细)
刷题之Leetcode209题(超级详细)
17 0
|
1月前
|
算法
leetcode28刷题打卡
leetcode28刷题打卡
17 0
|
1月前
|
索引
leetcode151刷题打卡
leetcode151刷题打卡
21 0
|
Java 测试技术 C语言
leetcode刷题(5)
各位朋友们,大家好,今天是我leedcode刷题的第五篇,我们一起来看看吧。
|
存储 Java 测试技术
leetcode刷题(6)
各位朋友们大家好,今天是我的leetcode刷题系列的第六篇。这篇文章将与队列方面的知识相关,因为这些知识用C语言实现较为复杂,所以我们就只使用Java来实现。