poj 3039 Close Encounter
来源:互联网 发布:人工智能工程师 编辑:程序博客网 时间:2024/06/05 14:19
我的做法是找叉积与输入最小的,其实应该就是1
然后在这些数中找与输入最近的,做法也是线性的,但是我发现比sorting速度还慢。。。sorting 是nlogn的啊。
不过这个做法没有贪心逼近好是真的
import java.io.*;import java.math.BigInteger;import java.util.*;class node{ int a, b; int t1,t2; public node(int x,int y){a=x;b=y;}}class problem{ int a,b; node arr[] = new node[32767]; void solver() throws IOException{ Scanner scan = new Scanner(System.in); a = scan.nextInt(); b = scan.nextInt(); double min = 99999; int count = 0; int n=0,m=0; for(int i=1;i<=32767;i++){ if(i!=1&&i*b%a==0) continue; int f = (int) Math.floor(i*b*1.0/a); int c = (int) Math.ceil(i*b*1.0/a); if(a==1){f-=1;c+=1;} if(f<=32767&&Math.abs(f*a-i*b)<min) min = Math.abs(f*a-i*b); if(c<=32767&&Math.abs(c*a-i*b)<min) min = Math.abs(c*a-i*b); } for(int i=1;i<=32767;i++){ if(i!=1&&i*b%a==0) continue; int f = (int) Math.floor(i*b*1.0/a); int c = (int) Math.ceil(i*b*1.0/a); if(a==1){f-=1;c+=1;} if(f<=32767&&Math.abs(f*a-i*b)==min) {arr[count++]=new node(i,f);} if(c<=32767&&Math.abs(c*a-i*b)==min) {arr[count++]=new node(i,c);} } for(int i=0;i<count;i++){ if(arr[i].a*1.0/arr[i].b - a*1.0/b<min){ min = arr[i].a*1.0/arr[i].b; n=arr[i].a; m = arr[i].b; } } System.out.println(n+" "+m); }}public class Main{ public static void main (String [] args) throws Exception { problem p = new problem(); p.solver(); }}
0 0
- poj 3039 Close Encounter
- poj 3039 Close Encounter 枚举水过
- poj 3039 Close Encounter “追赶法”搜索
- Close Encounter
- bzoj1684 [Usaco2005 Oct]Close Encounter
- 1684: [Usaco2005 Oct]Close Encounter
- [bzoj1684][Usaco2005 Oct]Close Encounter
- bzoj 1684: [Usaco2005 Oct]Close Encounter
- close ()
- close()
- Encounter with Mathematics - 《数学概观》
- Xiaoyi Ma encounter SUCO life
- File Encounter The Error/Warning
- BufferedReader.close() && FileReader.close()
- zoj 2271 Chance to Encounter a Girl
- ZOJ2271 Chance to Encounter a Girl
- Eclipse may encounter problem on Windows 7
- 7 Ways to Defuse a Difficult Encounter
- 华为:中国平安城市发展白皮书
- Dremel made simple with Parquet
- 一个简单网易新闻页面的实现
- Java / Android 基于Http的多线程下载的实现
- transaction completed on session with on_close connection release mode
- poj 3039 Close Encounter
- Android butterknife框架 注解攻略
- ios中的coredata的使用
- MyEclipse的实体关系设计
- Android开发,eclipse的LogCat使用技巧
- JVM GC总结
- C# 30分钟LINQ教程
- TabHost 缓存
- sort colors