bzoj1684[Usaco2005 Oct]Close Encounter*
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                bzoj1684[Usaco2005 Oct]Close Encounter*
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                bzoj1684[Usaco2005 Oct]Close Encounter
題意:
找一個分數它最接近給出一個分數。你要找的分數的分子分母的范圍在1..32767。
題解:
枚舉所求分數的分子,用其乘上給出分數得到一個浮點數分母,比較分母向上/下取整所得分數與答案比較。
代碼:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5 #define inc(i,j,k) for(int i=j;i<=k;i++) 6 #define maxn 10010 7 using namespace std; 8 9 inline int read(){ 10 char ch=getchar(); int f=1,x=0; 11 while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();} 12 while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); 13 return f*x; 14 } 15 int n,d,ansa,ansb; double ans; 16 int main(){ 17 n=read(); d=read(); ans=10; 18 inc(i,1,32767){ 19 int j=n*i/d,k=j+1; 20 if(fabs((double)k/(double)i-(double)n/(double)d)<ans) 21 ans=fabs((double)k/(double)i-(double)n/(double)d),ansa=k,ansb=i; 22 if(j*d!=n*i&&fabs((double)j/(double)i-(double)n/(double)d)<ans) 23 ans=fabs((double)j/(double)i-(double)n/(double)d),ansa=j,ansb=i; 24 } 25 printf("%d %d",ansa,ansb); return 0; 26 }?
20160927
轉載于:https://www.cnblogs.com/YuanZiming/p/5966618.html
總結
以上是生活随笔為你收集整理的bzoj1684[Usaco2005 Oct]Close Encounter*的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: addroid 自定义布局
- 下一篇: 结对编程-黄金点游戏
