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()过滤首尾空格(关键是尾部空格),结果: ...
- BZOJ 3876 [AHOI/JSOI2014]支线剧情 (最小费用可行流)
题面:洛谷传送门 BZOJ传送门 题目大意:给你一张有向无环图,边有边权,让我们用任意条从1号点开始的路径覆盖这张图,需要保证覆盖完成后图内所有边都被覆盖至少一次,求覆盖路径总长度的最小值 最小费用可 ...
- Hive sql
1.DDL操作 1.1 建表 CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_ ...
- ubuntu的LAMP环境搭建
服务器的搭建,经典组合:LAMP(Linux+Apache+Mysql+PHP) unbuntu源更新:sudo apt update 更新:sudo apt upgrade 安装Apache:sud ...
- TP框架 mysql子查询
一些比较复杂的业务关系,用子查询解决. 比循环便利要好的多哈. 比如下面这句 select 和where in 语句都用了子查询. 因为父查询在select里,所以用了select的字段当子查询的条件 ...
- 【codeforces 731E】Funny Game
[题目链接]:http://codeforces.com/contest/731/problem/E [题意] 两个人轮流玩游戏; 取序列中的前k(k>1)个数字,拿出来; 把这k个数字全部加起 ...
- poj 2139 flord水题
读懂题意就简单了 #include<stdio.h> #define inf 999999999 #define N 310 int f[N]; int map[N][N]; int ma ...
- 0108MySQL集群搭建详解(三种结点分离)
转自http://blog.csdn.net/yang1982_0907/article/details/20716845,感谢博主 本文将搭建一个最简化的MySQL Cluster系统,配置方法中的 ...
- BigInteger类(高精度整型)
位置:java.math.BigInteger 作用:提供高精度整型数据类型及相关操作 一.基本介绍 BigInteger为不可变的任意精度的整数(对象创建后无法改变,每次运算均会产生一个新的对象). ...
- 洛谷—— P3119 [USACO15JAN]草鉴定Grass Cownoisseur || BZOJ——T 3887: [Usaco2015 Jan]Grass Cownoisseur
http://www.lydsy.com/JudgeOnline/problem.php?id=3887|| https://www.luogu.org/problem/show?pid=3119 D ...