http://poj.org/problem?id=1775

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1334

题目大意:

给一个数n看看n是否能够拆成几个阶乘的和

如9=1!+2!+3!

方法一:

最初想法是直接打出0~10的阶乘,10的阶乘已经大于n的范围

然后DFS,也过了。不过时间好惨。。。

注意n=0输出NO和0!=1

#include<cstdio>
#include<cstring>
const int MAXN=10;
int temp[12]={1,1};
bool used[12];
bool ok;
void dfs(int cur,int sum,int target)
{
if(sum >target)
return; if(sum==target)
{
ok=true;
return ;
} for(int i=cur;i<=MAXN;i++)
{
if(used[i]==false)
{
used[i]=true;
dfs(i,sum+temp[i],target);
used[i]=false;
}
}
}
int main()
{ for(int i=2;i<=MAXN;i++)
temp[i]=temp[i-1]*i; int n;
while(scanf("%d",&n),n>=0)
{ if(n==0)
{
printf("NO\n");
continue;
}
memset(used,0,sizeof(used));
ok=false;
dfs(0,0,n); if(ok)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

方法二:

贪心。。

然后把不超过n的阶乘减去。

因为n! >= sum(1 ! + 2!+……n-1!)

#include<cstdio>
const int MAXN=10;
int main()
{
int temp[12]={1,1};
for(int i=2;i<=MAXN;i++)
temp[i]=temp[i-1]*i; int n;
while(scanf("%d",&n),n>=0)
{
if(n==0)
{
printf("NO\n");
continue;
}
for(int i=MAXN;i>=0;i--)
{
if(n >=temp[i])
n-=temp[i];
}
if(n==0)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

POJ 1775 Sum of Factorials (ZOJ 2358)的更多相关文章

  1. zoj 2358,poj 1775 Sum of Factorials(数学题)

    题目poj 题目zoj //我感觉是题目表述不确切,比如他没规定xi能不能重复,比如都用1,那么除了0,都是YES了 //算了,这种题目,百度来的过程,多看看记住就好 //题目意思:判断一个非负整数n ...

  2. POJ 1775 Sum of Factorials 数论,基础题

    输入一个小于1000000的正整数,是否能表达成式子:a1!+a2!+a3!+...+an (a1~an互不相等). 因为10!>1000000,所以先打1~10的阶乘表.从a[10]开始递减判 ...

  3. POJ 1775 (ZOJ 2358) Sum of Factorials

    Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematic ...

  4. 九度OJ 1038:Sum of Factorials(阶乘的和) (DP、递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1845 解决:780 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...

  5. poj 1564 Sum It Up (DFS+ 去重+排序)

    http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i] ...

  6. POJ 3090 Visible Lattice Points (ZOJ 2777)

    http://poj.org/problem?id=3090 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1777 题目大意: ...

  7. POJ 1707 Sum of powers(伯努利数)

    题目链接:http://poj.org/problem?id=1707 题意:给出n 在M为正整数且尽量小的前提下,使得n的系数均为整数. 思路: i64 Gcd(i64 x,i64 y) { if( ...

  8. POJ 1564 Sum It Up(DFS)

    Sum It Up Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  9. POJ 2235 Frogger / UVA 534 Frogger /ZOJ 1942 Frogger(图论,最短路径)

    POJ 2235 Frogger / UVA 534 Frogger /ZOJ 1942 Frogger(图论,最短路径) Description Freddy Frog is sitting on ...

随机推荐

  1. 洛谷——P1314 聪明的质监员

    https://www.luogu.org/problem/show?pid=1314 题目描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每 ...

  2. 华为PUSH SDK 接入方法

    本文参考了华为推送平台官网及其Demo:http://developer.huawei.com/cn/consumer/wiki/index.php?title=%E6%8E%A5%E5%85%A5% ...

  3. 从头认识Spring-2.3 注解装配-@autowired(4)-required(1)

    这一章节我们来具体讨论一下@autowired里面的參数required. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_new_spri ...

  4. PHP和JSON

    PHP和JSON 一.总结 1.php中json的使用方法:php中json的使用超级简单啦,主要是两个函数json_encode(编码)和json_decode(解码),像md5加密 2.json的 ...

  5. 【Java学习】Font字体类的用法介绍

    一.Font类简介 Font类是用于设置图形用户界面上的字体样式的,包括字体类型(例如宋体.仿宋.Times New Roman等).字体风格(例如斜体字.加粗等).以及字号大小. 二.Font类的引 ...

  6. swift -结构体

    // // main.swift // Struct-Demo-05 // import Foundation println("结构体測试!") //结构体和C语言的结构体不同 ...

  7. [NPM] Test npm packages locally in another project using npm link

    We will import our newly published package into a new project locally to make sure everything is wor ...

  8. POJ 1012:Joseph

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 50068   Accepted: 19020 Descript ...

  9. (错误记录)git push 报错 403

    在push的时候遇到错误: RPC failed; HTTP curl The requested URL returned error: Forbidden 如果是自己创建的项目的话,可以在网上找到 ...

  10. python项目实战-小游戏1

    项目规则: 1.玩家和敌人分别从现有的角色中选择3个角色 2.随机生成目前的血量,和攻击量 3.游戏规则:当玩家向敌人发起攻击,敌人当前的血量=之前的血量-玩家的血量,同理 4.3局两胜 5.自定义玩 ...