面向对象程序设计JAVA学习记录(10)

Fruits

The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera decided to go shopping. He made a list of m fruits he wanted to buy. If Valera want to buy more than one fruit of some kind, he includes it into the list several times.

When he came to the fruit stall of Ashot, he saw that the seller hadn't distributed price tags to the goods, but put all price tags on the counter. Later Ashot will attach every price tag to some kind of fruits, and Valera will be able to count the total price of all fruits from his list. But Valera wants to know now what can be the smallest total price (in case of the most «lucky» for him distribution of price tags) and the largest total price (in case of the most «unlucky» for him distribution of price tags).

Input

The first line of the input contains two integer number n and m (1 ≤ n, m ≤ 100) — the number of price tags (which is equal to the number of different kinds of fruits that Ashot sells) and the number of items in Valera's list. The second line contains n space-separated positive integer numbers. Each of them doesn't exceed 100 and stands for the price of one fruit of some kind. The following m lines contain names of the fruits from the list. Each name is a non-empty string of small Latin letters which length doesn't exceed 32. It is guaranteed that the number of distinct fruits from the list is less of equal to n. Also it is known that the seller has in stock all fruits that Valera wants to buy.

Output

Print two numbers a and b (a ≤ b) — the minimum and the maximum possible sum which Valera may need to buy all fruits from his list.

Sample test(s)

input

5 3
4 2 1 10 5
apple
orange
mango

output

7 19

input

6 5
3 5 1 6 8 1
peach
grapefruit
banana
orange
orange

output

11 30

提交时间
2022-11- 18 23:49:57
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int num = 0;
        int a = s.nextInt();
        int b = s.nextInt();
        int[] arr = new int[a];
        int[] count = new int[a];
        String[] name = new String[b];
        for(int i = 0; i < a; i++) arr[i] = s.nextInt();
        Arrays.sort(arr);
        s.nextLine();
        for(int i = 0; i < b; i++){
            String t = s.nextLine();
            int j = 0;
            for(j = 0; j < num; j++){
                if(t.equals(name[j])){
                    count[j]++;
                    break;
                }
            }
            if(j >= num){
                name[num] = t;
                count[num] = 1;
                num++;
            }
        }
        int[] all = new int[num];
        for(int i = 0; i < num; i++) all[i] = count[i];
        Arrays.sort(all);
        int min = 0;
        for(int i = num-1; i >= 0; i--) min += all[i]*arr[num-1-i];
        int max = 0;
        for(int i = num-1; i >= 0; i--) max += all[i]*arr[a+i-num];
        System.out.println(min + " " + max);
        s.close();
    }
}

先统计出现的种类和数量,再利用排序解决问题

Ball

N ladies attend the ball in the King's palace. Every lady can be described with three values: beauty, intellect and richness. King's Master of Ceremonies knows that ladies are very special creatures. If some lady understands that there is other lady at the ball which is more beautiful, smarter and more rich, she can jump out of the window. He knows values of all ladies and wants to find out how many probable self-murderers will be on the ball. Lets denote beauty of the i-th lady byBi, her intellect by Ii and her richness by Ri. Then i-th lady is a probable self-murderer if there is some j-th lady that Bi < Bj, Ii < Ij, Ri < Rj. Find the number of probable self-murderers.

Input

The first line contains one integer N (1 ≤ N ≤ 500000). The second line contains N integer numbers Bi, separated by single spaces. The third and the fourth lines contain sequences Ii and Ri in the same format. It is guaranteed that 0 ≤ Bi, Ii, Ri ≤ 109.

Output

Output the answer to the problem.

Sample test(s)

input

3
1 4 2
4 3 2
2 5 3

output

1
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int[] a = new int[n];
        int[] b = new int[n];
        int[] c = new int[n];
        for(int i = 0; i < n; i++) a[i] = s.nextInt();
        for(int i = 0; i < n; i++) b[i] = s.nextInt();
        for(int i = 0; i < n; i++) c[i] = s.nextInt();
        int sum = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(a[j]>a[i] && b[j]>b[i] && c[j]>c[i]){
                    sum++;
                    break;
                }
            }
        }
        System.out.println(sum);
        s.close();
    }
}

Numbers

Little Petya likes numbers a lot. He found that number 123 in base 16 consists of two digits: the first is 7 and the second is 11. So the sum of digits of 123 in base 16 is equal to 18.

Now he wonders what is an average value of sum of digits of the number A written in all bases from 2 to A - 1.

Note that all computations should be done in base 10. You should find the result as an irreducible fraction, written in base 10.

Input

The first line contains one integer t (1 ≤ t ≤ 10000) — the number of test cases to solve. Each case consists of one lines of single integer number A (3 ≤ A ≤ 1000).

Output

For each test case, output a line which should contain the required average value in format «X/Y», where X is the numerator and Y is the denominator.

Sample test(s)

input

1
5

output

7/3

input

1
3

output

2/1
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        for(int i = 0; i < n; i++){
            int a = s.nextInt();
            int sum = 0;
            for(int j = 2; j < a; j++){
                int t = a;
                while(t > 0){
                    sum += t%j;
                    t -= t%j;
                    t /= j;
                }
            }
            int b = a - 2;
            for(int j = 2; j < sum/2; j++){
                if(sum%j == 0 && b%j == 0){
                    sum /= j;
                    b /= j;
                    j--;
                }
            }
            System.out.println(sum +  "/" + b);
        }
        s.close();
    }
}

Letter A(Optional)

Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.

You are given three segments on the plane. They form the letter A if the following conditions hold:

  • Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments.
  • The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees.
  • The third segment divides each of the first two segments in proportion not less than 1 / 4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1 / 4).

Input

The first line contains one integer t (1 ≤ t ≤ 10000) — the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers — coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length.

Output

Output one line for each test case. Print «YES» (without quotes), if the segments form the letter A and «NO» otherwise.

Sample test(s)

input

3
4 4 6 0
4 1 5 2
4 0 4 4
0 0 0 6
0 6 2 -4
1 1 0 1
0 0 0 5
0 5 2 -1
1 2 0 1

output

YES
NO
YES
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner s=new Scanner (System.in);
        int t,x=0,y=0,m=0,n=0,p=0,q=0,x1=0,y1=0,x2=0,y2=0;
        int count1=0,count2=0;
        note a[]=new note[7];
        for(int i=0;i<7;++i) a[i]=new note();
        while(s.hasNext())
        {
            t=s.nextInt();
            while(t-->0){
                //cnt++;
                for(int i=0; i<3; i++){
                    a[i].x=s.nextInt();
                    a[i].y=s.nextInt();
                    a[i].z=s.nextInt();
                    a[i].zz=s.nextInt();
                }
                for(int i=0; i<2; i++){
                    for(int j=i+1; j<3; j++){
                        if(a[i].x==a[j].x&&a[i].y==a[j].y)
                        {
                            x=a[i].x;y=a[i].y;
                            m=a[i].z;n=a[i].zz;
                            p=a[j].z;q=a[j].zz;
                            count1=i;
                            count2=j;
                        }
                        else if(a[i].x==a[j].z&&a[i].y==a[j].zz)
                        {
                            x=a[i].x;y=a[i].y;
                            m=a[i].z;n=a[i].zz;
                            p=a[j].x;q=a[j].y;
                            count1=i;
                            count2=j;
                        }
                        else if(a[i].z==a[j].z&&a[i].zz==a[j].zz)
                        {
                            x=a[i].z;y=a[i].zz;
                            m=a[i].x;n=a[i].y;
                            p=a[j].x;q=a[j].y;
                            count1=i;
                            count2=j;
                        }
                        else if(a[i].z==a[j].x&&a[i].zz==a[j].y)
                        {
                            x=a[i].z;y=a[i].zz;
                            m=a[i].x;n=a[i].y;
                            p=a[j].z;q=a[j].zz;
                            count1=i;
                            count2=j;
                        }
                    }
                }
                for(int i=0; i<3; i++){
                    if(i!=count1&&i!=count2){
                        x1=a[i].x;y1=a[i].y;x2=a[i].z;y2=a[i].zz;
                        break;
                    }
                }
                int flag=1;
                if(xie(x,y,m,n,p,q)==0) flag=0;
                if(flag!=0){
                    if(fang(x,y,m,n,x1,y1)!=0&&fang(x,y,p,q,x2,y2)!=0){
                        if(juli(x,y,m,n,x1,y1)==0) flag=0;
                        if(juli(x,y,p,q,x2,y2)==0) flag=0;
                    }
                    else if(fang(x,y,p,q,x1,y1)!=0&&fang(x,y,m,n,x2,y2)!=0){
                        if(juli(x,y,m,n,x2,y2)==0) flag=0;
                        if(juli(x,y,p,q,x1,y1)==0) flag=0;
                    }
                    else flag=0;
                }
                if(flag==1) System.out.println("YES");
                else System.out.println("NO");
            }
        }
    }
    public static int fang(int a,int b,int c,int d,int e,int f){
        if((f-b)*(c-a)==(d-b)*(e-a)) return 1;
        return 0;
    }
    public static int xie(int x,int y,int a,int b,int p,int q){
        if((a-x)*(p-x)+(b-y)*(q-y)<0) return 0;
        return 1;
    }
    public static int juli(int x,int y,int m,int n,int a,int b){
        if(x!=a){
            if(a>x){
                if((a-x)*5<(m-x)) return 0;
                if((a-x)*5>(m-x)*4) return 0;
                return 1;
            }
            else{
                if((x-a)*5<(x-m)) return 0;
                if((x-a)*5>(x-m)*4) return 0;
                return 1;
            }
        }
        else{
            if(b>y){
                if((b-y)*5<(n-y)) return 0;
                if((b-y)*5>(n-y)*4) return 0;
                return 1;
            }
            else{
                if((y-b)*5<(y-n)) return 0;
                if((y-b)*5>(y-n)*4) return 0;
                return 1;
            }
        }
    }
}
class note{
    int x,y,z,zz;
}

Bob's Letter

A boy Bob likes to draw. Not long ago he bought a rectangular graph (checked) sheet with n rows and m columns. Bob shaded some of the squares on the sheet. Having seen his masterpiece, he decided to share it with his elder brother, who lives in Flatland. Now Bob has to send his picture by post, but because of the world economic crisis and high oil prices, he wants to send his creation, but to spend as little money as possible. For each sent square of paper (no matter whether it is shaded or not) Bob has to pay 3.14 burles. Please, help Bob cut out of his masterpiece a rectangle of the minimum cost, that will contain all the shaded squares. The rectangle's sides should be parallel to the sheet's sides.

Input

The first line of the input data contains numbers n and m (1 ≤ n, m ≤ 50), n — amount of lines, and m — amount of columns on Bob's sheet. The following n lines contain m characters each. Character «.» stands for a non-shaded square on the sheet, and «*» — for a shaded square. It is guaranteed that Bob has shaded at least one square.

Output

Output the required rectangle of the minimum cost. Study the output data in the sample tests to understand the output format better.

Sample test(s)

input

6 7
.......
..***..
..*....
..***..
..*....
..***..

output

***
*..
***
*..
***

input

3 3
***
*.*
***

output

***
*.*
***
找到最多*的行和列
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int a = s.nextInt();
        String[] arr = new String[a];
        int[] n1 = new int[a];
        int b = s.nextInt();
        int[] n2 = new int[b];
        int count = 0;
        s.nextLine();
        for(int i = 0; i < a; i++){
            arr[i] = s.nextLine();
            for(int j = 0; j < b; j++){
                if(arr[i].charAt(j) == '*'){
                    n1[count] = i;
                    count++;
                    break;
                }
            }
        }
        int p1 = n1[0];
        int p2 = n1[count-1];
        count = 0;
        for(int i = 0; i < b; i++){
            for(int j = 0; j < a; j++){
                if(arr[j].charAt(i) == '*'){
                    n2[count] = i;
                    count++;
                    break;
                }
            }
        }
        int p3 = n2[0];
        int p4 = n2[count-1];
        for(int i = p1; i <= p2; i++){
            for(int j = p3; j <= p4; j++) System.out.print(arr[i].charAt(j));
            System.out.println();
        }
        s.close();
    }
}

Young Photographer

Among other things, Bob is keen on photography. Especially he likes to take pictures of sportsmen. That was the reason why he placed himself in position x0 of a long straight racetrack and got ready to take pictures. But the problem was that not all the runners passed him. The total amount of sportsmen, training at that racetrack, equals n. And each of them regularly runs distances within a particular segment of the racetrack, which is the same for each sportsman. For example, the first sportsman runs from position a1 to position b1, the second — from a2 to b2

What is the minimum distance that Bob should move to have a chance to take pictures of each sportsman? Bob can take a picture of a sportsman, if he stands within the segment that this sportsman covers on the racetrack.

Input

The first line of the input file contains integers n and x0 (1 ≤ n ≤ 100; 0 ≤ x0 ≤ 1000). The following n lines contain pairs of integers ai, bi (0 ≤ ai, bi ≤ 1000; ai ≠ bi).

Output

Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1.

Sample test(s)

input

3 3
0 7
14 2
4 6

output

1

找到最符合的区间(最多人重合)以及去那个区间的路径

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int x = s.nextInt();
        int max = 0;
        int min = 1001;
        int out = 99999;
        int[] arr = new int[1001];
        for(int i = 0; i <1001; i++) arr[i] = 0;
        for(int i = 0; i < n; i++){
            int a  = s.nextInt();
            int b = s.nextInt();
            if(a > b){
                int t = a;
                a = b;
                b = t;
            }
            if(a < min) min = a;
            if(b > max) max = b;
            for(int j = a; j <= b; j++) arr[j]++;
        }
        int flag = 0;
        for(int i = min; i <= max; i++){
            if(arr[i] == n){
                if(out > Math.abs(i-x)) out = Math.abs(i-x);
                flag = 1;
            }
        }
        if(flag == 0) System.out.println("-1");
        else System.out.println(out);
        s.close();
    }
}

Correct Solution?

One cold winter evening Alice and her older brother Bob was sitting at home near the fireplace and giving each other interesting problems to solve. When it was Alice's turn, she told the number n to Bob and said:

—Shuffle the digits in this number in order to obtain the smallest possible number without leading zeroes.

—No problem! — said Bob and immediately gave her an answer.

Alice said a random number, so she doesn't know whether Bob's answer is correct. Help her to find this out, because impatient brother is waiting for the verdict.

Input

The first line contains one integer n (0 ≤ n ≤ 109) without leading zeroes. The second lines contains one integer m (0 ≤ m ≤ 109) — Bob's answer, possibly with leading zeroes.

Output

Print OK if Bob's answer is correct and WRONG_ANSWER otherwise.

Sample test(s)

input

3310
1033

output

OK

input

4
5

output

WRONG_ANSWER

直接对每个字母排序,然后让第一个非0到第一位

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        String a = s.nextLine();
        String b = s.nextLine();
        if(b.charAt(0) == '0' || a.length() != b.length()) System.out.println("WRONG_ANSWER");
        else{
            char arr[] = a.toCharArray();
            Arrays.sort(arr);
            if(arr[0] == '0'){
                for(int i = 1; i < arr.length; i++){
                    if(arr[i] != '0'){
                        arr[0] = arr[i];
                        arr[i] = '0';
                        break;
                    }
                }
            }
            int flag = 0;
            for(int i = 0; i < arr.length; i++){
                if(b.charAt(i) != arr[i]){
                    flag = 1;
                    break;
                }
            }
            if(flag == 0) System.out.println("OK");
            else System.out.println("WRONG_ANSWER");
        }
        s.close();
    }
}

你可能感兴趣的:(JAVA,java,学习,算法)