程序比较C中的两个分数
给定两个具有分子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
解决问题的方法如下
//baadmelikhunga
算法
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
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短