Homework Help Please - C++

In the new intellectual TV show, the participant passing to the super final is offered the following game: the letter of the Latin alphabet li is written on each of the n sectors of the big drum. After a minute of reflection, the player indicates one of the positions on drum i. Its gain is calculated according to the following rule: for each position j, the smaller of the distances clockwise and counterclockwise from i to j, measured in sectors, is multiplied by the absolute value of the difference of numbers in the alphabet of letters li and lj, after which all such values ​​are summed.

Can you write a program that finds a way to get the most winnings?
It's unclear what the program is supposed to do. What are the inputs? What are the outputs?

It's also unclear what question(s) you have? People on this forum won't write the program for you, but if you ask specific questions and show the work that you've done so far, you'll find it to be a pretty helpful bunch.
Here is the Input data to help solve math homework https://answershark.com/math.html. It helps me to keep my level of learning. The first line of the input file INPUT.TXT contains a positive integer n (1 ≤ n ≤ 100000) - the size of the drum. The second line contains space-separated lowercase Latin letters recorded on the drum.

Output

In the first line of the output OUTPUT.TXT file, output the largest gain that can be obtained with the given arrangement of letters on the reel. In the second line print the number of any of the sectors that the player must point to.
Last edited on
You could start with a brute force approach:
1. Write a function gain(int i, int j) that computes the gain from position i to position j.
2. Write a function gain(int i) that computes the total gain if the player selects position i.
This is simply the sum of gain(i,j) for all values of j <> i.
3. Write a function bestMove() that returns the position with the highest gain. This just
calls gain(i) for all possible values of i and remembers the i value that gives the largest gain.

I suspect this might be too slow and there is probably a faster way to compute the best move. I can't think of an approach off hand. You might try drawing a wheel with 5 positions, fill in some letters and compute the best move by hand. You'll probably discover some pattern that can be exploited.
Hello AlanW1,

Just a little note here:

You have mentioned an input file. Post the file or a good sample if the file is large. This way everyone has the same information to use and do not have to guess at what is in the file. Also seeing what is in the file can help to determine how to read the file.

I do not know what your instructions or directions look like, but one big block,as you have posted, is hard to read. I have found that when you break up the instructions they are not only easier to read, but it shows smaller steps that you can work on instead of trying to do the whole program at once.

In the new intellectual TV show, the participant passing to the super final is offered the following game:

the letter of the Latin alphabet li is written on each of the n sectors of the big drum.

After a minute of reflection, the player indicates one of the positions on drum i.

Its gain is calculated according to the following rule:
for each position j, the smaller of the distances clockwise and counterclockwise from i to j, measured in sectors,
is multiplied by the absolute value of the difference of numbers in the alphabet of letters li and lj,
after which all such values ​​are summed.


The more I read this the more I feel there is something missing.

Working on a program in small parts or steps is much easier than jumping around trying to get several parts to work.

I would start with opening the input file, checking that it is open and reading the file. Until you can get some data to work with the rest of the program can turn out to be a guessing game.

If by chance you were given a sample of input and output then please post that. It does help.

To start with you could write to "cout" instead of the output file just to see what is happening. Later you can change "cout" to he output file stream.

I usually post this after I see the need, but in case you do not know:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



I would take a look at the first line of dhayden's first post. An example of what is in the file and an example of expected out will go a long way at helping you.

Hope that helps,

Andy
Can you write a program that finds a way to get the most winnings?

Yes, I can.

The question is, can you TRY to write the program?

This smells like another CodeChef cheat session.
Last edited on
It seems to be done
Thanks to all!
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
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>

enum
{
    SINM = 60,
    MINH = 60,
    HINHD = 12,
    SINH = MINH * SINM,
    SINHD = HINHD * SINH
};

int
compare(const void *a, const void *b)
{
  return (*(int *)a - *(int *)b);
}

int
main(void)
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n;
    scanf("%d", &n);
    int *a = (int *) calloc(n, sizeof(a[0]));
    int i, j;
    unsigned sum = 0;
    for (i = 0; i < n; ++i) {
        int h, m, s;
        scanf("%d:%d:%d", &h, &m ,&s);
        a[i] = SINH * h + SINM * m + s;
        sum += a[i];
    }
    qsort(a, n, sizeof(a[0]), compare);
    unsigned min = SINHD * n + 1, tmp;
    int k;
    i = 0;
    while (i < n) {
        while (i < n - 1 && a[i] == a[i + 1]) {
            ++i;
        }
        tmp = (n - i - 1) * SINHD + n * a[i] - sum;
        if (tmp < min) {
            min = tmp;
            k = i;
        }
        ++i;
    }
    printf("%01d:%02d:%02d\n", a[k]/SINH, a[k]%SINH/SINM, a[k]%SINM);
    free(a);
    return 0;
}
Your program doesn't read input in the format that you describe. Nor does it generate output in the required format. Nor does the algorithm appear to solve the problem (or I can't figure out how).

Is this program even related to the problem?
dhayden, I will try to do it yourself according to your recommendations.
Thanks!
Last edited on
Topic archived. No new replies allowed.