1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > c 语言输入n个数求和 c++---天梯赛---N个数求和

c 语言输入n个数求和 c++---天梯赛---N个数求和

时间:2018-09-30 09:58:44

相关推荐

c 语言输入n个数求和 c++---天梯赛---N个数求和

★题目:

★难点:要求只能以有理数和分数去输出结果。

★分析:可以对输入的数据进行通分处理,随后把结果按格式输出。

★代码:

#include

#include

#include

using namespace std;

long gy(long n,long m) //递归求最大公约数

{

long temp;

if(n

if(m==)

return n;

else

return gy(m,n%m);

}

int main()

{

long n,i,a,b,c,d,x,y,t;

cin>>n;

scanf("%ld/%ld",&a,&b);

for(int i=;i

{

scanf("%ld/%ld",&c,&d);

if(a!=) //对输入的第一个数进行分析

{

x=a*d+c*b;

y=b*d;

t=gy(abs(x),y);

a=x/t;

b=y/t;

} //实现通分操作

else

{

x=c;

y=d;

t=gy(abs(x),y);

a=x/t;

b=y/t;

} //对首位数为0的式子实现通分

}

if(abs(a)>b&&a%b!=)

{

cout<

}

else if(a%b==) //包含a==0的情况

{

cout<

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。