Replies: 7 comments 3 replies
-
public static class Fraction {
...
public void normalize() {
int gcd = gcd(numerator, denominator);
this.numerator = this.numerator / gcd;
this.denominator = this.denominator / gcd;
}
public static int gcd(int a, int b) {
if (a % b == 0)
return b;
return gcd(b, a % b);
}
@Override
public String toString() {
return (denominator < 0) ? "-" + numerator + "/" + (denominator * -1)
: numerator + "/" + denominator;
}
}
public static int getDecimalNumber(BigDecimal num) {
String str = num.toPlainString();
return (int) Math.pow(10, str.split("\\.")[1].length());
}
public static String solution(String input) {
BigDecimal number = new BigDecimal(input);
int decimalNumber = getDecimalNumber(number);
double denominator = decimalNumber;
int numerator = (int) (decimalNumber * number.doubleValue());
return new Fraction(numerator, (int) denominator).toString();
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
public void normalize() {
int gcd = gcd(numerator, denominator);
this.numerator /= gcd;
this.denominator /= gcd;
}
public String toString() {
String s = "-";
if (getDenominator() < 0) {
return s + getNumerator() + "/" + Math.abs(getDenominator());
}
return getNumerator() + "/" + getDenominator();
}
public static int getDecimalNumber(BigDecimal num) {
String str = num.toPlainString();
return str.substring(str.indexOf(".")).length() - 1;
}
public static String solution(String input) {
BigDecimal number = new BigDecimal(input);
int decimalNumber = getDecimalNumber(number);
double denominator = Math.pow(10, decimalNumber);
int numerator = (int) (Double.parseDouble(input) * denominator);
return new Fraction(numerator, (int) denominator).toString();
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
public void normalize() {
if (normalized)
return;
while (true) {
long gcd = isGcd(numerator, denominator);
if (gcd == 1)
break;
this.numerator /= gcd;
this.denominator /= gcd;
}
if (this.denominator < 0) {
this.numerator = -this.numerator;
this.denominator = -this.denominator;
}
if (this.numerator < 0 && this.denominator < 0) {
this.numerator = -this.numerator;
this.denominator = -this.denominator;
}
normalized = true;
}
public static int getDecimalNumber(BigDecimal num) {
String str = num.toPlainString();
StringTokenizer st = new StringTokenizer(str, ".");
st.nextToken();
return Integer.parseInt(st.nextToken());
}
public static String solution(String input) {
boolean minus = false;
if (input.charAt(0) == '-')
minus = true;
BigDecimal number = new BigDecimal(input);
int decimalNumber = getDecimalNumber(number);
double denominator = lengthSize(decimalNumber, 10);
int numerator = decimalNumber;
if (minus)
numerator = numerator * -1;
return new Fraction(numerator, (int) denominator).toString();
}
public static int lengthSize(int decimalNumber, int decimal) {
if (decimalNumber / decimal > 1) {
decimal = lengthSize(decimalNumber, decimal * 10);
} else return decimal;
return decimal;
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
public static int getDecimalNumber(BigDecimal num) {
String str = num.toPlainString();
return str.split("\\.")[1].length();
}
public static String solution(String input) {
BigDecimal number = new BigDecimal(input);
int decimalNumber = getDecimalNumber(number);
double denominator = Math.pow(10, decimalNumber);
int numerator = (int) (number.doubleValue() * denominator);
return new Fraction(numerator, (int) denominator).toString();
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
public static String solution(String input) {
BigDecimal number = new BigDecimal(input);
int decimalNumber = getDecimalNumber(number);
double denominator = Math.pow(10, decimalNumber);
String str = number.toPlainString();
int numerator = Integer.parseInt(str.substring(str.length() - decimalNumber));
if(str.contains("-")) {
numerator = -1 * numerator;
}
returnpublic void normalize() {
int a = denominator;
int b = numerator < 0 ? -1 * numerator : numerator;
int c = 0;
while(b > 0) {
c = a % b;
a = b;
b = c;
}
this.denominator = denominator / a;
this.numerator = numerator / a;
} |
Beta Was this translation helpful? Give feedback.
2 replies
-
|
고생하셨어요 |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
👍 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
필요한 코드만 올려주세욤!
Beta Was this translation helpful? Give feedback.
All reactions