Dice arrange [] (https://www.codechef.com/PATN2021/problems/DICEGAME)

Practice

Author: Aryan KD
Tester: Aryan KD
Editorialist: Aryan KD

DIFFICULTY:

CAKEWALK, SIMPLE, EASY.

PREREQUISITES:

Math .

PROBLEM:

Charlie has given work by his teacher , Charlie has given 1 dice and he throws dice N number of times. after throwing dice N number of times he has to arrange the upperface outcome of dice in non-increasing order, without using any sorting Algorithm .

EXPLANATION:

In question given that there is a only one dice and one dice is thrown many times .
so we know that in one dice there is not more than 6 number .
if we throw a dice many time it will never exclude 6
so the way is to take 6 counter which counts the frequency of number from 1 to 6 .
after counting it we have to just print it out one by one in decreasing order .
if we throw one dice 4 times and in upper face outcome is 3 2 5 1
then we will count occurance of each number then print it out by its decreasing order .

SOLUTIONS:

Setter's Solution
  1. //dice game

  2. #include<bits/stdc++.h>

  3. using namespace std;

  4. void testcase()

  5. {

  6. int n;

  7. cin>>n;

  8. int a[n];

  9. int cnt1=0,cnt2=0,cnt3=0,cnt4=0,cnt5=0,cnt6=0;

  10. for(int i=0;i<n;i++){

  11. cin>>a[i];

  12. if(a[i]==1){

  13. cnt1++;

  14. }

  15. else if(a[i]==2){

  16. cnt2++;

  17. }

  18. else if(a[i]==3){

  19. cnt3++;

  20. }

  21. else if(a[i]==4){

  22. cnt4++;

  23. }

  24. else if(a[i]==5){

  25. cnt5++;

  26. }

  27. else

  28. {

  29. cnt6++;

  30. }

  • }
  1. while(cnt6>0){

  2. cout<<6<<" ";

  3. cnt6–;

  4. }

  5. while(cnt5>0){

  6. cout<<5<<" ";

  7. cnt5–;

  8. }

  9. while(cnt4>0){

  10. cout<<4<<" ";

  11. cnt4–;

  12. }

  13. while(cnt3>0){

  14. cout<<3<<" ";

  15. cnt3–;

  16. }

  17. while(cnt2>0){

  18. cout<<2<<" ";

  19. cnt2–;

  20. }

  21. while(cnt1>0){

  22. cout<<1<<" ";

  23. cnt1–;

  24. }

  • return;
  1. }

  2. int main()

  3. {

  4. int t;

  5. cin>>t;

  6. while(t–)

  7. {

  8. testcase();

  9. cout<<endl;

  10. }

  11. return 0;

  12. }

Tester's Solution
                                                  //dice game

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
int n;
cin>>n;
int a[n];
int cnt1=0,cnt2=0,cnt3=0,cnt4=0,cnt5=0,cnt6=0;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]==1){
cnt1++;
}
else if(a[i]==2){
cnt2++;
}
else if(a[i]==3){
cnt3++;
}
else if(a[i]==4){
cnt4++;
}
else if(a[i]==5){
cnt5++;
}
else
{
cnt6++;
}

}
while(cnt6>0){
cout<<6<<" “;
cnt6–;
}
while(cnt5>0){
cout<<5<<” “;
cnt5–;
}
while(cnt4>0){
cout<<4<<” “;
cnt4–;
}
while(cnt3>0){
cout<<3<<” “;
cnt3–;
}
while(cnt2>0){
cout<<2<<” “;
cnt2–;
}
while(cnt1>0){
cout<<1<<” ";
cnt1–;
}

return;
}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}

Editorialist's Solution
  1. //dice game

  2. #include<bits/stdc++.h>

  3. using namespace std;

  4. void testcase()

  5. {

  6. int n;

  7. cin>>n;

  8. int a[n];

  9. int cnt1=0,cnt2=0,cnt3=0,cnt4=0,cnt5=0,cnt6=0;

  10. for(int i=0;i<n;i++){

  11. cin>>a[i];

  12. if(a[i]==1){

  13. cnt1++;

  14. }

  15. else if(a[i]==2){

  16. cnt2++;

  17. }

  18. else if(a[i]==3){

  19. cnt3++;

  20. }

  21. else if(a[i]==4){

  22. cnt4++;

  23. }

  24. else if(a[i]==5){

  25. cnt5++;

  26. }

  27. else

  28. {

  29. cnt6++;

  30. }

  • }
  1. while(cnt6>0){

  2. cout<<6<<" ";

  3. cnt6–;

  4. }

  5. while(cnt5>0){

  6. cout<<5<<" ";

  7. cnt5–;

  8. }

  9. while(cnt4>0){

  10. cout<<4<<" ";

  11. cnt4–;

  12. }

  13. while(cnt3>0){

  14. cout<<3<<" ";

  15. cnt3–;

  16. }

  17. while(cnt2>0){

  18. cout<<2<<" ";

  19. cnt2–;

  20. }

  21. while(cnt1>0){

  22. cout<<1<<" ";

  23. cnt1–;

  24. }

  • return;
  1. }

  2. int main()

  3. {

  4. int t;

  5. cin>>t;

  6. while(t–)

  7. {

  8. testcase();

  9. cout<<endl;

  10. }

  11. return 0;

  12. }