HDU 5912 Fraction(模拟)
2024-09-06 15:39:16
Problem Description
Mr. Frog recently studied how to add two fractions up, and he came up with an evil idea to trouble you by asking you to calculate the result of the formula below:
As a talent, can you figure out the answer correctly?
Input
The first line contains only one integer T, which indicates the number of test cases.
For each test case, the first line contains only one integer n (n≤8).
The second line contains n integers: a1,a2,⋯an(1≤ai≤10).
The third line contains n integers: b1,b2,⋯,bn(1≤bi≤10).
Output
For each case, print a line “Case #x: p q”, where x is the case number (starting from 1) and p/q indicates the answer.
You should promise that p/q is irreducible.
Sample Input
1
2
1 1
2 3
Sample Output
Case #1: 1 2
题目大意:给定a,b两个长度为n得数组,求出按图示公式计算后的分式的分子分母
思路: 模拟!由于n不大,可以直接从后往前模拟一边,最后求一遍最大公约数即可
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio> using namespace std;
int T,n;
int a[],b[];
int gcd(int c,int d){
if(c==d)return c;
else if(c<d)return gcd(d-c,c);
return gcd(c-d,d);
}
void Swap(int &c,int &d){
c = c^d;
d = c^d;
c = c^d;
}
int main()
{
scanf("%d",&T);
for(int t=;t<=T;t++){
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(int i=;i<=n;i++)scanf("%d",&b[i]);
int fa = a[n],fb = b[n];
for(int i=n-;i>=;i--){
fb = a[i]*fa+fb;
Swap(fa,fb);
fb *= b[i];
}
printf("Case #%d: ",t);
int tmp = gcd(fa,fb);
printf("%d %d\n",fb/tmp,fa/tmp);
}
return ;
}
最新文章
- DX9入门笔记1-D3D初始化
- 《zw版&#183;Halcon-delphi系列原创教程》 Halcon分类函数007, match,图像匹配
- Nginx 单机百万QPS环境搭建
- 2.TCP_IP互联线缆_TCP_UDP报文抓包详解
- postgresql 关闭自动提交
- DOM-判断元素节点类型
- OC的类的构造方法
- Java学习笔记之:java环境搭建
- C#面向对象——成员变量及封装
- UILabel字体加粗等属性和特效
- php基础知识--文件操作
- Network view
- webgoat——XSS
- 【翻译】使用新的Sencha Cmd 4命令app watch
- install virtual enviroment on windows
- 【python小练】0010
- OpenSSL-Win32,rsa,私钥,公钥,1024,2048
- 黄聪:OTP动态密码_Java代码实现
- win10 任务栏锁定,win键没反应
- c++11 强类型枚举