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 ...
随机推荐
- mysql 与 memcache 字段名后面有空格时会产生什么问题(转)
同事下午遇到一问题,MySQL 和 Memcached 对于同一个key,不能对应起来.最终原因是:PHP将key写入MySQL数据库之前,没有经过trim()过滤首尾空格(关键是尾部空格),结果: ...
- Project Euler 39 Integer right triangles( 素勾股数 )
题意:若三边长 { a , b , c } 均为整数的直角三角形周长为 p ,当 p = 120 时,恰好存在三个不同的解:{ 20 , 48 , 52 } , { 24 , 45 , 51 } , ...
- linux github 添加ssh
1.本地生成key, xxx 是github 的账号, 执行下面命令一路下一步 ssh-keygen -t rsa -C "xxx" 2.复制下面的public key 到git ...
- 我的第一个arcgis地图应用
步骤: 1.设置一个基本的html文档 <!DOCTYPE html> <html> <head> <meta http-equiv="Conten ...
- c++之vector使用
Vector是向量模板,C++ STL之一(本质上是一个动态数组).vector是一个动态生长的数组,一开始vector为空时,会给一个初始化的容量 (就是允许的添加个数并申请好内存),当往添加的元素 ...
- web.xml 中context-param元素
context-param元素含有一对参数名和参数值,用作应用的ServletContext上下文初始化参数.参数名在整个Web应用中必须是惟一的 param-name 子元素包含有参数名,而para ...
- HDU 4355
只能说感觉是三分吧,因为两端值肯定是最大的,而中间肯定存在一点使之最小,呃,,,,猜 的... #include <iostream> #include <cstdio> #i ...
- Mysql存储过程包括事务,且传入sql数据运行
有这样一个需求.要求在mysql存储过程中使用到事务,并且运行的是动态的sql语句 代码例如以下: BEGIN DECLARE in_data TEXT; /** 标记是否出错 */ DECLARE ...
- ListView的adapter中getView方法一直调用
当ListView的高度不定(比如重写ListView搞成可自己主动的扩展的ListView)或 ListView嵌套在SrollView(高度不定)中,listView中的一个item元素改变会使得 ...
- UVALive 3027 Corporative Network 带权并查集
Corporative Network A very big corporation is developing its corporative networ ...