HDU 5912 Fraction
题目来源:2016 CCPC 长春站
题意:青蛙先生想计算一个式子的值,输入两个数列a[],b[]求出最后的分子和分母
思路:一开始看到这个图片首先想到的是递归实现,递归部分始终计算的是右下部分
/*************************************************************************
> File Name: A.cpp
> Author: WArobot
> Mail: 768059009@qq.com
> Created Time: 2017年04月15日 星期六 21时10分45秒
************************************************************************/
#include<bits/stdc++.h>
#include<cstdio>
using namespace std;
long long n,a[100],b[100];
long long p,q;
void fun(long long step,long long z,long long m){
long long t1 = m*b[step-1] , t2 = m*a[step-1]+z;
if(step==1) {
p = t1 ; q = t2 ;
return;
}
fun(step-1, t1 , t2 );
}
int gcd(long long a,long long b){
return b==0?a:gcd(b,a%b);
}
int main(){
int t,kase = 0;
scanf("%d",&t);
while(t--){
scanf("%lld",&n);
for(int i=0;i<n;i++) scanf("%lld",a+i);
for(int i=0;i<n;i++) scanf("%lld",b+i);
p = q = 0;
if(n<=1) p = b[0] , q = a[0];
else fun(n-1,b[n-1],a[n-1]);
int d = gcd(p,q);
if(d!=0){
p /= d; q /= d;
}
printf("Case #%d: %lld %lld\n",++kase,p,q);
}
return 0;
}
后来一想为啥非得要用递归做......
/*************************************************************************
> File Name: A2.cpp
> Author: WArobot
> Mail: 768059009@qq.com
> Created Time: 2017年04月16日 星期日 20时35分55秒
************************************************************************/
#include<bits/stdc++.h>
using namespace std;
int t , kase = 0 , n;
int a[100] , b[100];
int p,q; // p为分子 q为分母
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
void solve(){
p = b[n-1] , q = a[n-1];
for(int i=n-2;i>=0;i--){
int t1 = p , t2 = q;
p = t2*b[i];
q = t2*a[i] + t1;
}
int d = gcd(p,q);
p /= d; q /= d;
}
int main(){
int kase = 0;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%d",a+i);
for(int i=0;i<n;i++) scanf("%d",b+i);
solve();
printf("Case #%d: %d %d\n",++kase,p,q);
}
return 0;
}
HDU 5912 Fraction的更多相关文章
- HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))
Fraction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU 5912 Fraction(模拟——分子式化简求解)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5912 Problem Description Mr. Frog recently studied h ...
- HDU 5912 Fraction (模拟)
题意:给定一个分式,让你化简. 析:从分母开始模拟分数的算法,最后约分. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400000 ...
- HDU 5912 Fraction(模拟)
Problem Description Mr. Frog recently studied how to add two fractions up, and he came up with an ev ...
- hdu 5912(迭代+gcd)
Fraction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- 2016CCPC长春 - B/D/F/H/I/J/K - (待补)
目录: B - Fraction D - Triangle F - Harmonic Value Description H - Sequence I I - Sequence II B题:HDU 5 ...
- hdu 6223 Infinite Fraction Path
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6223 题意:给定长度为n的一串数字S,现在要按照一种规则寻找长度为n的数字串,使得该数字串的字典序最大 ...
- HDU - 3556 - Continued Fraction
先上题目: Continued Fraction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Jav ...
- ACM-ICPC 2017 沈阳赛区现场赛 G. Infinite Fraction Path && HDU 6223(BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6223 参考题解:https://blog.csdn.net/qq_40482495/article/d ...
随机推荐
- 前端异步编程之Promise和async的用法
传统的异步解决方案采用回调函数和事件监听的方式,而这里主要记录两种异步编程的新方案: ES6的新语法Promise ES2017引入的async函数 Generator函数(略) Promise的含义 ...
- 实现el-dialog的拖拽,全屏,缩小功能
基于el-dialog, 封装了一下.,实在懒得写,所以直接把代码 粘出来了 大概粘了一下效果.自己体会把. 组件使用 <el-dialog v-dialogDrag ref="xhz ...
- 编译htop
git clone https://github.com/hishamhm/htop cd htop ./autogen.sh ./configure make make install
- 【codeforces 723E】One-Way Reform
[题目链接]:http://codeforces.com/contest/723/problem/E [题意] 给你一个无向图; 让你把这m条边改成有向图; 然后使得出度数目等于入度数目的点的数目最多 ...
- spring 整合freemarker 实现模板继承
<!--freemarker 配置--> <bean id="freemarkerConfig" class="org.springframework. ...
- HDU 4352
#include <iostream> #include <cstdio> #include <cmath> #include <string.h> # ...
- VMWare虚拟机下为Ubuntu 12.04.1网络设置(NAT方式)
NAT方式: 虚拟机能够上外网,能够訪问宿主计算机所在网络的其它计算机(反之不行). 第一步:设置虚拟机vmware网络參数 (1)打开虚拟机,选择菜单"编辑">" ...
- exFAT格式
买了一个64GB的T卡,发如今Windows XP上格式化."文件系统"仅仅有exFAT选项. 用这个exFAT格式化还失败了. 给XP打上KB955704补丁,能够用exFAT格 ...
- SDUT 1225-编辑距离(串型dp)
编辑距离 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 如果字符串的基本操作仅为:删除一个字符.插入一个字符和将一个字符改动 ...
- mysql基础综述(四)
1.数据库的简单介绍 1.1 数据库,就是一个文件系统,使用标准sql对数据库进行操作 1.2 常见的数据库 oracle 是oracle公司的数据库,是一个收费的大型的数据库 DB2,是IBM公司 ...