分数拆分( Fractions Again, UVA 10976)-ACM
It is easy to see that for every fraction in the form (k > 0), we can always find two positive integers x and y,x ≥ y, such that:
.
Now our question is: can you write a program that counts how many such pairs of x and y there are for any givenk?
Input
Input contains no more than 100 lines, each giving a value of k (0 < k ≤ 10000).
Output
For each k, output the number of corresponding (x, y) pairs, followed by a sorted list of the values of x and y,
as shown in the sample output.
Sample Input
2
12
Sample Output
2
1/2 = 1/6 + 1/3
1/2 = 1/4 + 1/4
8
1/12 = 1/156 + 1/13
1/12 = 1/84 + 1/14
1/12 = 1/60 + 1/15
1/12 = 1/48 + 1/16
1/12 = 1/36 + 1/18
1/12 = 1/30 + 1/20
1/12 = 1/28 + 1/21
1/12 = 1/24 + 1/24
这个题目做起来不难,难点在数值精度到问题上,我是参照了这为朋友到讲解
http://www.2cto.com/kf/201111/111420.html
/*
* FractionAgain.cpp
*
* Created on: 2014-8-27
* Author: root
*/ #include <iostream>
#include <vector>
#include <string>
#include <cstdio>
using namespace std;
bool isInt(double n){
double c = n-(int)n;
if(n >= 0){
if( c < 1e-15 || c < -0.999999999999999 ) {
//单精度对应1e-6和6个9,双精度对应1e-15和15个9
return true;
}
else{
return false;
}
}
else{
if( -c < 1e-15 || -c < -0.999999999999999 ){
return true;
}
else{
return false;
}
} } int main(){ long k ;
vector<string> ans;
char str[100];
while((cin>>k) && k != 0){
long max = k << 1;
int y;
ans.clear();
for ( y = k + 1; y <= max; ++y) {
double x = (double)(k*y)/(y - k);
if(isInt(x)){
sprintf(str,"1/%ld = 1/%d + 1/%d\n",k,(int)x,y);
ans.push_back(str);
} }
int size = ans.size();
cout<<size<<endl;
for (y = 0;y < size;y++) {
cout<<ans[y];
}
} return 0;
}
分数拆分( Fractions Again, UVA 10976)-ACM的更多相关文章
- 分数拆分(Fractions Again?!, UVa 10976)
题目链接:https://vjudge.net/problem/UVA-10976 It is easy to see that for every fraction in the form 1k(k ...
- Fractions Again?! UVA - 10976
It is easy to see that for every fraction in the form 1k(k > 0), we can always find two positive ...
- UVA10976 分数拆分 Fractions Again?! 题解
Content 给定正整数 \(k\),找到所有的正整数 \(x \geqslant y\),使得 \(\frac{1}{k}=\frac{1}{x}+\frac{1}{y}\). 数据范围:\(0& ...
- 暴力枚举 UVA 10976 Fractions Again?!
题目传送门 /* x>=y, 1/x <= 1/y, 因此1/k - 1/y <= 1/y, 即y <= 2*k */ #include <cstdio> #inc ...
- 洛谷P1458 顺序的分数 Ordered Fractions
P1458 顺序的分数 Ordered Fractions 151通过 203提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 输入一个 ...
- NYOJ 66 分数拆分
分数拆分 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 现在输入一个正整数k,找到所有的正整数x>=y,使得1/k=1/x+1/y. 输入 第一行输入一个 ...
- UVA 725 UVA 10976 简单枚举
UVA 725 题意:0~9十个数组成两个5位数(或0开头的四位数),要求两数之商等于输入的数据n.abcde/fghij=n. 思路:暴力枚举,枚举fghij的情况算出abcde判断是否符合题目条件 ...
- 洛谷——P1458 顺序的分数 Ordered Fractions
P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1, ...
- nyoj_66_分数拆分_201312012122
分数拆分 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 现在输入一个正整数k,找到所有的正整数x>=y,使得1/k=1/x+1/y. 输 ...
- 洛谷 P1458 顺序的分数 Ordered Fractions
P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1, ...
随机推荐
- .net code injection
.NET Internals and Code Injection http://www.ntcore.com/files/netint_injection.htm Windows Hooks in ...
- java传递json数据到前台jsp
在数据传输流程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键.例如: JSON字符串: var str1 = '{ &q ...
- 某返利网站admin目录index.php文件混淆加密算法分析
---恢复内容开始--- 文件已经加密,可以在此下载:index.php 文件内容打开大概如此: 简单字符替换之后,发现字符串用base64_decode仍无法解码. 找到一个解码网站:找源码 解码后 ...
- The Boost C++ Libraries
" ...one of the most highly regarded and expertly designed C++ library projects in the world.&q ...
- HDU 1025 Constructing Roads In JGShining's Kingdom (DP)
Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which ...
- android 05 桢布局:FrameLayout 网格布据 GridLayout
xml文件: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android: ...
- 在LINUX中跟踪函数调用----http://stackoverflow.com/
http://stackoverflow.com/questions/311840/tool-to-trace-local-function-calls-in-linux I am looking f ...
- 使用navicat 11 出现不能返回存储过程结果的问题
问题: 使用navicat 11 调试存储过程,select返回结果,总是不能返回. 原因: 经google发现,navicat仅支持返回10个resultset,超过则不现实. 解决方法: 减少存储 ...
- Objective-C--Runtime机制
个人理解: 简单来说,Objective-C runtime是一个实现Objective-C语言的C库.对象可以用C语言中的结构体表示,而方法(methods)可以用C函数实现.事实上,他们 差不多也 ...
- 浏览器中JavaScript执行原理
本章我们讨论javascript在浏览器中是如果工作的,包括:下载.解析.执行的全过程.javascript的这些讨人嫌的地方我们是知道的: i.需要串行下载 ii.需要解析 iii.需要串行执行 而 ...