Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
85 views
in Technique[技术] by (71.8m points)

java - Maximum difference between final and initial sum

I am new to programming and having trouble solving one task. I have several input example. First line contains two numbers m (number of digits on paper, 1<m<1000) and h (limit on the number of operations, 1<h<1000). I have the opportunity, no more than h times, to take any number from a piece of paper (means m), then paint over one of the old digits, and write a new arbitrary digit in its place. By what maximum value can I be able to increase the sum of all the numbers on the piece of paper?

First example:

Input:

5 2 //m and h

1 3 1 4 5 //m = 5, so I can add 5 arbitrary numbers and h=2, so I can change 2 numbers

Output:

16 // cause I changed 1 and 1 to 9 and 9, so the difference 8 and 8 and the sum is 16

Second example:

Input:

3 1

99 5 85

Output:

10 //85 to 95, so the difference is 10

Third example:

Input:

1 10

9999

Output:

0 // nothing to be change

What I have for now:

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number: ");
    int m = sc.nextInt();
    int h = sc.nextInt();
    System.out.println("Entered: " + m);
    System.out.println("Entered: " + h);
    int[] numbers = new int[m];
    for(int i = 0; i < m; ++i) {
        numbers[i] = sc.nextInt();
    }
    Arrays.sort(numbers);
//here is my logic: I am changing 1 to 9 
    for (int i = 0; i < h; i++) {
        if (numbers[i] < 10) {
            numbers[i] = 9;
        }
    else if (numbers[i] > 9 and numbers[i] < 100) {
    numbers[i] = 99;
    }
    }
    sc.close();

My logic can work for the first example, but for the second example it won't work. Can you assist me if I am using right logic or is there any easier way to solve this? Thanks in advance.

question from:https://stackoverflow.com/questions/65909121/maximum-difference-between-final-and-initial-sum

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I quickly came up with below solution

public static void calculateMax(int m,int h, int[] arr){
     int sum = 0;
     ArrayList<Integer> al = new ArrayList<>();
     for(int i=0;i<arr.length;i++){
         String stringNum = Integer.toString(arr[i]);
         int num = Integer.parseInt(stringNum.substring(0, 1));
         if(num!=9){
             al.add(Integer.parseInt(("9"+stringNum.substring(1)))-arr[i]);
             continue;
         }
         al.add(0);
     }
     Collections.sort(al);
     int j = al.size()-1;
     for(int i=0;i<h && j>0;i++){
         sum+=al.get(j--);
     }
     System.out.println(sum);
 }

Here what I am doing is basically, calculating for each number what we can get as maximum by removing one digit and replacing by 9. And I am storing them in a list. Then we can sort that list and get 'h' largest numbers from stored list, and essentially getting the sum of them and printing it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...