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
-
//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;
-
}
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
-
//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;
-
}