分数拆分(刘汝佳紫书P183)

枚举,由已知条件推得y大于k,小于等于2K

AC代码:

#include"iostream"
#include"cstring"
using namespace std;
const int maxn=20002;
int a[maxn];
int b[maxn];
int main()
{
int i,y;
int x,f,k;
while(cin>>k&&k)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
f=0;
for(y=k+1;y<=2*k;y++)
{
if(((k*y)%(y-k))==0)
{
a[f]=(k*y)/(y-k);
b[f]=y;
f++;
}
}
cout<<f<<endl;
for(int j=0;j<f;j++)
{
cout<<"1/"<<k<<" = "<<"1/"<<a[j]<<" + "<<"1/"<<b[j]<<endl;
}
}
return 0;
}

疑问(未解决)

同样的代码codeblock运行正常,VC6.0错误

#include"iostream"
#include"cstring"
using namespace std;
const int maxn=10001;
int a[maxn];
int b[maxn];
int main()
{
    double k,i,y;
    int x,f;
    while(cin>>k&&k)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        f=0;
     for(y=k+1;y<=2*k;y++)
     {
         i=1/(1/k-1/y);
         x=i;
         if(x-i==0)
         {
         a[f]=x;
         b[f]=y;
         f++;
         }
     }
     cout<<f<<endl;
     for(int j=0;j<f;j++)
     {
          cout<<"1/"<<k<<" = "<<"1/"<<a[j]<<" + "<<"1/"<<b[j]<<endl;
     }
    }
    return 0;
}

codeblock:

2
2
1/2 = 1/6 + 1/3
1/2 = 1/4 + 1/4
12
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

VC6.0

2
1
1/2 = 1/4 + 1/4
12
4
1/12 = 1/84 + 1/14
1/12 = 1/36 + 1/18
1/12 = 1/28 + 1/21
1/12 = 1/24 + 1/24

时间: 2024-08-11 19:39:54

分数拆分(刘汝佳紫书P183)的相关文章

POJ 1042 Gone Fishing (贪心)(刘汝佳黑书)

Gone Fishing Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 30281   Accepted: 9124 Description John is going on a fishing trip. He has h hours available (1 <= h <= 16), and there are n lakes in the area (2 <= n <= 25) all reachab

今天看刘汝佳的书,用面向对象的重载用算符打大数,自己也打了篇

#include <iostream> #include<cstdio> #include<cstring> #include<cstdlib> using namespace std; #define maxn 1002 struct bign { int len,s[maxn]; bign () { memset(s,0,sizeof(s)); len=1; } string str() const { string res = ""

计算几何模板(刘汝佳本)(转载)

转载自: 计算几何模板(仿照刘汝佳大白书风格) 想想自己一个学期连紫皮都没看完就想自杀 // Geometry.cpp #include <bits/stdc++.h> #define LL long long #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define PI 3.1415926535897932384626 #define EXIT exit(0); #define DEBUG puts(

UVA1625 Color Length(附 刘汝佳代码)

这是刘汝佳<算法竞赛入门经典第二版>的一道例题,只看书上的解释并没有理解,随后结合着代码才理解了. 解题思路:用d[i][j]表示序列1移走i个元素和序列2移走j个元素的最小"代价", 这个代价指的是由那些已经移出的字母合并而来的序列中已经出现但尚未结束的字母对总距离和的贡献.比如说一个合并而来的序列中有两个那样的字母,第一个在这个序列中后面有3个字母,另一个字母后面有2个字母,那么此时的代价就是2+3,表示这两个字母在这种合并情况下至少能为总距离和贡献5,因为随着向该序列

算法竞赛_入门经典_刘汝佳__(2)

1,有几位数字 #include<stdio.h> int main_2_1_digit(){ int n; while(scanf("%d",&n)){ int count = 0; if(n==0) count = 1; while(n){ count++; n/=10; } printf("%d\n",count); } return 0; } 2,三位数的三个数字 #include<stdio.h> int main_2_2_

c++20701除法(刘汝佳1、2册第七章,暴搜解决)

20701除法 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述     输入正整数n,按从小到大的顺序输出所有满足表达式abcde/fghij=n的abcde和fghij,其中a~j恰好为数字0~9的一个排列. 如果没有符合题意的数,输出0.本题所说的五位数可以包括前导0的,如01234在这里也称为五位数. 输入 一个正整数n  输出 若干行,每行包括两个符合要求的五位正整数(每行的两个数先大后小),两数之

(凸包模板)(刘汝佳)

struct point{ int x,y;} p[N],stack[N]; bool cmp(point A,point B){ if(A.y==B.y)return A.x<B.x; return A.y<B.y;}int cross(point A,point B,point C){ return (B.x-A.x)*(C.y-A.y)-(C.x-A.x)*(B.y-A.y);}void graham(){ sort(p,p+n,cmp); int i; top=0; for(i=0;

bzoj 2732 [HNOI2012]射箭 半平面交(刘汝佳版不超时) + 整型二分处理

题目来源: http://61.187.179.132/JudgeOnline/problem.php?id=2732 题意:   对于一个靶子, 得到两个不等式. 裸地半平面交 . 分析: 用的 一般的 模板,总是TLE . 改成了 刘汝佳 版本 ,依然超时, 所谓的常数太大???? 后来注意到 : 当    判断两个向量平行且 同向 ,取左边的一个,不要用 叉积,用极角判断, 可行. 精度 开 1e -16 , 卡精度严重. 注意:这里 也不需要用 friend 写, 也可以ac. 整型二分

UVA_10653 公主与王子 #刘汝佳DP题刷完计划

题意如蓝书66页例题27所示. 这个问题描述了一个LCS的特殊情况--单个字符串内所有元素各不相同. 题目要求输入两个数字串,A,B,要求求出最长公共字串.且数字上限是256*256. 做法:数组A表示为256*256的大数组,每一位表示标号元素的出现位置 数组B表示为"数组A中有的每一位元素的出现位置",如果有公共串,就必然满足在串B编号递增.复杂度从O(N^2)升级到O(nlogn) 1 #include<bits/stdc++.h> 2 using namespace