ACM 2009. 6. 2. 20:47
반응형


I got accept from Judge Server

----------------------------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define MAX_NUM 1000000
int getCycleLength(unsigned int input);
 
typedef struct Number{
 int num1;
 int num2;
 unsigned int cycleLength;
}Number;
 
char *number;
 
int main(void)
{
 char line[100];
 int  numCount=0, tmp;
 int  i, cycleLength, check=0;
 Number  num;
 
 while(scanf("%d %d", &num.num1, &num.num2) == 2){
  
  if(num.num1>1000000 ||  num.num2>1000000 || num.num1<1 || num.num2<1){
   continue;
  }
 
  check = 0;
  if(num.num1 > num.num2){
   tmp       = num.num2;
   num.num2 = num.num1;
   num.num1 = tmp;
   check  = 1;
  }
 
  number = (char*)malloc(MAX_NUM);
  memset(number, '1', MAX_NUM);
 
  num.cycleLength = 0;
  for(i=num.num2; i>=num.num1; i--)
  {
   if(number[i-1]=='0')
    continue;
 
   cycleLength = getCycleLength(i);
   if(!cycleLength)
    return 0;
 
   if(cycleLength > num.cycleLength ){
    num.cycleLength = cycleLength;
   }
  }
  if(check){
   printf("%d %d %d\n", num.num2, num.num1, num.cycleLength);
  }
  else
  {
   printf("%d %d %d\n", num.num1, num.num2, num.cycleLength);
  }
  free(number);
 }
 return 0;
}
 
int getCycleLength(unsigned int input)
{
 unsigned int cycleLength=1;
 while(input != 1)
 {
  if(input-1<MAX_NUM && input>0){
   number[input-1] = '0';
  // printf("%d = %c\n", input, number[input-1]);
  }
  //printf("%d ", input);
  if(input%2 == 1){
   input = (input*3) +1;
   cycleLength++;
  }
  else {
   input = (input/2);
   cycleLength++;
  }
 }
 //printf("%d\n", input);
 return cycleLength;
}

반응형

'ACM' 카테고리의 다른 글

[Time Limit] 10099 - The Tourist Guide  (0) 2009.06.02
[Accept] 10004 - Bicoloring problem  (0) 2009.06.02
10006 - Carmichael Numbers  (0) 2009.06.02
850 - Crypt Kicker II  (0) 2009.06.02
10039 - Railroads  (0) 2009.06.02
posted by ssuk1010
: