Sherlock and The Beast
Problem Statement
Sherlock
Holmes is getting paranoid about Professor Moriarty, his arch-enemy.
All his efforts to subdue Moriarty have been in vain. These days
Sherlock is working on a problem with Dr. Watson. Watson mentioned that
the CIA has been facing weird problems with their supercomputer, 'The
Beast', recently.
This afternoon, Sherlock received a note from Moriarty, saying that he has infected 'The Beast' with a virus. Moreover, the note had the numberN printed on it. After doing some calculations, Sherlock figured out that the key to remove the virus is the largest Decent Number having N digits.
A Decent Number has the following properties:
Input Format
The 1st line will contain an integerT , the number of test cases. This is followed by T lines, each containing an integer N . i.e. the number of digits in the number.
Output Format
Largest Decent Number havingN digits. If no such number exists, tell Sherlock that he is wrong and print −1 .
Constraints
1≤T≤20
1≤N≤100000
Sample Input
ForN=1 , there is no such number.
ForN=3 , 555 is the only possible number.
ForN=5 , 33333 is the only possible number.
ForN=11 , 55555533333 and all permutations of these digits are valid numbers; among them, the given number is the largest one.
Solusi
Setelah beberapa kali coba dan gagal akhirnya berikut ini adalah kode yang diterima (accepted).
#include <iostream>
using namespace std;
int main(){
int T;
long int N, buff_N, jumlah_3, jumlah_5;
bool isTrue;
cin >> T;
while(T--){
cin >> N;
buff_N=N;
jumlah_3=0;
jumlah_5=0;
isTrue=true;
while(buff_N>0){
if(buff_N%3==0){
jumlah_3=buff_N/3;
buff_N=0;
}
else{
buff_N-=5;
jumlah_5++;
}
}
if(buff_N<0) isTrue=false;
if(isTrue){
while(jumlah_3--) cout << "555";
while(jumlah_5--) cout << "33333";
cout << endl;
}else cout << "-1" << endl;
}
return 0;
}
Penjelasan source code:
Intinya nilai N akan dikurangi dengan angka 5 secara terus menerus apabila N bukan kelipatan dari 3 dan nilai N lebih besar dari 0. Jika ternyata hasil akhirnya tidak 0 (kurang dari 0) berarti salah (isTrue=false).
source: https://www.hackerrank.com/challenges/sherlock-and-the-beast
This afternoon, Sherlock received a note from Moriarty, saying that he has infected 'The Beast' with a virus. Moreover, the note had the number
A Decent Number has the following properties:
- 3, 5, or both as its digits. No other digit is allowed.
- Number of times 3 appears is divisible by 5.
- Number of times 5 appears is divisible by 3.
Input Format
The 1st line will contain an integer
Output Format
Largest Decent Number having
Constraints
Sample Input
4
1
3
5
11
Sample Output-1
555
33333
55555533333
ExplanationFor
For
For
For
Solusi
Setelah beberapa kali coba dan gagal akhirnya berikut ini adalah kode yang diterima (accepted).
#include <iostream>
using namespace std;
int main(){
int T;
long int N, buff_N, jumlah_3, jumlah_5;
bool isTrue;
cin >> T;
while(T--){
cin >> N;
buff_N=N;
jumlah_3=0;
jumlah_5=0;
isTrue=true;
while(buff_N>0){
if(buff_N%3==0){
jumlah_3=buff_N/3;
buff_N=0;
}
else{
buff_N-=5;
jumlah_5++;
}
}
if(buff_N<0) isTrue=false;
if(isTrue){
while(jumlah_3--) cout << "555";
while(jumlah_5--) cout << "33333";
cout << endl;
}else cout << "-1" << endl;
}
return 0;
}
Penjelasan source code:
Intinya nilai N akan dikurangi dengan angka 5 secara terus menerus apabila N bukan kelipatan dari 3 dan nilai N lebih besar dari 0. Jika ternyata hasil akhirnya tidak 0 (kurang dari 0) berarti salah (isTrue=false).
source: https://www.hackerrank.com/challenges/sherlock-and-the-beast
Tidak ada komentar:
Posting Komentar
Selamat berinovasi :D Salam berbagi..