HDOJ 1284 钱币兑换问题

很久很久以前 WA 过,今天找到了原因:位运算的优先级比 + 低!,重新改了一下结构(for 变 while),稍有提速(31ms—>15ms)。

 1 # include <stdio.h>
2
3 int main()
4 {
5 int N, tot;
6
7 while (~scanf("%d", &N))
8 {
9 tot = 0;
10 while (N >= 0)
11 {
12 tot += (N>>1)+1;
13 N -= 3;
14 }
15 printf("%d\n", tot);
16 }
17
18 return 0;
19 }

 

附 for 循环结构:

 1 # include <stdio.h>
2
3 int main()
4 {
5 int N, tot, i, x;
6
7 while (~scanf("%d", &N))
8 {
9 tot = 0;
10 x = N / 3;
11 for (i = 0; i <= x; ++i)
12 tot += (N-3*i)/2 + 1;
13 printf("%d\n", tot);
14 }
15
16 return 0;
17 }

耗时的是乘法(行12),其实乘法相比除法快很多,不过在这属于多余的情况。

posted on 2012-03-13 11:18  getgoing  阅读(223)  评论(0编辑  收藏  举报

导航