网易9/19

网易9/19

写在最前面:等一个大佬分享一下第一题和最后一题的正解
备注:并非全A,分别是75/100/100/90

第一题 涂格子

找到最多的颜色,第一次整个涂上,然后再遍历,当少数颜色出现,上一个是多数颜色count++,不是多数颜色不管他

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class WY01 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String m = in.nextLine();
        String line = in.nextLine();
        int countR = 0;
        if (line.charAt(0) == 'R')countR++;
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 1; i < line.length(); i++) {
            //红色的数量
            if (line.charAt(i) == 'R'){
                countR++;
            }
        }
        int countB = line.length() - countR;
        if (countB >= countR){
            System.out.println(countCh(line,queue,'B')+1);
        }else {
            System.out.println(countCh(line,queue,'R')+1);
        }
    }
    private static int countCh(String line, Queue<Integer> queue, char color){
        int res = 0;
        if (line.charAt(0) != color){
            res++;
        }
        for (int i = 1; i < line.length(); i++) {
            if (line.charAt(i) != color){
                if (line.charAt(i-1) != color){
                    continue;
                }else {
                    res++;
                }
            }
        }
        return res;
    }
}

第二题 找它数

import java.util.Scanner;

public class WY02 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String lien = in.nextLine();
        String[] sij = lien.split(" ");
        int x = Integer.valueOf(sij[0]);
        int y = Integer.valueOf(sij[1]);
        if (y%x != 0){
            System.out.println("-1");
        }else {
            int res = y/x;
            for (int i = 2; i <= res; i++) {
                if (res%i == 0){
                    System.out.println(times(res));
                    break;
                }
            }
        }
    }
    private static int times(int res){

        int resoult = 0;
        for (int i = 2; i <= res; i++) {
            if (res%i == 0){
                resoult += times(res/i)+1;
                break;
            }
        }
        return resoult;
    }
}

第三题 AcMer 字符串处理

import java.util.Scanner;

public class WY03 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < line.length()-4; i++) {
            int count = 0;
            //A
            if (line.charAt(i) == 'A'){

            }else if (line.charAt(i) == 'a' || line.charAt(i) <= 'Z'&& line.charAt(i) >= 'A'){
                count += 5;
            }else {
                count += 10;
            }
            if(count > min) continue;
            //c
            if (line.charAt(i+1) == 'c'){

            }else if (line.charAt(i+1) == 'C' || line.charAt(i+1) <= 'z'&& line.charAt(i+1) >= 'a'){
                count += 5;
            }else {
                count += 10;
            }
            if(count > min) continue;
            //M
            if (line.charAt(i+2) == 'M'){

            }else if (line.charAt(i+2) == 'm'|| line.charAt(i+2) <= 'Z'&& line.charAt(i+2) >= 'A'){
                count += 5;
            }else {
                count += 10;
            }
            if(count > min) continue;
            //e
            if (line.charAt(i+3) == 'e'){

            }else if (line.charAt(i+3) == 'E'|| line.charAt(i+3) <= 'z'&& line.charAt(i+3) >= 'a'){
                count += 5;
            }else {
                count += 10;
            }
            if(count > min) continue;
            //r
            if (line.charAt(i+4) == 'r'){

            }else if (line.charAt(i+4) == 'R'||line.charAt(i+4) <= 'z'&& line.charAt(i+4) >= 'a'){
                count += 5;
            }else {
                count += 10;
            }
            min = Math.min(count,min);
        }
        System.out.println(min);
    }
}

第四题 找公比

1+n*公比 = 结果
所以结果-1除以公比余数为0,以此去掉一些不必要的遍历
正确做法应该不是这样,看看得了

import java.util.Scanner;

public class WY04 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for (int i = 0; i < t; i++) {
            int num = in.nextInt();
            //q^n = (q-1)x + 1
            int q = 0;
            int n = 0;
            for (int j = 2; j < num; j++) {
                //如果公比为偶数,目标为偶数,则此公比不可能匹配
                /*
                if( j%2 == 0 && num%2 == 0){
                    continue;
                }*/
                //除了第一项,其它均为公比倍数相加,除以公比必余0
                if((num-1)%j != 0){
                    continue;
                }
                int sum = 1;
                q = j;
                n = 1;
                while(sum < num){
                    sum += q;
                    q = q*j;
                    n++;
                }
                if (sum == num){
                    q = j;
                    break;
                }
            }
            System.out.println(n +" " + q);
        }
    }
}
全部评论

相关推荐

冲芭芭拉鸭:你这图还挺新,偷了。
投递美团等公司10个岗位
点赞 评论 收藏
分享
11-26 15:41
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
1
分享
牛客网
牛客企业服务