pat A1081
题意:
输入:
1.输入n个数字
2.后面紧接着输分数入n个
输出:
输出n个分数之和,注意输入方式按照下图中所示的分数形式输出
/注意这里欠张notability上的图
解题思路:
参考代码:
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
/*
解题思路:
1.gcd辗转相除函数
2.设置分数结构Fraction
3.reduction化简:1.分母为负数 2.分子为0,设置分母为1 3.分子,分母同除以最大公约数
4.add将两个分数相加,十分简单
5.showResult()输出: 1.分母为1,直接输出分子 2.分子绝对值>分母,依次输出除数,分子带绝对值求余分母,输出分母
6.main中,只要设置两个变量sum,temp就行,注意sum.up=0,sum.down=1;最后直接输出
*/
ll gcd(ll a, ll b)
{
return !b?a:gcd(b,a%b); //辗转相除法:注意当b为0时,返回的是a
}
struct Fraction
{
ll up,down; //设置分子,分母
};
Fraction reduction(Fraction reduct) //化简:1.分子<0,分子,分母全部换为相反数;2.如果分子为0,则直接将分母设置为1;否则,取分子分母的最大公约数,进行化简
{
if(reduct.down<0) //如果分母<0,就把分子分母都设置为相反数
{
reduct.up=-reduct.up;
reduct.down=-reduct.down;
}
if(reduct.up==0) //如果分子为0,则设置分母为1
reduct.down=1;
else
{
//如果分子不为0,则直接约分
int d=gcd(abs(reduct.up),abs(reduct.down));
reduct.up=reduct.up/d;
reduct.down=reduct.down/d;
}
return reduct;
}
Fraction add(Fraction f1,Fraction f2) //分数相加
{
Fraction result;
result.up=f1.up*f2.down+f2.up*f1.down;
result.down=f1.down*f2.down;
return reduction(result); //最后返回化简后的分数
}
void showResult(Fraction r)
{
r=reduction(r); //先化简
if(r.down==1)
printf("%lld\n",r.up); //如果分母为1,直接输出整数
else if(abs(r.up)>r.down) //如果分子的绝对值>分母,将分子设置为绝对值因为如果当分母为负数时,再化简过程中将负号给了分子,所以在比较时,给分子带绝对值,计算整数的部分时不用带绝对值,算子时带绝对值,
{
printf("%lld %lld/%lld\n",r.up/r.down,abs(r.up)%r.down,r.down);
}
else
{ //直接输出真分数
printf("%lld/%lld\n",r.up,r.down);
}
}
int main()
{
int n;
Fraction sum,temp;
cin >> n;
sum.up=0,sum.down=1; //和的分子为0,分母为1
for(int i=0; i<n; i++)
{
scanf("%lld/%lld",&temp.up,&temp.down);
sum=add(sum,temp);
}
showResult(sum);
return 0;
}
pat A1088
题意:
输入:
输出:
解题思路:
参考代码:
#include<iostream>
#include<algorithm>
using namespace std;
/*
解题思路:
1.古老的传统智慧,gcd+struct Fraction+Fraction reduction+Fraction add+void showResult
2.针对加减乘除分别输出,如果是负数套括号,如果对于除法中,分母为0,则输出Inf
*/
typedef long long ll;
const int Inf=1000000l;
int gcd(ll a, ll b)
{
return !b?a:gcd(b,a%b);
}
struct Fraction
{
ll up,down;
};
Fraction reduction(Fraction reduct)
{
if(reduct.down<0)
{
reduct.up=-reduct.up;
reduct.down=-reduct.down;
}
if(reduct.up==0)
reduct.down=1;
else
{
int d=gcd(abs(reduct.up),abs(reduct.down));
reduct.up/=d;
reduct.down/=d;
}
return reduct;
}
Fraction add(Fraction f1,Fraction f2)
{
Fraction result;
result.up=f1.up*f2.down+f2.up*f1.down;
result.down=f1.down*f2.down;
return reduction(result);
}
Fraction jian(Fraction f1,Fraction f2)
{
Fraction result;
result.up=f1.up*f2.down-f2.up*f1.down;
result.down=f1.down*f2.down;
return reduction(result);
}
Fraction cheng(Fraction f1,Fraction f2)
{
Fraction result;
result.up=f1.up*f2.up;
result.down=f1.down*f2.down;
return reduction(result);
}
Fraction chu(Fraction f1,Fraction f2)
{
Fraction result;
result.up=f1.up*f2.down;
result.down=f1.down*f2.up;
return reduction(result);
}
void showResult(Fraction r)
{
r=reduction(r);
if(r.down==1)
printf("%lld",r.up);
else if(abs(r.up)>r.down)
{
printf("%lld %lld/%lld",r.up/r.down,abs(r.up)%r.down,r.down);
}
else
printf("%lld/%lld",r.up,r.down);
}
int main()
{
Fraction temp1,temp2,c,n,m,b;
scanf("%lld/%lld %lld/%lld",&temp1.up,&temp1.down,&temp2.up,&temp2.down);
c=add(temp1,temp2);
n=jian(temp1,temp2);
m=cheng(temp1,temp2);
b=chu(temp1,temp2);
//输出加法
if(temp1.up<0)
{
cout << "(";
showResult(temp1);
cout << ")";
}
else
showResult(temp1);
cout << " + ";
if(temp2.up<0)
{
cout << "(";
showResult(temp2);
cout << ")";
}
else
showResult(temp2);
cout << " = ";
if(c.up<0)
{
cout << "(";
showResult(c);
cout << ")";
}
else
showResult(c);
cout << endl;
//输出减法
if(temp1.up<0)
{
cout << "(";
showResult(temp1);
cout << ")";
}
else
showResult(temp1);
cout << " - ";
if(temp2.up<0)
{
cout << "(";
showResult(temp2);
cout << ")";
}
else
showResult(temp2);
cout << " = ";
if(n.up<0)
{
cout << "(";
showResult(n);
cout << ")";
}
else
showResult(n);
cout << endl;
//输出乘法
if(temp1.up<0)
{
cout << "(";
showResult(temp1);
cout << ")";
}
else
showResult(temp1);
cout << " * ";
if(temp2.up<0)
{
cout << "(";
showResult(temp2);
cout << ")";
}
else
showResult(temp2);
cout << " = ";
if(m.up<0)
{
cout << "(";
showResult(m);
cout << ")";
}
else
showResult(m);
cout << endl;
//输出除法
if(temp1.up<0)
{
cout << "(";
showResult(temp1);
cout << ")";
}
else
showResult(temp1);
cout << " / ";
if(temp2.up<0)
{
cout << "(";
showResult(temp2);
cout << ")";
}
else
showResult(temp2);
cout << " = ";
if(temp2.up==0)
{
cout << "Inf";
return 0;
}
if(b.up<0)
{
cout << "(";
showResult(b);
cout << ")";
}
else
showResult(b);
cout << endl;
}
参考代码2:
#include<iostream>
#include<algorithm>
using namespace std;
/*
解题思路:
1.古老的传统智慧,gcd+struct Fraction+Fraction reduction+Fraction add+void showResult
2.针对加减乘除分别输出,如果是负数套括号,如果对于除法中,分母为0,则输出Inf
*/
typedef long long ll;
const int Inf=1000000l;
int gcd(ll a, ll b)
{
return !b?a:gcd(b,a%b);
}
struct Fraction
{
ll up,down;
};
Fraction reduction(Fraction reduct)
{
if(reduct.down<0)
{
reduct.up=-reduct.up;
reduct.down=-reduct.down;
}
if(reduct.up==0)
reduct.down=1;
else
{
int d=gcd(abs(reduct.up),abs(reduct.down));
reduct.up/=d;
reduct.down/=d;
}
return reduct;
}
Fraction add(Fraction f1,Fraction f2)
{
Fraction result;
result.up=f1.up*f2.down+f2.up*f1.down;
result.down=f1.down*f2.down;
return reduction(result);
}
Fraction jian(Fraction f1,Fraction f2)
{
Fraction result;
result.up=f1.up*f2.down-f2.up*f1.down;
result.down=f1.down*f2.down;
return reduction(result);
}
Fraction cheng(Fraction f1,Fraction f2)
{
Fraction result;
result.up=f1.up*f2.up;
result.down=f1.down*f2.down;
return reduction(result);
}
Fraction chu(Fraction f1,Fraction f2)
{
Fraction result;
result.up=f1.up*f2.down;
result.down=f1.down*f2.up;
return reduction(result);
}
void showResult(Fraction r)
{
r=reduction(r);
if(r.up<0) //在这里加入输出的判断如果<0,则带括号,否则不带
cout << "(";
if(r.down==1)
printf("%lld",r.up);
else if(abs(r.up)>r.down)
{
printf("%lld %lld/%lld",r.up/r.down,abs(r.up)%r.down,r.down);
}
else
printf("%lld/%lld",r.up,r.down);
if(r.up<0) //在这里加入输出的判断如果<0,则带括号,否则不带
cout << ")";
}
int main()
{
Fraction temp1,temp2,c,n,m,b;
scanf("%lld/%lld %lld/%lld",&temp1.up,&temp1.down,&temp2.up,&temp2.down);
c=add(temp1,temp2);
n=jian(temp1,temp2);
m=cheng(temp1,temp2);
b=chu(temp1,temp2);
//输出加法
showResult(temp1);
cout << " + ";
showResult(temp2);
cout << " = ";
showResult(c);
cout << endl;
//输出减法
showResult(temp1);
cout << " - ";
showResult(temp2);
cout << " = ";
showResult(n);
cout << endl;
//输出乘法
showResult(temp1);
cout << " * ";
showResult(temp2);
cout << " = ";
showResult(m);
cout << endl;
//输出除法
showResult(temp1);
cout << " / ";
showResult(temp2);
cout << " = ";
if(temp2.up==0)
{
cout << "Inf";
return 0;
}
showResult(b);
cout << endl;
}