分数拆分( 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, ...
随机推荐
- CentOS添加swap分区
在多个VPS上尝试. 1.进入目录 cd /var/ 2.获取要增加的SWAP文件块(这里以1GB为例) dd if=/dev/zero of=swapfile bs=1024 count=10383 ...
- C#在ASP.NET4.5框架下的首次网页应用
运行效果预览: 先看实践应用要求: 1.编写一个函数,用于计算1!+2!+3!+4!+5!,在控制台或页面输出运行结果. 2.在控制台或页面输出九九乘法表. 3.输入10个以内的整数,输出该组整数的降 ...
- spark 启动时候报 Unable to load native-hadoop library for your platform 警告
hadoop2.6.4 jdk1.8 spark2.0.1 方案1: 在spark的conf目录下,修改spark-env.sh文件加入LD_LIBRARY_PATH环境变量,值为hadoop的nat ...
- U3D C# 实现AS3事件机制
写了很多年的AS3,最近接触U3D感觉事件机制没AS3的爽.咬紧牙关一鼓作气 基于C# 的委托实现了一版.废话不多说上干货. EventDispatcher代码如下: using UnityEngin ...
- 读书笔记-《基于Oracle的SQL优化》-第一章-3
优化器: 1.优化器的模式: 用于决定在Oracle中解析目标SQL时所用优化器的类型,以及决定当使用CBO时计算成本值的侧重点.这里的“侧重点”是指当使用CBO来计算目标SQL各条执行路径的成本值时 ...
- 跨服务器修改数据 分类: SQL Server 2014-08-21 21:24 316人阅读 评论(0) 收藏
说明: 两个服务器: 192.168.0.22 A 192.168.0.3 B 数据库备份在A上 数据库在B上 在A上写: exec sp_addlinkedserver 'ITSV ...
- python 多级菜单 纯循环与分支
源代码: dic1 = {'湖南':{'衡阳':{'珠晖区':'湖南工学院'},'湘潭':{'晖晖':'啦啦'}}, '北京':{'朝阳': {"德玛:北京大学"}}}print( ...
- mac下修改mysql的默认字符集为utf8
1,检查默认安装的mysql的字符集 mysql> show variables like '%char%'; +--------------------------+------------- ...
- Latex笔记(参考文献) 分类: LaTex 2014-11-08 17:41 239人阅读 评论(0) 收藏
当你用LaTeX来写文档,在管理参考文献时,你可能会用到bibtex, 也许你会嫌麻烦,会选择用 \begin{thebibliography}{10} \bibitem xxxx \bibitem ...
- [Javascript] Manage Application State with Immutable.js
Learn how Immutable.js data structures are different from native iterable Javascript data types and ...
.