Concepts Used
Strings
Difficulty Level
Hard
Problem Statement (Simplified):
Print the decimal output of fraction of two given numbers and enclose the repeating part in decimal places in brackets. For example, for a given input of 2 and 3, it’s fraction is 2/3. It’s decimal representation would be 0.6666 where 6 repeats indefinitely, so we cover 6 in brackets and puts output as 0.
Solving Approach :
- If a fraction is non-repeating, any of it’s remainder will come 0 on repetitive division.
- We know decimal portion repeats itself when a remainder repeats.
- So we’ll check if any remainder repeats and will note it’s last position in quotient.
- After we note it, we finish the process of division as now quotient will repeat itself if it goes on.
- Now print the integer part, a decimal point, non-repeating decimal part, an opening bracket, repeating decimal part and an closing bracket if there is any recurring decimal places.
- If there is no recurring decimal portion, just print the integer part, decimal point and fraction.
Solutions
#include <stdio.h>
int main()
{
int num,den;
scanf("%d%d", &num,&den);
int firstValue = num/den;
num -= firstValue * den;
int remPos[1000000] = {0};
char quotient[15];
int recurr = 0;
int rem = num % den ;
int quoPos = 0;
remPos[rem] = 1;
int i = 0;
if(rem!=0)
i = 1;
for( ; rem && !recurr; i++){
int temp = rem * 10;
while(temp<den){
if(remPos[temp] > 0){
rem = temp;
quotient[quoPos++] = '0';
recurr = 1;
break;
}
temp *= 10;
quotient[quoPos++] = '0';
}
if(recurr)
break;
num = temp;
rem = num % den;
quotient[quoPos++] = num/den + '0';
if(rem == 0){
break;
}
else{
if(remPos[rem] > 0){
recurr = 1;
break;
}
else{
remPos[rem] = i+1;
}
}
}
quotient[quoPos] = '\0';
if(!recurr){
if(i == 0 )
printf("%d",firstValue);
else
printf("%d.%s",firstValue,quotient);
}else{
printf("%d.",firstValue);
for(int k=0; k<remPos[rem]-1; k++)
printf("%c",quotient[k]);
printf("(");
for(int k=remPos[rem]-1; k<quoPos; k++){
printf("%c",quotient[k]);
}
printf(")\n");
}
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
int num,den;
cin>>num>>den;
char dec[100000];
int firstValue = num/den;
num -= firstValue * den;
int remPos[1000000] = {0};
int remainders[1000000] = {0};
char quotient[15];
bool recurr = false;
int rem = num % den ;
int quoPos = 0;
remPos[rem] = 1;
int i = 0;
if(rem!=0)
i = 1;
for( ; rem && !recurr; i++){
int temp = rem * 10;
while(temp<den){
if(remPos[temp] > 0){
rem = temp;
quotient[quoPos++] = '0';
recurr = true;
break;
}
temp *= 10;
quotient[quoPos++] = '0';
}
if(recurr)
break;
num = temp;
rem = num % den;
quotient[quoPos++] = num/den + '0';
if(rem == 0){
break;
}
else{
if(remPos[rem] > 0){
recurr = true;
break;
}
else{
remPos[rem] = i+1;
}
}
}
quotient[quoPos] = '\0';
if(!recurr){
if(i == 0 )
cout<<firstValue;
else
cout<<firstValue<<"."<<quotient;
}else{
cout<<firstValue<<".";
for(int k=0; k<remPos[rem]-1; k++)
cout<<quotient[k];
cout<<"(";
for(int k=remPos[rem]-1; k<quoPos; k++){
cout<<quotient[k];
}
cout<<")";
}
cout<<endl;
}
import java.util.*;
import java.io.*;
import java.lang.Math;
public class Main {
public static void main(String args[]) throws IOException {
Scanner sc= new Scanner(System.in);
int num = sc.nextInt();
int den = sc.nextInt();
int firstValue = 0;
int rem = 0;
if(den != 0){
firstValue = num/den;
rem = num % den ;
}
num -= firstValue * den;
int remPos[] = new int[1000000];
String quotient = "";
int recurr = 0;
remPos[rem] = 1;
int i = 0;
if(rem!=0)
i = 1;
for( ; rem!=0 && recurr==0 ; i++){
int temp = rem * 10;
while(temp 0){
rem = temp;
quotient += '0';
break;
}
temp *= 10;
quotient += '0';
}
if(recurr == 1)
break;
num = temp;
rem = num % den;
quotient += (char)(num/den + (int)('0'));
if(rem == 0){
break;
}
else{
if(remPos[rem] > 0){
recurr = 1;
break;
}
else{
remPos[rem] = i+1;
}
}
}
if(recurr==0){
if(i == 0 )
System.out.print(firstValue );
else
System.out.print(firstValue + "." + quotient);
}else{
System.out.print(firstValue + ".");
for(int k=0; k
[forminator_quiz id="739"]
This article tried to discuss Strings. Hope this blog helps you understand and solve the problem. To practice more problems on Strings you can check out .
