2012年7月26日

摘要: 开关灯问题:有n个人和n盏灯,第一个人开所有的灯,第二个人按所有2的倍数的灯的开关,第三个人按所有3的倍数的灯的开关,依此类推,求最后多少盏灯亮着解法:按奇数次则亮,偶数次则灭,所以因子数为奇数亮,所以平方数亮,所以即求n内平方数个数,为(int)sqrt(n),如n=16,根号16=4,所以平方数有1*1,2*2,3*3,4*4,即1、4、9、16四个。code:#include<stdio.h>#include<math.h>int main(){ int t,n,m; scanf("%d",&t); while(t--) { scanf 阅读全文
posted @ 2012-07-26 12:24 acmer-jun 阅读(176) 评论(0) 推荐(0) 编辑
摘要: 题意:求1-n内与n互质的数的个数code:#include<iostream>#include<cstdio>#include<cstdlib>int phi[10000];int main(){ int n,i,cnt,j,s; while(scanf("%d",&n)!=EOF) { cnt=0;s=n; for(i=2;i*i<=n;i++) //求质因数 { if(n%i==0) { phi[cnt++]=i; whil... 阅读全文
posted @ 2012-07-26 12:23 acmer-jun 阅读(184) 评论(0) 推荐(0) 编辑
摘要: 题意:求1<=x<=n内不超过x的所有与x互质的数的个数解法:欧拉公式code:#include<iostream>#include<cstdio>#include<cstdlib> const int N=100; int phi[N],prime[N];int main() { int i,j; prime[0]=prime[1]=0; for(i=2;i<N;i++) { prime[i]=1; } for(i=2;i*i<N;i++) { if(prime[i]) { ... 阅读全文
posted @ 2012-07-26 12:22 acmer-jun 阅读(2161) 评论(0) 推荐(0) 编辑
摘要: hdu1372: http://acm.hdu.edu.cn/showproblem.php?pid=1372题意:一张8*8棋盘,横坐标用a~h表示,纵坐标用1~8表示,每对数据给出马的初始位置和目标位置,问需要多少步能到达code:#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;int dx[]={-2,2,-1,1,-2,2,-1,1};int dy[]={1,-1,-2,-2,-1,1,2,2};int q[ 阅读全文
posted @ 2012-07-26 12:17 acmer-jun 阅读(166) 评论(0) 推荐(0) 编辑
摘要: hdu2579: http://acm.hdu.edu.cn/showproblem.php?pid=2579题意:“#”代表石头,如果走的步数是k的倍数,石头会消失,求最小时间(步数)code:#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;const int inf=1<<29;char v[150][150];int q[150*150*150][2], 阅读全文
posted @ 2012-07-26 12:10 acmer-jun 阅读(162) 评论(0) 推荐(0) 编辑
摘要: hdu2102: http://acm.hdu.edu.cn/showproblem.php?pid=2102 题意:迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙 的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。code:#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib 阅读全文
posted @ 2012-07-26 12:07 acmer-jun 阅读(180) 评论(0) 推荐(0) 编辑
摘要: hdu1728: http://acm.hdu.edu.cn/showproblem.php?pid=1728题意:求最少转弯数,开始时方向不定,所以第一次算不成转弯code:#include<iostream>#include<cstdio>#include<cstdlib>int dx[]={1,-1,0,0};int dy[]={0,0,1,-1};int q[150*600][2],d[150][150][4];char v[150][150];const int inf=1<<29;int main(){ int i,j,m,n,x,y 阅读全文
posted @ 2012-07-26 12:02 acmer-jun 阅读(185) 评论(0) 推荐(0) 编辑
摘要: hdu1171: http://acm.hdu.edu.cn/showproblem.php?pid=1171题意:给定设备种类n,接下来输出n行,v[i]、m[i]表示有m[i]个价值为v[i]的设备,求将这些设备分为两部分,每部分的价值,要求两部分的价值相差最小。解法:混合背包,背包最大容量为总价值sum的一半s,要求价值尽量大,则f[s]和sum-f[s]即为答案。 code:#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int v[100],m[100 阅读全文
posted @ 2012-07-26 11:56 acmer-jun 阅读(149) 评论(0) 推荐(0) 编辑
摘要: hdu2191: http://acm.hdu.edu.cn/showproblem.php?pid=2191题意:容量为v的背包,有n种物品,每种物品时有限个的,有不同体积及价值,求最大价值解法:多重背包code:#include<iostream>#include<cstdio>#include<algorithm>int v[200],w[200],c[200],f[200];int max(int a,int b){ if(a>b) return a; else return b;}int main(){ int t,n,m,i,j,k,x.. 阅读全文
posted @ 2012-07-26 11:54 acmer-jun 阅读(202) 评论(0) 推荐(0) 编辑
摘要: hdu1712: http://acm.hdu.edu.cn/showproblem.php?pid=1712题意:输入课程数n和总天数m,再输入矩阵n*m,v[i][j]表示花费j天在课程i上得到价值为v[i][j]解法:分组背包问题:组数为n,总容量为m,每件物品费用为c[i]=j,价值为w[i]=v[i][j]code:#include<iostream>#include<cstdio>#include<cstdlib>int max(int a,int b){ if(a>b) return a; else return b;}int f[120 阅读全文
posted @ 2012-07-26 11:50 acmer-jun 阅读(154) 评论(0) 推荐(0) 编辑
摘要: hdu2602: http://acm.hdu.edu.cn/showproblem.php?pid=2602题意:有n个物品和容量为v的背包,每种物品只可取一个,且有不同价值及体积,求最大价值 解法:01背包code:#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int max(int a,int b){ if(a>b) return a; else return b;}int v[1010],w[1010],f[1010];int main(){ . 阅读全文
posted @ 2012-07-26 11:34 acmer-jun 阅读(206) 评论(0) 推荐(0) 编辑
摘要: hdu2546: http://acm.hdu.edu.cn/showproblem.php?pid=2546题意:输入n,表示有n种菜可购买,再输入n个数v[i],表示菜的价格,再输入m,表示卡上有m元,规定若卡上余额大于等于5则可购买任意价钱的物品(即使买后余额为负),否则不可购买任何物品,求最后卡上最小余额解法:01背包+贪心:最贵的物品肯定要被购买到,所以先选出来最后购买。先对除了最贵的物品外其余物品进行01背包处理,总容量为m-5,物品费用和价值都为w[i]。code:#include<iostream>#include<cstdio>#include< 阅读全文
posted @ 2012-07-26 10:19 acmer-jun 阅读(218) 评论(0) 推荐(0) 编辑

导航