Luogu brush questions C++ language | P2911 Bovine Bones G

Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.

Attached is a summary post: Luogu Brush Questions C++ Language | Summary


【Description】

Bessie loved playing board games and role-playing games, so she convinced John to drive her to the little store. There she bought three dice. The faces of these three different dice are  s 1, s 2, s 3 respectively.

For a dice with  S  faces the numbers on each face are 1,2,3,…, S . Each face (number on it) appears with equal probability. Bessie hopes to find out which sum has the highest probability of occurrence among all "sums of numbers on three faces".

Now given the number of sides of each dice, it is necessary to find out which of all "the sum of the numbers on the three sides" appears most frequently. If there are many with the same probability of occurrence, only the smallest one needs to be output.

Data range: 2≤ s 1≤20, 2≤ s 2≤20, 2≤ s 3≤40.

【enter】

* Line 1: Three space-separated integers: S1, S2, and S3

【Output】

* Line 1: The smallest integer sum that appears most frequently when the dice are rolled in every possible combination.

【Input example】

3 2 3

【Example of output】

5

Instructions/Tips

Here are all the possible outcomes.

1 1 1 -> 3  
1 2 1 -> 4  
2 1 1 -> 4  
2 2 1 -> 5  
3 1 1 -> 5  
3 2 1 -> 6 
1 1 2 -> 4  
1 2 2 -> 5  
2 1 2 -> 5  
2 2 2 -> 6  
3 1 2 -> 6  
3 2 2 -> 7 
1 1 3 -> 5  
1 2 3 -> 6  
2 1 3 -> 6  
2 2 3 -> 7  
3 1 3 -> 7  
3 2 3 -> 8

Both 5 and 6 appear most frequently (five times each), so 5 is the answer.

【Code Explanation】

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int s1,s2,s3,a[85]={0};
    cin >> s1 >> s2 >> s3;
    for (int i=1; i<=s1; i++) {
        for (int j=1; j<=s2; j++) {
            for (int k=1; k<=s3; k++) {
                a[i+j+k]++;
            }
        }
    }
    int max=a[3], ans=3;
    for (int i=3; i<=s1+s2+s3; i++) {
        if (a[i]>max) {
            max = a[i];
            ans = i;
        }
    }
    cout << ans;
    return 0;
}

【operation result】

3 2 3
5

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132675974