题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5912

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

Hint

Here are the details for the first sample:
2/(1+3/1) = 1/2

 
Source
题意描述:
给出上述图中的分子式,给出ai和bi
计算该分子形式的最简结果
解题思路:
总体来说还是比较考验数学思维的。
简单实用代入法看一下,例如只有a1,a2和b1,b2,那么结果可以写成b1/(a1+b2/a2)
初始是fz(分子)=b2,fm=a2,经过化简可得结果为b1*fm/(a1*fm+fz),容易得到fz += fm*a[i];和fm *= b[i];只不过fz和fm需要交换一下(仔细验算一下)。
代码实现:
 #include<stdio.h>
#include<algorithm>
using namespace std;
int gcd(int a,int b)
{
return b==? a : gcd(b,a%b);
}
int main()
{
int T,t=,n,i,a[],b[],k;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=;i<=n;i++) scanf("%d",&a[i]);
for(i=;i<=n;i++) scanf("%d",&b[i]);
int fz=b[n],fm=a[n];
for(i=n-;i>=;i--)
{
fz += fm*a[i];
fm *= b[i];
swap(fz,fm);
}
k=gcd(fz,fm);
printf("Case #%d: %d %d\n",t++,fz/k,fm/k);
}
return ;
}
 

HDU 5912 Fraction(模拟——分子式化简求解)的更多相关文章

  1. HDU 5912 Fraction (模拟)

    题意:给定一个分式,让你化简. 析:从分母开始模拟分数的算法,最后约分. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400000 ...

  2. HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))

    Fraction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  3. HDU 5912 Fraction(模拟)

    Problem Description Mr. Frog recently studied how to add two fractions up, and he came up with an ev ...

  4. HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识

    求$G(a,b,n,p) = (a^{\frac {p-1}{2}}+1)(b^{\frac{p-1}{2}}+1)[(\sqrt{a} + \sqrt{b})^{2F_n} + (\sqrt{a} ...

  5. HDU 5912 Fraction

    题目来源:2016 CCPC 长春站 题意:青蛙先生想计算一个式子的值,输入两个数列a[],b[]求出最后的分子和分母 思路:一开始看到这个图片首先想到的是递归实现,递归部分始终计算的是右下部分 /* ...

  6. HDU.2503 a/b + c/d (分式化简)

    a/b + c/d Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  7. HDU 4565 So Easy! 数学 + 矩阵 + 整体思路化简

    http://acm.hdu.edu.cn/showproblem.php?pid=4565 首先知道里面那个东西,是肯定有小数的,就是说小数部分是约不走的,(因为b限定了不是一个完全平方数). 因为 ...

  8. YZOI Easy Round 2_化简(simplify.c/cpp/pas)

    Description 给定一个多项式,输出其化简后的结果. Input 一个字符串,只含有关于字母x 的多项式,不含括号与分式,没有多余的空格. Output 一个字符串,化简后的多项式,按照次数从 ...

  9. 化简复杂逻辑,编写紧凑的if条件语句

    当业务逻辑很复杂,涉及多个条件的真假,或者多种条件下都会执行同一动作时,如何编写紧凑的if语句呢?本文借由一个实际例子,利用数学的布尔逻辑整理条件,最终产生if语句. 问题 在<X3 重聚> ...

随机推荐

  1. HTML5学习知识点

    一.文档问题 1.html5新标签:section.header.footer.nav.aside.blockquote.q.fieldest.figure.address.article.detai ...

  2. CentOS5 可用yum源

    [base] name=CentOS-$releasever - Base #mirrorlist=http://mirrorlist.centos.org/?release=$releasever& ...

  3. Nodejs真.多线程处理

    前言 Threads à gogo 是nodejs 的原生模块,使用这个模块可以让nodejs 具备多线程处理功能 安装方法 npm install threads_a_gogo 下载测试源码 git ...

  4. redis centos启动

    转到redis目录 ./redis-server /usr/java/redis/redis.conf

  5. 纯CSS实现箭头、气泡让提示功能具有三角形图标(简单实例)

    <style type="text/css"> /*向上箭头,类似A,只有三个边,不能指定上边框*/ .arrow-up { width: 0px; height: 0 ...

  6. 通过WebSocket实现一个简单的聊天室功能

    WebSocket WebSocket是一个协议,它是是基于TCP的一种新的网络协议,TCP协议是一种持续性的协议,和HTTP不同的是,它可以在服务器端主动向客户端推送消息.通过这个协议,可以在建立一 ...

  7. Node.js 蚕食计划(四)—— Express + SQL Server 搭建电影网站

    前段时间在慕课网上看了 scott 大神的<node+mongodb建站攻略>课程,按照自己的思路做了一遍,发博客记录一下 一.项目介绍 这个项目是一个简单的电影网站,由首页.详情页.评论 ...

  8. Sp_Lock

    SP_LOCK 其显示信息为: Spid:进程ID号(要发现哪些用户和该spid相连,你就要执行存储过程sp_who) Dbid:数据库ID号(可以在主数据库中的sysdatabases表格中找到它) ...

  9. window.atob()与window.btoa()方法实现编码与解码

    window.atob() 与window.btoa() WindowBase64.atob() 函数用来解码一个已经被base-64编码过的数据.你可以使用 window.btoa() 方法来编码一 ...

  10. 关于linux下的date日期,并以日期给文件命名

    在linux的终端中,我们输入date后会有以下显示: 然后博主也扩展了一下date的基础用法: date + "%-": %y 输出年份的后2位:%Y 输出完整年份 %m 输出月 ...