BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #89956同步于 2016/1/19
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

求问:九度1436

ccccgn
2016/1/19镜像同步10 回复
题目描述: Long time ago , Kitty lived in a small village. The air was fresh and the scenery was very beautiful. The only thing that troubled her is the typhoon. When the typhoon came, everything is terrible. It kept blowing and raining for a long time. And what made the situation worse was that all of Kitty's walls were made of wood. One day, Kitty found that there was a crack in the wall. The shape of the crack is a rectangle with the size of 1×L (in inch). Luckly Kitty got N blocks and a saw(锯子) from her neighbors. The shape of the blocks were rectangle too, and the width of all blocks were 1 inch. So, with the help of saw, Kitty could cut down some of the blocks(of course she could use it directly without cutting) and put them in the crack, and the wall may be repaired perfectly, without any gap. Now, Kitty knew the size of each blocks, and wanted to use as fewer as possible of the blocks to repair the wall, could you help her ? 输入: The problem contains many test cases, please process to the end of file( EOF ). Each test case contains two lines. In the first line, there are two integers L(0<L<1000000000) and N(0<=N<600) which mentioned above. In the second line, there are N positive integers. The ith integer Ai(0<Ai<1000000000 ) means that the ith block has the size of 1×Ai (in inch). 输出: For each test case , print an integer which represents the minimal number of blocks are needed. If Kitty could not repair the wall, just print "impossible" instead. 样例输入: 5 3 3 2 1 5 2 2 1 样例输出: 2 impossible 我的代码如下: #include <iostream> #include <algorithm> using namespace std; int main() { int L,N,a[600]; while(cin>>L>>N) { int M=0,j=0; for(int i=0;i<N;i++) { cin>>a[i]; M+=a[i]; //求可用木块面积总和; } if(M>=L) //可用木块面积大于等于所需木块; { sort(a,a+N); reverse(a,a+N); //将输入的木块从大到小排序; while(L>0) //L依次与木块面积相减,直到L<=0,跳出循环,j为最小木块数; { L-=a[j]; j++; } cout<<j<<endl; } else //可用木块面积小于所需木块时,输出“impossible”; cout<<"impossible"<<endl; } return 0; } 提交一直WA,请大神帮忙看一眼,在此谢过!
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
chenxiansf机器人#1 · 2016/1/19
贪心 #include <stdio.h> #include <algorithm> using namespace std; bool cmp(int x, int y) { return x > y; } int main() { int l, n; int a[610]; while (scanf("%d%d", &l, &n) != EOF) { for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n, cmp); int num = 0; for (int i = 0; i < n; i++) { if (l == 0) break; if (a[i] <= l) { l -= a[i]; num++; } else { l = 0; num++; } } if (l == 0) { printf("%d\n", num); } else { printf("impossible\n"); } } return 0; } 【 在 ccccgn 的大作中提到: 】 : 题目描述: : Long time ago , Kitty lived in a small village. The air was fresh and the scenery was very beautiful. The only thing that troubled her is the typhoon. : When the typhoon came, everything is terrible. It kept blowing and raining for a long time. And what made the situation worse was that all of Kitty's walls were made of wood. : ...................
ccccgn机器人#2 · 2016/1/19
多谢多谢,不过还是想知道自己的方法哪里错了,好痛苦啊[ema12] 【 在 chenxiansf 的大作中提到: 】 贪心 #include <stdio.h> #inc...
beyondliyang机器人#3 · 2016/1/19
假设输入: 6 4 4 3 1 1 你代码的20行到24行是有问题的。看看那个贪心的。。
ccccgn机器人#4 · 2016/1/19
我的代码会输出2,应该没错吧 【 在 beyondliyang 的大作中提到: 】 假设输入: 6 4 4 3 1 1 你代码的20行到2...
ml3615556机器人#5 · 2016/1/19
这篇英文题目肯定是中文强行翻译过去的… 一股乡土气息 发自「贵邮」
buptocean机器人#6 · 2016/1/20
ccccgn机器人#7 · 2016/1/20
额,你说的这个例子我输出也是2,也没错额。。 【 在 beyondliyang 的大作中提到: 】 : : 好吧。。我的那个例子没举好。假设, : 输入: : ...................
ccccgn机器人#8 · 2016/1/20
这都被发现了。。捂脸 【 在 buptocean 的大作中提到: 】 :
popols机器人#9 · 2016/1/20
[ema9] 【 在 ccccgn 的大作中提到: 】 题目描述: Long time ago , Kitty...