5-5 有理数均值
2016-07-13 20:07
253 查看
5-5 有理数均值 (20分)
本题要求编写程序,计算N个有理数的平均值。
这道题没什么好说的,每次把分母乘起来即可,注意保存的时候要保存最小公倍数!~(╬ ̄皿 ̄)=○#( ̄#)3 ̄) 不然会爆( >﹏<。)~我就是这么死的
(虽然前面莫名死的每次都有好好化简!)
题目连接:https://pta.patest.cn/pta/test/1287/exam/4/question/17648;
全过代码:
然后这里有一个每次都化简的,不知道为什么总是第三个测试点过不了(っ╥╯﹏╰╥c)可以帮忙看下的
#include <iostream>
#include <bits/stdc++.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
long long grad(long long x,long long y)
{
if(x<0)x*=-1;
long long r;
while(y)
{
r=x%y;
x=y;
y=r;
}
return x;
}
long long f(int x,int y)
{
long long sum=x*y;
return sum/grad(x,y);
}
int main()
{
int n,m;
int i,j;
scanf("%d",&n);
long long x1,x2,sum,x3,x4;
long long x,p;
scanf("%lld/%lld",&x1,&x2);
if(n==1)
{
p=grad(x1,x2);
x1/=p;x2/=p;
if(x2<0)
{
x1*=-1;
x2*=-1;
}
if(x2!=1)
printf("%lld/%lld",x1,x2);
else printf("%lld",x1);
}
else
{
for(i=1;i<n;++i)
{
scanf("%d/%d",&x3,&x4);
x=f(x4,x2);
x1*=x/x2;x3*=x/x4;
x1+=x3;
p=grad(x1,x);
x1=x1/p;
x2=x/p;
}
x2*=n;
p=grad(x1,x2);
x1/=p;x2/=p;
if(x2<0)
{
x1*=-1;
x2*=-1;
}
if(x2==1)printf("%lld\n",x1);
else
{
printf("%lld/%lld",x1,x2);
}
}
return 0;
}
本题要求编写程序,计算N个有理数的平均值。
输入格式:
输入第一行给出正整数N(≤\le≤100);第二行中按照a1/b1 a2/b2 …的格式给出N个分数形式的有理数,其中分子和分母全是整形范围内的整数;如果是负数,则负号一定出现在最前面。
输出格式:
在一行中按照a/b的格式输出N个有理数的平均值。注意必须是该有理数的最简分数形式,若分母为1,则只输出分子。
输入样例1:
4 1/2 1/6 3/6 -5/10
输出样例1:
1/6
输入样例2:
2 4/3 2/3
输出样例2:
1
这道题没什么好说的,每次把分母乘起来即可,注意保存的时候要保存最小公倍数!~(╬ ̄皿 ̄)=○#( ̄#)3 ̄) 不然会爆( >﹏<。)~我就是这么死的
(虽然前面莫名死的每次都有好好化简!)
题目连接:https://pta.patest.cn/pta/test/1287/exam/4/question/17648;
全过代码:
#include <iostream> #include <bits/stdc++.h> int a[110],b[110]; #include <iostream> #include <bits/stdc++.h> using namespace std; long long gcd(long long x,long long y) { //if(x<0)x*=-1; return y?gcd(y,x%y):x;(新技能get√) } int main() { int n; long long sum=1; long long x=0; int i; scanf("%d",&n); scanf("%d/%d",&a[0],&b[0]); sum=b[0]; for(i=1;i<n;++i) { scanf("%d/%d",&a[i],&b[i]); sum=sum*b[i]/gcd(sum,b[i]); } for(i=0;i<n;++i) { x+=a[i]*(sum/b[i]); } sum*=n; long long p=gcd(x,sum); x/=p;sum/=p; if(sum==1)printf("%lld",x); else printf("%lld/%lld",x,sum); return 0; }
然后这里有一个每次都化简的,不知道为什么总是第三个测试点过不了(っ╥╯﹏╰╥c)可以帮忙看下的
#include <iostream>
#include <bits/stdc++.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
long long grad(long long x,long long y)
{
if(x<0)x*=-1;
long long r;
while(y)
{
r=x%y;
x=y;
y=r;
}
return x;
}
long long f(int x,int y)
{
long long sum=x*y;
return sum/grad(x,y);
}
int main()
{
int n,m;
int i,j;
scanf("%d",&n);
long long x1,x2,sum,x3,x4;
long long x,p;
scanf("%lld/%lld",&x1,&x2);
if(n==1)
{
p=grad(x1,x2);
x1/=p;x2/=p;
if(x2<0)
{
x1*=-1;
x2*=-1;
}
if(x2!=1)
printf("%lld/%lld",x1,x2);
else printf("%lld",x1);
}
else
{
for(i=1;i<n;++i)
{
scanf("%d/%d",&x3,&x4);
x=f(x4,x2);
x1*=x/x2;x3*=x/x4;
x1+=x3;
p=grad(x1,x);
x1=x1/p;
x2=x/p;
}
x2*=n;
p=grad(x1,x2);
x1/=p;x2/=p;
if(x2<0)
{
x1*=-1;
x2*=-1;
}
if(x2==1)printf("%lld\n",x1);
else
{
printf("%lld/%lld",x1,x2);
}
}
return 0;
}
相关文章推荐
- android stuido常见问题查询网址(此贴不定期更新)
- 【结论】升序逆序对
- unity3D引擎:2D游戏自动瞄准算法实现
- gcd
- javascript原理系列— new的原理
- 将 Web 应用性能提高十倍的10条建议
- 设计模式之c++实现(三)
- android自定义 数字键盘2
- Linux基础(9)文本处理三剑客之grep
- 转:理解 Linux 的硬链接与软链接
- windows apache开启url rewrite
- 关于UITextFiled,UIlabel,UIBUtton相关设置,边框设置和字体设置
- day_3-acm贪心(sort结构体排序)
- python列表常见方法
- Ex2010-18 Exclude email address/recipient from Exchange 2010 dynamic distribution group
- C语言的值传递、地址传递、引用传递
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) E DFS
- 网页设计的字体
- 判断一个年份是否是闰年Java初级入门
- HDU2521 求因数个数