给定两个具有分子nume1和nume2以及deno1和deno2作为它们各自分母的分数,任务是比较两个分数并找出更大的分数。就像我们有分数1/2和2/3,较高的分数是2/3,因为1/2的值是0.5,而2/3的值是0.66667,这是较高的。
输入值
first.nume = 2, first.deno = 3 second.nume = 4, second.deno = 3
输出结果
4/3
说明
2/3 = 0.66667 < 4/3 = 1.33333
输入值
first.nume = 1, first.deno = 2 second.nume = 4, second.deno = 3
输出结果
4/3
// baadme likhunga
Start Declare a struct Fraction with elements nume, deno In function Fraction greater(Fraction first, Fraction sec) Step 1→ Declare and Initialize t Y = first.nume * sec.deno - first.deno * sec.nume Step 2→ Return (Y > 0) ? first : sec In function int main() Step 1→ Declare Fraction first = { 4, 5 } Step 2→Fraction sec = { 3, 4 } Step 3→ Fraction res = greater(first, sec) Step 4→ Print res.nume, res.deno Stop
#include <stdio.h> struct Fraction { int nume, deno; }; //获取两个分数的最大值 Fraction greater(Fraction first, Fraction sec){ //检查结果是否为负,然后 //第二个分数更大,否则第一个更大 int Y = first.nume * sec.deno - first.deno * sec.nume; return (Y > 0) ? first : sec; } int main(){ Fraction first = { 4, 5 }; Fraction sec = { 3, 4 }; Fraction res = greater(first, sec); printf("The greater fraction is: %d/%d\n", res.nume, res.deno); return 0; }
输出结果
如果运行上面的代码,它将生成以下输出-
The greater fraction is: 4/5