Skip to content

Instantly share code, notes, and snippets.

@rishiloyola
Created May 28, 2016 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rishiloyola/f60e6965d6917a79d5c9c6af1bca16c3 to your computer and use it in GitHub Desktop.
Save rishiloyola/f60e6965d6917a79d5c9c6af1bca16c3 to your computer and use it in GitHub Desktop.
Games - How many games ( spoj question).
import java.util.*;
import java.lang.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner s=new Scanner(System.in);
int num = s.nextInt();
for(int i=0;i<num;i++){
String input = s.next();
if (!input.contains(".")){
System.out.println(1);
} else {
String frag = input.substring(input.indexOf('.')+1);
int number = Integer.parseInt(frag);
int length = frag.length();
int power = (int) Math.pow(10, length);
int dec = power;
while(dec%number!=0){
dec = dec + power;
}
System.out.println(dec/number);
}
}
}
}
@deepjyot
Copy link

deepjyot commented Jan 3, 2019

Why is this approach of multiplying the decimal part till an integer is obtained, wrong?
#include<bits/stdc++.h>
using namespace std;
int main() {
int t,int_part,num;
double n,dec_part;
cin>>t;
while(t--) {
cin>>n;
num=1;
int_part=int(n10000)/10000;
dec_part=(n-int_part);
if(dec_part!=0) {
while(1) {
float temp=dec_part
num;
int int_part_temp=int(temp*10000)/10000;
double dec_part_temp=(temp-int_part_temp);
if(dec_part_temp==0)
break;
else
num++;
}
}
cout<<num<<endl;
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment