12.5k views
2 votes
Akash loves listening to music while jogging and wants to set up a new regime for his jogging with an app named "Jog track" on his new ipod. He would complete his workout based on the playlist time.

Initially, he sets an expected workout time D for the jog. Then, he chooses N songs to be played while jogging. The songs are denoted as S and T, where Si is the Song Number and Ti is the number of times he listens to the particular song. Here, a song may get repeated as he is obsessed with some of the songs.
He now needs to find out his ideal workout time Z for the day. Z is calculated by the following algorithm:
Step 1 - Add up all T to get a sum X.
Step 2 - Multiply X with unique number of songs (by removing the duplicates) to get Y.
Step 3 - All the songs are sorted in descending order according to its corresponding time T If a song is played more than one number of times, consider the song which has the lowest value of T .The sorted songs are given sequence number 0, 1, 2 ...
Step 4 - Multiply T of each unique songs with the sequence number to get different products Pi.
Step 5 - Sort all the products Pi in increasing order and start subtracting each P from Y. Note that each time Y is getting smaller and the last value of Y is Z.
Step 6 - Print Z.
Next, subtract D from Z and print the result R.
Now, Z is compared to D and if :
I) Z is less than D, he did not complete his expected workout time D which he had set before the jog. Hence print "<" (without quotes). .
II) Z is equals to D, he has completed his expected workout time D. Hence print "=" (without quotes).
III) Z is more than D, he has completed more time than the his expected workout time D. Hence print ">" (without quotes).
Write a program to illustrate this scenario by reading the input from STDIN and the output from STDOUT. You should not write arbitrary strings while reading the input and while printing as these contribute to the standard output.
template:

Input Format: The first line of input contains D, the expected workout time for the jog Akash sets. The second line of input contains the number of songs N he intends to listen. The next N lines contain the following: The song track Si and the number of minutes Ti for which he listens that song. Si and Ti are separated by a single white space.
Output Format: The first line of the output contains Z, the ideal workout time for today. The second line of the output contains R. The last line of output contains one of the following: "<", "=" or ">".
Sample Inputl :
12
6
5 2
1 6
2 3
6 10
5 2
1 2
output:
87
75
>
code in c
template:
# include
void getResult(int D, int N, int soundTrack[][2]){
//complete code

}
int main()
{ int D,N;
scanf("%d", &D);
scanf("%d", &N);
int soundTrack[N][2];
for(int i=0; i {
scanf("%d", &soundTrack[i][0]);
scanf("%d", &soundTrack[i][1]);}
getResult(D, N, soundTrack);
return 0;
}

follow all the steps properly while giving a code

User Tmhalbert
by
8.2k points

1 Answer

5 votes
Sure, here is a C program that follows the steps you've provided to calculate Z, R, and the appropriate comparison ("<", "=", or ">"). Make sure to include the necessary libraries and complete the `getResult` function:

```c
#include

void getResult(int D, int N, int soundTrack[][2]) {
int X = 0;
for (int i = 0; i < N; i++) {
X += soundTrack[i][1];
}

// Step 2 - Multiply X by the number of unique songs
int unique_songs = 0;
int prev_song = -1;
for (int i = 0; i < N; i++) {
if (soundTrack[i][0] != prev_song) {
unique_songs++;
prev_song = soundTrack[i][0];
}
}
int Y = X * unique_songs;

// Step 3 - Sort songs in descending order of Ti (and ascending order of Si)
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
if (soundTrack[i][1] < soundTrack[j][1] || (soundTrack[i][1] == soundTrack[j][1] && soundTrack[i][0] > soundTrack[j][0])) {
int temp[2];
temp[0] = soundTrack[i][0];
temp[1] = soundTrack[i][1];
soundTrack[i][0] = soundTrack[j][0];
soundTrack[i][1] = soundTrack[j][1];
soundTrack[j][0] = temp[0];
soundTrack[j][1] = temp[1];
}
}
}

// Step 4 - Calculate Pi for each unique song
int Pi[N];
int prev_T = -1;
int prev_index = -1;
for (int i = 0; i < N; i++) {
if (soundTrack[i][1] != prev_T) {
prev_T = soundTrack[i][1];
prev_index = 0;
} else {
prev_index++;
}
Pi[i] = soundTrack[i][1] * prev_index;
}

// Step 5 - Sort Pi in increasing order and calculate Z
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
if (Pi[i] > Pi[j]) {
int temp = Pi[i];
Pi[i] = Pi[j];
Pi[j] = temp;
}
}
}

int Z = Y;
for (int i = 0; i < N; i++) {
Z -= Pi[i];
}

// Step 6 - Print Z and R
printf("%d\\", Z);
int R = Z - D;
printf("%d\\", R);

// Compare Z and D
if (Z < D) {
printf("<\\");
} else if (Z == D) {
printf("=\\");
} else {
printf(">\\");
}
}

int main() {
int D, N;
scanf("%d", &D);
scanf("%d", &N);
int soundTrack[N][2];
for (int i = 0; i < N; i++) {
scanf("%d", &soundTrack[i][0]);
scanf("%d", &soundTrack[i][1]);
}
getResult(D, N, soundTrack);
return 0;
}
```

This program should read the input as described, calculate Z, R, and make the appropriate comparison, and then print the results accordingly.
User Lowleetak
by
8.1k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.