Codeforces #172 div2的B题
2013-03-11 10:51
232 查看
B. Nearest Fraction
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given three positive integers x, y, n. Your task is to find the nearest fraction to fraction
whose
denominator is no more than n.
Formally, you should find such pair of integers a, b (1 ≤ b ≤ n; 0 ≤ a) that
the value
is
as minimal as possible.
If there are multiple "nearest" fractions, choose the one with the minimum denominator. If there are multiple "nearest" fractions with the minimum denominator, choose the one with the minimum numerator.
Input
A single line contains three integers x, y, n (1 ≤ x, y, n ≤ 105).
Output
Print the required fraction in the format "a/b" (without
quotes).
Sample test(s)
input
output
input
output
题目意思还是很清楚的,要找一个分数使得它与x/y的差最小,要求分母b<n,b要最小,分子也最小的那个。
昨晚纠结了半天,还是没纠结出来,今天把它a了。。。。。。
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given three positive integers x, y, n. Your task is to find the nearest fraction to fraction
whose
denominator is no more than n.
Formally, you should find such pair of integers a, b (1 ≤ b ≤ n; 0 ≤ a) that
the value
is
as minimal as possible.
If there are multiple "nearest" fractions, choose the one with the minimum denominator. If there are multiple "nearest" fractions with the minimum denominator, choose the one with the minimum numerator.
Input
A single line contains three integers x, y, n (1 ≤ x, y, n ≤ 105).
Output
Print the required fraction in the format "a/b" (without
quotes).
Sample test(s)
input
3 7 6
output
2/5
input
7 2 4
output
7/2
题目意思还是很清楚的,要找一个分数使得它与x/y的差最小,要求分母b<n,b要最小,分子也最小的那个。
昨晚纠结了半天,还是没纠结出来,今天把它a了。。。。。。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; double go(double xy,int a,int b) { double s=((double)a)/((double)b)-xy; if(s>0)return s; else return -s; } int main() { int x,y,n; while(scanf("%d%d%d",&x,&y,&n)!=EOF) { double xy=((double)x)/((double)y); int a,b,j=0; double ss=110005,v; for(int i=1;i<=n;i++) { while(go(xy,j,i)>go(xy,j+1,i))j++; v=go(xy,j,i); if(v<ss) { ss=v; a=j; b=i; } if(ss==0)break; } cout<<a<<"/"<<b<<endl; } return 0; }
相关文章推荐
- Codeforces Round #172 (Div. 2) B. Nearest Fraction
- Codeforces Round #172 (Div. 2)总结
- Codeforces Round #172 (Div. 2)
- Codeforces Round #172 (Div. 2)---D. Maximum Xor Secondary(RMQ + 二分)
- Codeforces Round #172 (Div. 2) B. Nearest Fraction 二分
- Codeforces Round #172 (Div. 2) D. Maximum Xor Secondary 单调栈应用
- Codeforces Round #172 (Div. 2) C.Rectangle Puzzle(几何)
- Codeforces Round #172 (Div. 2) C. Rectangle Puzzle 数学题几何
- Codeforces Round #172 (Div. 2) A题
- Codeforces Round #172 (Div. 1) BMaximum Xor Secondary 单调栈
- Codeforces Round #172 (Div. 2)
- codeforces水题100道 第二十七题 Codeforces Round #172 (Div. 2) A. Word Capitalization (strings)
- NKOJ-2134“最大”异或“次大”【Codeforces Round #172 (Div. 1)】
- Codeforces Round #172 (Div. 2)
- Codeforces Round #426 (Div. 2) B.The Festive Evening+C.The Meaningless Game
- Educational Codeforces Round 38 (Rated for Div. 2) A. Word Correction
- Codeforces Round #154 (Div. 2)——B
- Codeforces Round #264 (Div. 2) A
- Codeforces Round #304 (Div. 2) D. Soldier and Number Game 数论 筛表法
- 【Codeforces Round 323 (Div 2)D】【暴力 脑洞 插入贡献思想】Once Again... 循环节重复T次后的LIS