2017年4月5日 星期三

UVa 100 The 3n + 1 problem - 參考答案

Difficulty: Eazy
Ref: UVa Online Judge - UVa 100 The 3n + 1 problem
/*******************************************************/
/* UVa 100 - The 3n + 1 problem                        */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/04                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int n3(int n) {
 if (n == 1) return 1;
 n = n % 2 ? 3 * n + 1 : n / 2;
 return n3(n) + 1;
}

int main() {
 int i, j;
 int mx, temp;
 while (scanf("%d%d", &i, &j) != EOF){
  printf("%d %d", i , j);
  if (i > j) {
   temp = i; i = j; j = temp;
  }
  mx = -1;
  for (int k = i; k <= j; k++) {
   if (mx < (temp = n3(k))) mx = temp;
  }
  printf(" %d\n", mx);
 }
}
Debug: I/O, uDebug
605293 606510
956739 956006                                    
826611 825983
756134 756776
478642 479101
815892 815933
719220 719135
929349 929040
605293 606510 341
956739 956006 352
826611 825983 313
756134 756776 362
478642 479101 338
815892 815933 269
719220 719135 274
929349 929040 339

沒有留言:

張貼留言