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 ;
}

最新文章

  1. DX9入门笔记1-D3D初始化
  2. 《zw版&#183;Halcon-delphi系列原创教程》 Halcon分类函数007, match,图像匹配
  3. Nginx 单机百万QPS环境搭建
  4. 2.TCP_IP互联线缆_TCP_UDP报文抓包详解
  5. postgresql 关闭自动提交
  6. DOM-判断元素节点类型
  7. OC的类的构造方法
  8. Java学习笔记之:java环境搭建
  9. C#面向对象——成员变量及封装
  10. UILabel字体加粗等属性和特效
  11. php基础知识--文件操作
  12. Network view
  13. webgoat——XSS
  14. 【翻译】使用新的Sencha Cmd 4命令app watch
  15. install virtual enviroment on windows
  16. 【python小练】0010
  17. OpenSSL-Win32,rsa,私钥,公钥,1024,2048
  18. 黄聪:OTP动态密码_Java代码实现
  19. win10 任务栏锁定,win键没反应
  20. c++11 强类型枚举

热门文章

  1. Python PEP8标准
  2. 深入浅出Cocoa 之动态创建类【转】
  3. spss命令数据整理中compute与record命令的区别
  4. CSS浏览器兼容解决方案
  5. sql —— check
  6. QT_OPENGL-------- 5.model
  7. MyBatis动态SQL(一)
  8. @NOIP2018 - D1T2@ 货币系统
  9. HZOJ 回家
  10. 模板—中国剩余定理+拓展GCD