dfs
来源:互联网 发布:阿里云企业邮箱购买 编辑:程序博客网 时间:2024/05/18 21:50
#include<bits/stdc++.h>using namespace std;int a[12],b[12];int n;int gcd(int x,int y){ if(y==0) return x; return gcd(y,x%y);}struct Node{ int p,q;};Node jia(int v,int p,int q) //整数加分数{ p=v*q+p; int com=gcd(p,q); p=p/com; q=q/com; Node tmp;tmp.p=p,tmp.q=q; return tmp;}Node chu(int v,int p,int q) //整数除分数{ Node tmp;tmp.p=v*q,tmp.q=p; int com=gcd(tmp.p,tmp.q); tmp.p=tmp.p/com; tmp.q=tmp.q/com; return tmp;}Node dfs(int cen){ if(cen==n) { return jia(a[n-1],b[n],a[n]); //从终止条件推..快 } Node tmp1=dfs(cen+1); Node tmp=chu(b[cen],tmp1.p,tmp1.q); return jia(a[cen-1],tmp.p,tmp.q);}int main(){ int t,kase=1; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) scanf("%d",&b[i]); Node tmp=dfs(1); printf("Case #%d: %d %d\n",kase++,tmp.p,tmp.q); } return 0;}
阅读全文
0 0
- DFS
- DFS
- dfs
- dfs
- dfs
- dfs
- DFS
- DFS
- dfs
- DFS
- DFS
- DFS
- dfs
- DFS
- dfs
- dfs
- dfs
- dfs
- 专访Dan Kohn:阡陌交迭,云原生布局开源生态构建及深度应用
- 乔纳森-艾维
- 2017java面试题
- TypeError: object() takes no parameters
- JQuery DataTables 基本配置(最新实例)
- dfs
- MySQL 导出数据
- Design库-TabLayout属性详解
- 设计师说,我们要在 App 里用十种字体!!!
- 将InputStream转换为byte数组
- 【环境部署】-jenkins下取包部署新版本
- protobuf windows编译和安装
- 学生信息系统的c程序
- CDH基本概念