Structure-01. Rational Number comparison (10)

Source: Internet
Author: User

This question requires programming to compare the two rational numbers.

Input Format:

Input two rational numbers in the form of scores in a row in the format of "A1/B1 A2/B2". The numerator and denominator are all positive integers in the Integer Range.

Output Format:

Output two rational numbers in a row in the format of "A1/B1 link character A2/B2. ">" Indicates "greater than", "<" indicates "less than", and "=" indicates "equal ".

Input Example 1:
1/2 3/4
Output Example 1:
1/2 < 3/4
Input Example 2:
6/8 3/4
Output Example 2:

6/8 = 3/4


import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner cin = new Scanner(System.in);String str = cin.nextLine();int[] a = new int[4];int temp = 0;int j = 0;for (int i = 0; i < str.length(); i++) {if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {temp = temp * 10 + str.charAt(i) - '0';} else {j++;temp = 0;}a[j] = temp;}double d1 = ((double) a[0]) / a[1];double d2 = ((double) a[2]) / a[3];if (d1 > d2) {System.out.printf("%d/%d > %d/%d", a[0], a[1], a[2], a[3]);} else if (d1 < d2) {System.out.printf("%d/%d < %d/%d", a[0], a[1], a[2], a[3]);} else {System.out.printf("%d/%d = %d/%d", a[0], a[1], a[2], a[3]);}}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.