CodeChef April Long Challenge Solution| Chef and Dice solution in Java | AskTheCode
Problem:
Chef has N 6-sided standard dice. Each die has dimensions 1×1×1. Since Chef is bored during the quarantine, he decides to stack dice for fun.
First, Chef forms four vertical stacks of dice (not necessarily with the same height; empty stacks are allowed) on his table, which together make up a pile of dice with base area up to 2×2. Among all such structures, the total visible surface area of Chef's structure must be the smallest possible.
Then, Chef calculates the number of pips on the visible faces of all dice in the structure. A face of a die is visible if it does not touch the table or another die.
Now, he is wondering: among all possible arrangements of dice, what is the maximum possible total number of visible pips? Since he is busy cooking, he is asking you to solve this.
Input:
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first and only line of each test case contains a single integer N.
Output:
For each test case, print a single line containing one integer ― the maximum possible number of visible pips.
Example Input:
1 1
Example Output:
20
Explanation:
Example case 1: There is only one die. If Chef places it on the table with 1 pip facing down, the visible pips are 2, 3, 4, 5 and 6, and their sum is 20.
Code:
In Java
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef{
public static void main (String[] args) throws java.lang.Exception{
try {
Scanner sc = new Scanner(System.in);
long t = sc.nextLong();
while(t-- > 0){
long n = sc.nextLong();
long x = n % 4;
long y = n / 4;
long s2 = 0, s3 = 0, s4=0, s5=0, e=0, f=0;
if(x == 1)
s5 = 1;
else if(x == 2)
s4 = 2;
else if(x == 3){
s4 = 2;
s3 = 1;
}
if(y > 0){
s2 += y*4;
f = (4-x)*4;
}
long result = (s2*11)+(s3*15)+(s4*18)+(s5*20)+f;
System.out.println(result);
}
}catch (Exception e) {
}
}
}
In C++
#include <bits/stdc++.h>
using namespace std;
int main(){
long long int T;
cin >> T;
while(T-- > 0){
long long N;
cin >> N;
long long int x = N % 4;
long long int y = N / 4;
long long int s2=0,s3=0,s4=0,s5=0, e=0,f=0;
switch(x){
case 1: s5 = 1; break;
case 2: s4 = 2; break;
case 3: s4 = 2; s3 = 1; break;
}
if(y > 0){
s2 += y*4;
f = (4-x)*4;
}
long long int result = (s2*11)+(s3*15)+(s4*18)+(s5*20)+f;
cout << result << endl;
}
return 0;
}
why my code is not accepting ??? it is running well in other compiler.
#include <iostream>
using namespace std;
int chef_dice(int p){
if (p==1) return 20;
else if (p==2) return 40;
else if (p==3) return 51;
else if (p==4) return 60;
else{
int Div = int(p/4);
int mod = p%4;
int m;
if (mod ==0){
m= 60*Div - 16*(Div-1);
return m;
}
else if (mod != 0){
m = 60*Div +chef_dice(mod) -16*(Div-1)-4*mod;
return m;
}
}
}
int main(){
int n;
cout<<"";
cin>>n;
while (n - - ){
int p;
cout<<"";
cin>>p;
int result = chef_dice(p);
cout<<result<<endl;
}
return 0;
}
atleast give explaination of problem with more example test cases that involve more than 1 die. Otherwise this first test case is telling me nothing. It is shitty test case that doesn't help in understanding the problem itself :(
Please provide the Solution to this i am new here