结构-01. 有理数比较(10)

来源:互联网 发布:如何免备案域名 编辑:程序博客网 时间:2024/06/06 23:50

本题要求编写程序,比较两个有理数的大小。

输入格式:

输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整形范围内的正整数。

输出格式:

在一行中按照“a1/b1 关系符 a2/b2”的格式输出两个有理数的关系。其中“>”表示“大于”,“<”表示“小于”,“=”表示“等于”。

输入样例1:
1/2 3/4
输出样例1:
1/2 < 3/4
输入样例2:
6/8 3/4
输出样例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]);}}}


0 0
原创粉丝点击