美文网首页
第7章 数组下标更多应用

第7章 数组下标更多应用

作者: 得力小泡泡 | 来源:发表于2020-03-30 17:55 被阅读0次

1、统计字符数2

算法分析

  • 1、由于小写字母和大写字母字符相差32,将所有的小写字母统一变成大写字母
  • 2、计算所有字母的数量,算出最大值,再枚举所有字母,若输出第一个字母数量是最大值的字母

时间复杂度 O(n)

Java 代码

import java.util.Scanner;

public class Main{
    static int N = 100;
    static int[] sum = new int[N];
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        char[] temp = scan.next().toCharArray();
        int len = temp.length;
        for(int i = 0;i < len;i ++)
        {
            if(temp[i] - 'a' >= 0 && temp[i] - 'z' <= 0)
                temp[i] = (char)(temp[i] - 32);
        }
        int ans = 0;
        for(int i = 0;i < len;i ++)
        {   
            int x = (int)(temp[i]); 
            sum[x] ++;
        }
        int res = 0;
        for(int i = 0;i < 100;i ++) res = Math.max(res, sum[i]);
        for(int i = 0;i < 100;i ++)
        {
            if(sum[i] == res)
            {
                System.out.print(sum[i] + " " + (char)(i));
                break;
            }
        }
    }
}
import java.util.Scanner;

public class Main {
    static int[] ans = new int[100];
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s = scan.next().trim();
        int n = s.length();
        for(int i = 0;i < n;i ++)
        {
            char t;
            if(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') t = s.charAt(i);
            else
            {
                t = (char)(s.charAt(i) - ('a' - 'A'));
            }
            ans[t] ++;
        }
        int maxv = 0;
        for(int i = 'A';i <= 'Z';i ++)
            maxv = Math.max(maxv, ans[i]);
        
        
        for(int i = 'A';i <= 'Z';i ++)
        {
            if(maxv == ans[i])
            {
                System.out.println(ans[i] + " " + (char)(i) );
                break;
            }
        }
    }
}

2、蒜头君的越野比赛

算法分析

这题主要就是考前缀和 和输入输出,需要熟练输入和输出
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));

时间复杂度 O(n)

Java 代码

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main{
    static int N = 100010;
    static long[] f = new long[N];
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s1 = br.readLine().split(" ");
        int n = Integer.parseInt(s1[0]);
        int k = Integer.parseInt(s1[1]);
        String[] s2 = br.readLine().split(" ");
        for(int i = 2;i <= n;i ++) 
        {
            int x = Integer.parseInt(s2[i - 2]);
            f[i] = f[i - 1] + x;
        }
        while(k -- > 0)
        {
            String[] s3 = br.readLine().split(" ");
            int a = Integer.parseInt(s3[0]);
            int b = Integer.parseInt(s3[1]);
            if(k != 0) log.write(f[b] - f[a] + " ");
            else log.write(f[b] - f[a] + "");
        }
        log.close();
    }
}

3、多项式相乘

算法分析

要求的东西过小,只有两项乘两项,直接写暴力,理清楚下标是幂的次数,值是系数

时间复杂度 O(1)

Java 代码

import java.util.Scanner;

public class Main{
    static int N = 55;
    static int[] f = new int[N];
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a1 = scan.nextInt();
        int a2 = scan.nextInt();
        int b1 = scan.nextInt();
        int b2 = scan.nextInt();
        int c1 = scan.nextInt();
        int c2 = scan.nextInt();
        int d1 = scan.nextInt();
        int d2 = scan.nextInt();
        
        f[a2 + c2] += a1 * c1;
        f[a2 + d2] += a1 * d1;
        f[b2 + c2] += b1 * c1;
        f[b2 + d2] += b1 * d1;
        
        for(int i = 50;i >= 0;i --)
        {
            if(f[i] != 0)
            {
                System.out.println(f[i] + " " + i);
            }
        }
    }
}

相关文章

  • 第7章 数组下标更多应用

    1、统计字符数2 算法分析 1、由于小写字母和大写字母字符相差32,将所有的小写字母统一变成大写字母 2、计算所有...

  • iOS 数组使用:replaceObjectAtIndex:越界

    可能存在问题: 使用的数组下标超出数组最大下标值:比如数组长度count, index的下标范围[0, count...

  • 2.5.2 数组下标

    2.5.2 数组下标 1.数组下标 访问数组的某个元素,只要写出数组名和方括号内的用逗号分开的下标即可。 例如: ...

  • 25 - awk数组

    awk数组的定义及使用 定义数组格式:数组名[下标]=元素值 调用数组格式:数组名[下标] 遍历数组格式:for(...

  • 寻找数组的中心索引【LeetCode】

    给你一个整数数组 nums,请编写一个能够返回数组 “中心下标” 的方法。 数组 中心下标 是数组的一个下标,其左...

  • Jump Game(Leetcode55)

    题目 给定一个非负数组,从第0个下标开始跳,能跳过的最大步长为这个下标对应的数组值,求问能不能从起始节点跳到数组的...

  • 下标

    扩展一个数组的的下标, 传入一个下标的数组,返回这些下标对应的元素的数组 extension Array{ sub...

  • 2021-11-24 724. 寻找数组的中心下标【Easy】

    给你一个整数数组 nums ,请计算数组的 中心下标 。数组 中心下标 是数组的一个下标,其左侧所有元素相加的和等...

  • kotlin精讲-第5章(7)数组下标越界问题

    数组下标越界,可以说是数组操作的一个经典问题。是什么数组下标越界呢?我们在用数组下标去获取元素的时候,如果给定的下...

  • LeetCode 724. 寻找数组的中心下标

    题目 给你一个整数数组 nums ,请计算数组的中心下标。数组中心下标是数组的一个下标,其左侧所有元素相加的和等于...

网友评论

      本文标题:第7章 数组下标更多应用

      本文链接:https://www.haomeiwen.com/subject/rraluhtx.html