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 (xy) 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的更多相关文章

  1. 分数拆分(Fractions Again?!, UVa 10976)

    题目链接:https://vjudge.net/problem/UVA-10976 It is easy to see that for every fraction in the form 1k(k ...

  2. 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 ...

  3. UVA10976 分数拆分 Fractions Again?! 题解

    Content 给定正整数 \(k\),找到所有的正整数 \(x \geqslant y\),使得 \(\frac{1}{k}=\frac{1}{x}+\frac{1}{y}\). 数据范围:\(0& ...

  4. 暴力枚举 UVA 10976 Fractions Again?!

    题目传送门 /* x>=y, 1/x <= 1/y, 因此1/k - 1/y <= 1/y, 即y <= 2*k */ #include <cstdio> #inc ...

  5. 洛谷P1458 顺序的分数 Ordered Fractions

    P1458 顺序的分数 Ordered Fractions 151通过 203提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 输入一个 ...

  6. NYOJ 66 分数拆分

    分数拆分 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 现在输入一个正整数k,找到所有的正整数x>=y,使得1/k=1/x+1/y.   输入 第一行输入一个 ...

  7. UVA 725 UVA 10976 简单枚举

    UVA 725 题意:0~9十个数组成两个5位数(或0开头的四位数),要求两数之商等于输入的数据n.abcde/fghij=n. 思路:暴力枚举,枚举fghij的情况算出abcde判断是否符合题目条件 ...

  8. 洛谷——P1458 顺序的分数 Ordered Fractions

    P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1, ...

  9. nyoj_66_分数拆分_201312012122

    分数拆分 时间限制:3000 ms  |           内存限制:65535 KB 难度:1   描述 现在输入一个正整数k,找到所有的正整数x>=y,使得1/k=1/x+1/y.   输 ...

  10. 洛谷 P1458 顺序的分数 Ordered Fractions

    P1458 顺序的分数 Ordered Fractions 题目描述 输入一个自然数N,对于一个最简分数a/b(分子和分母互质的分数),满足1<=b<=N,0<=a/b<=1, ...

随机推荐

  1. [经典] Best Time to Buy and Sell Stock

    这一系列求最优值的问题变种挺多 1. Say you have an array for which the ith element is the price of a given stock on ...

  2. 字符串、十六进制、byte数组互转

    import java.io.ByteArrayOutputStream; public class HexUtil { /** * @param args */ public static void ...

  3. 【Android - 框架】之GreenDao的使用

    上一篇博客([Android - 框架]之ORMLite的使用)中介绍了ORMLite的基本使用,今天我们来研究以下GreenDao的使用. GreenDao和ORMLite一样,都是基于ORM(Ob ...

  4. CSS3动画变形transition

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. HDU2255 奔小康赚大钱【二分图最佳匹配】

    题目链接: http://acm.hdu.edu.cn/showproblem.php? pid=2255 题目大意: 村里要分房子. 有N家老百姓,刚好有N间房子.考虑到每家都要有房住,每家必须分配 ...

  6. Hash表——The Hash table

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include "list.h&q ...

  7. 京东手机webapp商城

    http://bases.wanggou.com/mcoss/mportal/show?tabid=2&ptype=1&actid=1562&tpl=3&pi=1&am ...

  8. Java语言基础(三)

    Java语言基础(三) 一.    补码 (1).之所以有补码是因为要考虑成本 就是造计算机的成本 (2).下面让我们分析一下补码 以四位补码为例 <1> 高位是符号位,它决定其是正数还是 ...

  9. CentOS6安装配置rsh

    基本信息:节点一:ip:192.168.8.166 主机名:hadrtest01节点二:ip:192.168.8.250 主机名:hadrtest02 1.两节点分别安装rsh,rsh-server包 ...

  10. AS【常用插件】

    安装插件,Settings -->[Plugins]-->搜索-->点击install-->重启AS 禁用插件,右侧面板会显示出已经安装的插件列表,取消勾选即可禁用插件 AS插 ...