Java练习题2021-4

"某游戏公司设计了一个奖励活动,给N个用户(1≤N≤10^7)连续编号为1到N,依据用户的编号S发放奖励。

发放奖励规则为:
公司随机设定三个非零正整数x,y,z。
如果S同时是x、y的倍数,奖励2张卡片;
如果S同时是y、z的倍数,奖励4张卡片;
如果S同时是x、z的倍数,奖励8张卡片;
如果S同时是x,y,z的倍数奖励10张卡片;
其他奖励1张卡片;
以上奖励不能同时享受。满足多个奖励条件时,以最高奖励为准。

求任意连续的L个用户,使得这L个用户得到的奖励总和最多,输出奖励总和的值。


输入说明:第一行,输入N,L,以空格隔开;(1≤L≤N≤10^7)
第二行,输入x,y,z,以空格隔开;(1≤x,y,z≤L)
输出说明:符合条件的连续L个用户的奖励总和的最大值。
输入样例:40 7
3 5 2
输出样例:24"

注释代码过多可以自行删除

package _2021Ti;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Test4 {
    public static void main(String[] args) {
        List scoreList = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        // 即代表N个用户 也代表 编号的结束   , 和L
        String N_L = sc.nextLine();
        String[] s1 = N_L.split(" ");



        // x , y , z
        String xyz = sc.nextLine();
        String[] s2 = xyz.split(" ");

        //例如 N = 20 七个用户 编号就是 1-20
        // 假设 设定  3  5  2
        // 奖励根据 用户编号是否为 xyz 的倍数
        //倍数就是 对几倍求余 % ==0
        //假设L = 7 就是输出任意连续七个用户
        // 这七个用户奖励和是最多的
        // 输出奖励和总是

     //   int jiangLihe = 0;



//        for (int i = 1 ; i <= Integer.valueOf(s1[0]) ; i++){
//            //找出最牛的一组编号 都是给 10张牌的
//            if (i % Integer.valueOf(s2[0]) == 0 && i % Integer.valueOf(s2[1]) == 0 && i % Integer.valueOf(s2[2]) == 0 ){
//                List firstList = new ArrayList<>();
//                firstList.add(i);
//            }
//            //找出第二牛的 给8个牌 x 和 z 倍数
//            if (i % Integer.valueOf(s2[0]) == 0 && i % Integer.valueOf(s2[2]) == 0 ){
//                List secondList = new ArrayList<>();
//                secondList.add(i);
//            }
//            // 第三 y z 给 4 牌
//            if (i % Integer.valueOf(s2[1]) == 0 && i % Integer.valueOf(s2[2]) == 0){
//                List thirdList = new ArrayList<>();
//                thirdList.add(i);
//            }
//
//            // 第四大 x y 的倍数
//            if (i % Integer.valueOf(s2[0]) == 0 && i % Integer.valueOf(s2[1]) == 0){
//                List fourthList = new ArrayList<>();
//                fourthList.add(i);
//            }
//        }

        // 想循环遍历 因为是连续的L个用户
        //外层循环为判断总循环次数 假设药连续的L =3个用户  123 234 345 456 就会有这几种
        // 假设输入 40 用户 7 个一组
        //              33                  40 - 7 = 33
            for (int j = 1; j <= Integer.parseInt(s1[0]) - (Integer.parseInt(s1[1]) -1) ; j++) {
                //总分
                int score = 0;
               // 控制次数
                int i=1;
                // 控制开始数字
                int s = j;
                //这层想 输出循环编号
                //                      7
                    while( i <= Integer.parseInt(s1[1])){

                      //  System.out.print(s);

                        if (s % Integer.valueOf(s2[0]) == 0 && s % Integer.valueOf(s2[1]) == 0 && s % Integer.valueOf(s2[2]) == 0 ) {
                            score += 10;
                        } else  if (s % Integer.valueOf(s2[0]) == 0 && s % Integer.valueOf(s2[2]) == 0 ){
                            score += 8;
                        }else if (s % Integer.valueOf(s2[1]) == 0 && s % Integer.valueOf(s2[2]) == 0){
                            score += 4;
                        }else if (s % Integer.valueOf(s2[0]) == 0 && s % Integer.valueOf(s2[1]) == 0){
                            score +=2;
                        }else {
                            score += 1 ;
                        }
                        s++;
                        i++;
                    }
                    scoreList.add(score);
              //  System.out.println();
            }
        Collections.sort(scoreList);


        System.out.println(scoreList.get(scoreList.size() -1));


    }
}

Java练习题2021-4_第1张图片

你可能感兴趣的:(java,开发语言)