Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1562133
  • 博文数量: 327
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-05 21:28
个人简介

东黑布衣,流浪幽燕。 真诚善良,值得信赖。

文章分类

全部博文(327)

我的朋友

分类: BSD

2017-05-24 21:13:36

[Description]
You have two hourglasses in front of you, one N minutes and
the other M min
utes. If it's possible to use both hourglass
es to set the timer to K minutes, prin
t 1, If not, print 0.
Write a program that prints the value.


However, the hourglasses can be rotated maximum 10 times. W
hen both hou
rglasses are rotated at the same time, it is co
nsidered as a 1 time rotation.


[Constraints]
1. 1<= N, M, K <= 1000
2. It can be assumed that the initial state of the hourglas
ses shows all of the
sands at the bottom of the glass.


  1. #include <stdio.h>
  2. int N,M,K;
  3. int result;
  4. void initialize(void)
  5. {
  6.    scanf("%d %d %d", &N,&M,&K);
  7.    result=0;
  8. }

  9. void rotate_combination(int step, int upn, int upm, int totalmin)
  10. {
  11.    int tmp;
  12.    if(step>10)
  13.       return;
  14.    if(result==1)
  15.       return;
  16.    if(totalmin==K)
  17.    {
  18.       result=1;
  19.       return;
  20.    }

  21.    if(upn > 0 && totalmin+upn <= K)
  22.    {
  23.       tmp = (upm-upn>0)?upm-upn:0;
  24.       rotate_combination(step+1, N, tmp, totalmin+upn);
  25.       rotate_combination(step+1, 0, M-tmp, totalmin+upn);
  26.       rotate_combination(step+1, N, M-tmp, totalmin+upn);
  27.    }
  28.    if(upm > 0 && totalmin+upm <= K)
  29.    {
  30.       tmp = (upn-upm>0)?upn-upm:0;
  31.       rotate_combination(step+1, N-tmp, 0, totalmin+upm);
  32.       rotate_combination(step+1, tmp, M, totalmin+upm);
  33.       rotate_combination(step+1, N-tmp, M, totalmin+upm);
  34.    }
  35. }

  36. int main()
  37. {
  38.    int test_case,T;
  39.    freopen("20170524in.txt","r",stdin);
  40.    scanf("%d",&T);
  41.    for(test_case=1;test_case<=T;test_case++)
  42.    {
  43.       initialize();
  44.       rotate_combination(0,N,0,0);
  45.       rotate_combination(0,0,M,0);
  46.       rotate_combination(0,N,M,0);
  47.       printf("#%d %d\n", test_case,result);
  48.    }
  49.    fclose(stdin);
  50.    return 0;
  51. }

/* input
5
3 5 7
12 13 118
8 3 9
1 103 18
4 3 41
*/
/* output
1
0
1
1
0
*/



阅读(1400) | 评论(0) | 转发(0) |
0

上一篇:[20170517 ADV]排列组合

下一篇:[ACM02] 0503

给主人留下些什么吧!~~