IO

package InterView.YunCongKeji;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main_1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
long candies = scanner.nextLong();
long num_people = scanner.nextLong();

        if (num_people == 0)
            continue;
        if (num_people == 1){
            System.out.println("["+candies+"]");
        }

        long sum = (1+num_people)*num_people/2;
        long add_per_time = num_people*num_people;

        int count = 0;
        long cur_sum = count*add_per_time + sum;
        while (candies>cur_sum){
            candies -= cur_sum;
            count++;
            cur_sum = count*add_per_time + sum;
        }

        int index =1;
        long temp_val = count*num_people+index;
        while (candies>temp_val){
            candies -=  temp_val;
            index++;
            temp_val = count*num_people+index;
        }

        List<Long> result = new ArrayList<>();
        for (int i=1; i<index; i++){
            long temp = (1+count)*count/2 *num_people + i*(count+1);
            result.add(temp);
        }
        long temp = (1+count-1)*count/2 *num_people + index*(count)+ candies;
        result.add(temp);

        count--;
        for (int i= index+1; i<=num_people;i++){
            temp = (1+count)*count/2 *num_people + i*(count+1);
            result.add(temp);
        }

        //[1,2,3,1]
        System.out.print("[");
        int i=0;
        for (; i<result.size()-1;i++){
            System.out.print(result.get(i)+",");
        }
        System.out.println(result.get(i)+"]");
    }
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package InterView.YunCongKeji;

import java.util.*;

public class Main_2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String data1 = scanner.nextLine();
String data2 = scanner.nextLine();

        String[] data_1 = data1.subSequence(1,data1.length()-1).toString().split(",");
        String[] data_2 = data2.subSequence(1,data2.length()-1).toString().split(",");

        HashSet<Integer> set_1= new HashSet<>();
        HashSet<Integer> set_2 = new HashSet<>();

        for (int i=0; i<data_1.length; i++){
            int temp = Integer.valueOf(data_1[i]);
            set_1.add(temp);
        }

        for (int i=0; i< data_2.length; i++){
            int temp = Integer.valueOf(data_2[i]);
            set_2.add(temp);
        }

        Set<Integer> temp_set = new HashSet<>();
        temp_set.addAll(set_1);

        temp_set.removeAll(set_2);
        set_1.removeAll(temp_set);

        List<Integer> result = new ArrayList<>();
        result.addAll(set_1);
        Collections.sort(result);

        System.out.print("[");
        int i=0;
        for (; i<result.size()-1; i++){
            System.out.print(result.get(i)+",");
        }
        System.out.println(result.get(i)+"]");
    }
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package InterView.YunCongKeji;

import java.util.Scanner;

public class Main_3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String data = scanner.nextLine();
int amount = Integer.valueOf(scanner.nextLine());

        String[] string_array = data.subSequence(1,data.length()-1).toString().split(",");
        int[] coins = new int[string_array.length];
        for (int i=0; i<coins.length; i++){
            coins[i] = Integer.valueOf(string_array[i].trim());
        }

        int[] dp = new int[amount+1];
        for (int j=0; j<coins.length; j++)
            dp[coins[j]] = 1;

        for (int i=1; i<dp.length; i++){
            if (dp[i] == 0)
                continue;

            for (int j=0; j<coins.length; j++){
                int index =  i+ coins[j];
                if (index>=dp.length)
                    continue;

                if (dp[index] == 0)
                    dp[index] = dp[i]+1;
                else
                    dp[index] = Math.min(dp[index],dp[i]+1);
            }
        }

        if (dp[amount]==0)
            System.out.println(-1);
        else
            System.out.println(dp[amount]);
    }
}

}

全部评论

相关推荐

10-09 09:39
门头沟学院 C++
HHHHaos:这也太虚了,工资就一半是真的
点赞 评论 收藏
分享
10-28 14:42
门头沟学院 Java
watermelon1124:因为嵌入式炸了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务