2017年12月4日 星期一

ITSA 58 - [Problem 3] 完整二元樹 - 參考答案

Difficulty: Easy
Ref: ITSA 58 - [Problem 3] 完整二元樹
/*******************************************************/
/* [Problem 3] 完整二元樹                               */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/12/04                                 */
/*******************************************************/
#include <iostream>
#include <cmath>

using namespace std;

int main() {
    int m;
    cin >> m;
    
    while(m--) {
        int C, index = 1, binTree[16];
        char buf[100];
        
        cin >> C;
        cin.ignore(0x7fffffff, '\n');
        cin.getline(buf, 101, '\n');
        
        for(int i = 0; buf[i] != '\0'; i++)
            if(buf[i] >= '0' && buf[i] <= '9') {
                binTree[index++] = atoi(&buf[i]);
                while(buf[i+1] >= '0' && buf[i+1] <= '9') i++;
            }
        
        int innerNodes = floor(index/2);
        bool notFirstOut = false;
        
        for(int i = 1; i < innerNodes; i++) {
            if(abs(binTree[i] - binTree[i * 2]) <= C) {
                if(notFirstOut) cout << " ";
                cout << char('@' + i) << char('@' + i * 2);
                notFirstOut = true;
            }
            if(abs(binTree[i] - binTree[i * 2 + 1]) <= C) {
                if(notFirstOut) cout << " ";
                cout << char('@' + i) << char('A' + i * 2);
                notFirstOut = true;
            }
        }
        cout << endl;
    }
    return 0;
}
Debug: I/O
需修補的路段進行標示,然後統計出答案。
2
8
(A,2),(B,10),(C,13)
7
(A,2),(B,10),(C,13),(D,7),(E,8),(F,9),(G,11)
AB
BD BE CF CG

ITSA 58 - [Problem 2] 道路修補 - 參考答案

Difficulty: Easy
Ref: ITSA 58 - [Problem 2] 道路修補
/*******************************************************/
/* [Problem 2] 道路修補                                 */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/12/04                                 */
/*******************************************************/
#include <iostream> 

using namespace std;

int main() {
    int m;
    cin >> m;
    
    while(m--) {
        int n, s, e, length = 0, road[10001] = { 0 };
        
        cin >> n;
        
        while(n--) {
            cin >> s >> e;
            for(int i = s; i < e; i++)
                road[i] = 1;
        }
        
        for(int i = 0; i < 10001; i++)
            if(road[i])
                length++;
        
        cout << length << endl;
    }
    return 0;
}
Debug: I/O
需修補的路段進行標示,然後統計出答案。
2
3
6 13
3 5
10 14
7
2 4
0 2
7 9
5 6
1 3
8 10
7 10
10
8

ITSA 58 - [Problem 1] 計算正整數被3整除之數值之總和 - 參考答案

Difficulty: Easy
Ref: ITSA 58 - [Problem 1] 計算正整數被3整除之數值之總和
/*******************************************************/
/* [Problem 1] 計算正整數被3整除之數值之總和              */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/12/04                                 */
/*******************************************************/
#include <iostream> 

using namespace std;

int main() {
 int m, N;
 cin >> m;
 while (m-- && cin >> N) {
  N = N / 3;
  cout << N * (N + 1) * 3 / 2 << endl;
 }
}
Debug: I/O
等差數列的和。
2
100
150
1683
3825

2017年6月1日 星期四

ITSA 50補 - [Problem 3] 面積逼近 - 參考答案

Difficulty: Easy
Ref: ITSA 50補 - [Problem 3] 面積逼近
/*******************************************************/
/* [Problem 3] 面積逼近                                 */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/06/01                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h> 

int main()
{
 int m;
 scanf("%d", &m);
 while (m--) {
  int n;
  scanf("%d", &n);
  double k = 1.0 / n;
  printf("%.4lf\n", k * k * k * n * (n + 1) * (2 * n + 1) / 6);
 }
 return 0;
}
Debug: I/O
(1/n)*(1/n)*(1/n) + 2*(1/n)*2*(1/n)*(1/n) + 3*(1/n)*3*(1/n)*(1/n) + ... + (1/n)^5
= (1*1 + 2*2 + 3*3 + ... + n*n) * (1/n)^3 。
觀察1~n個長方形的算式時會發現答案可以利用平方和公式來解。
6
2
3
4
11
17
28
0.6250
0.5185
0.4688
0.3802
0.3633
0.3514

ITSA 50補 - [Problem 2] 檢驗學號 - 參考答案

Difficulty: Easy
Ref: ITSA 50補 - [Problem 2] 檢驗學號
/*******************************************************/
/* [Problem 2] 檢驗學號                                 */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/06/01                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()
{
 char buf[14];
 int n;
 scanf("%d", &n);
 while (n--) {
  scanf("%s", buf);
  int sum = 0;
  for (int i = 0; i < 12; i += 2)
   sum += buf[i] + 3 * buf[i + 1];

  sum = 26 + '@' - (sum - 24 * '0') % 26;

  if (sum == buf[12])
   printf("valid\n");
  else
   printf("invalid\n");
 }
 return 0;
}
Debug: I/O
基偶位置作 sum,然後加減乘除。
2
123456789012L
098765432100A
valid
invalid

2017年5月28日 星期日

ITSA 50補 - [Problem 1] 猜數字 - 參考答案

Difficulty: Easy
Ref: ITSA 50補 - [Problem 1] 猜數字
/*******************************************************/
/* [Problem 1] 猜數字                                   */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/28                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int main() {  
    int a, b;  
    char n[5], c[5];  
    scanf("%s", n);  
    while (scanf("%s", c)) {  
        if (!strcmp(c, "0000"))  
            return 0;  
        a = b = 0;  
        for (int i = 0; i < 4; i++) {  
            for (int j = 0; j < 4; j++) {  
                if (i == j && n[i] == c[j]) a++;  
                if (i != j && n[i] == c[j]) b++;  
            }  
        }  
        printf("%dA%dB\n", a, b);  
          
    }  
}  
Debug: I/O
字串比較。
1234
5621
4321
1324
1234
0000
0A2B
0A4B
2A2B
4A0B

ITSA 51 - [Problem 5] Number Puzzle - 參考答案

Difficulty: Easy
Ref: ITSA 51 - [Problem 5] Number Puzzle
/*******************************************************/
/* [Problem 5] Number Puzzle                           */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/28                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

char buf[3][4];
int ok = 0;
void permute(int *a, int l, int r)
{
 if (ok) return;
 if (l == r) {     
  int num3 = a[buf[2][0]] * 100 + a[buf[2][1]] * 10 + a[buf[2][2]];
  if (num3 >= 300) return;
  int num1 = a[buf[0][0]] * 10 + a[buf[0][1]];
  int num2 = a[buf[1][0]] * 10 + a[buf[1][1]];
  if (num1 + num2 == num3) ok = 1;
 }
 else
 {
  char temp;
  for (int i = l; i <= r; i++)
  {
   temp = *(a + l);
   *(a + l) = *(a + i);
   *(a + i) = temp;
   permute(a, l + 1, r);
   temp = *(a + l);
   *(a + l) = *(a + i);
   *(a + i) = temp;
  }
 }
}

int main()
{
 while (scanf("%s%s%s", buf[0], buf[1], buf[2]) != EOF) {
  for (int i = 0; i < 3; i++)
   for (int j = 0; buf[i][j] != '\0'; j++)
    buf[i][j] -= 'A';

  ok = 0;
  int str[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  permute(str, 0, 9);
  if (ok)
   printf("YES\n");
  else
   printf("NO\n");
 }
 return 0;
}
Debug: I/O
窮舉法,把所有可能的答案進行排列匹配。
AB
BC
EED
AA
BB
ABA
AB
CD
DFG
AB
CD
EFG
AB
CD
CAD
YES
NO
YES
YES
NO

2017年5月22日 星期一

ITSA 51 - [Problem 3] 中值濾波器 - 參考答案

Difficulty: Easy
Ref: ITSA 51 - [Problem 3] 中值濾波器
/*******************************************************/
/* [Problem 3] 中值濾波器                               */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/22                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()
{
 int n;
 scanf("%d", &n);
 while (n--) {
  int m, seq[3];
  scanf("%d %d %d", &m, seq, &seq[1]);
  printf("%d ", seq[0]);
  m -= 2;
  while (m--) {
   scanf("%d", &seq[2]);
   if (seq[2] <= seq[0]) {
    if (seq[0] <= seq[1])
     seq[1] = seq[0];
    else if (seq[1] <= seq[2])
     seq[1] = seq[2];
   }
   else {
    if (seq[1] < seq[0])
     seq[1] = seq[0];
    else if (seq[2] < seq[1])
     seq[1] = seq[2];
   }
   printf("%d ", seq[0] = seq[1]);
   seq[1] = seq[2];
  }
  printf("%d\n", seq[2]);
 }
 return 0;
}
Debug: I/O
不斷比大小,答案就出來了。
2
9
3 3 5 3 3 9 4 4 4
10
4 15 3 4 4 1 3 4 4 8
3 3 3 3 3 4 4 4 4
4 4 4 4 4 3 3 4 4 8

2017年5月21日 星期日

ITSA 51 - [Problem 2] 數數之積 - 參考答案

Difficulty: Easy
Ref: IITSA 51 - [Problem 2] 數數之積
/*******************************************************/
/* [Problem 2] 數數之積                                 */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/21                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main()
{
 char buf[40], *pch;
 while (fgets(buf, 40, stdin)) {
  unsigned long long ans = strtoull(buf, &pch, 10),temp;
  while (temp = strtoull(pch, &pch, 10))
   ans *= temp;
  printf("%llu\n", ans);
 }
 return 0;
}
Debug: I/O
一直乘。
2 7 9 40 2 
35 1 7 9 24 11
10080
582120

ITSA 51 - [Problem 1] 十進制轉二進制 - 參考答案

Difficulty: Easy
Ref: ITSA 51 - [Problem 1] 十進制轉二進制
/*******************************************************/
/* [Problem 1] 十進制轉二進制                           */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/21                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main()
{
 int n;
 scanf("%d", &n);
 while (n--) {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < 8; i++){
   printf("%d", N & 128 ? 1 : 0);
   N <<= 1;
  }
  printf("\n");
 }
 return 0;
}
Debug: I/O
Bitwise operations 練習題目。
3
127
-128
66
01111111
10000000
01000010

ITSA 52 - [Problem 5] Lowest Non-zero Digit of Factorial - 參考答案

Difficulty: Normal
Ref: ITSA 52 - [Problem 5] Lowest Non-zero Digit of Factorial
/*******************************************************/
/* [Problem 5] Lowest Non-zero Digit of Factorial      */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/21                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int lnzsdf(int x) {
 unsigned i = 0, j, k, l, ret = 1;
 for (k = 282475249, l = 0; k >= 1; l = k, k /= 49)
  for (i -= l, i += k; i <= x; i += k){
   for (j = i; j % 7 == 0; j /= 7);
   ret = (ret * (j % 7)) % 7;
  }
 return ret;
}

int main()
{
 int n;
 scanf("%d", &n);
 while (n--) {
  int N;
  scanf("%d", &N);
  printf("%d\n", lnzsdf(N));
 }
 return 0;
}
Debug: I/O
暴力解會超時,所以就用了點技巧來加速運算。
先投小範圍的測資,例如1~10000,會發現規律,每間格7^(2n),其中n大於等於0時,會有同樣的循環數產生。
如果題目改為10 base的話,可以參考: Last non-zero digit of a factorial
網路上有人說可用 Wilson’s Theorem 來解,但我是看不懂啦,只說沒實作,理論大師4ni? 黑人問號...
7
7
14
21
28
2000000000
1234567899
987654321
6
2
1
3
1
2
4

2017年5月20日 星期六

ITSA 52 - [Problem 4] 合併排序 - 參考答案

Difficulty: Eazy
Ref: ITSA 52 - [Problem 4] 合併排序
/*******************************************************/
/* [Problem 4] 合併排序                                 */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/20                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
 char buf[512];
 while (1) {
  bool data[512] = { false };
  int lastout;

  for (int i = 0; i < 2; i++) {
   fgets(buf, 512, stdin);
   char *pch = strtok(buf, " ");
   while (pch != NULL) {
    data[lastout = atoi(pch)] = true;
    pch = strtok(NULL, " ");
   }
   if (!lastout) return 0;
  }

  for (int i = 0; i < 512; i++)
   if (data[i])
    if (lastout) {
     lastout = 0;
     printf("%d", i);
    }
    else
     printf(" %d", i);

  printf("\n");
 }
}
Debug: I/O
使用陣列做標記完成。
不用想太複雜,測資給的也很小。
2 5 6 8 12
1 4 15
1 8 9 11
2 4 5 10 13
0 0 0 0 0
1 2 4 5 6 8 12 15
1 2 4 5 8 9 10 11 13

ITSA 52 - [Problem 3] 頑皮的比爾 - 參考答案

Difficulty: Eazy
Ref: ITSA 52 - [Problem 3] 頑皮的比爾
/*******************************************************/
/* [Problem 3] 頑皮的比爾                               */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/20                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

struct Node {
 char id;
 Node *next;
};

int main() {
 Node node[102];
 int n;
 scanf("%d", &n);
 while (n--) {
  int k;
  scanf("%d", &k);
  for (int i = 0; i <= k; i++) {
   node[i].id = i;
   node[i].next = &node[i + 1];
  }

  int layer;
  while (scanf("%d", &layer)){
   if (layer == 0) break;
   Node *left = node[0].next;
   Node *center = left->next;
   Node *right = center->next;
   for (int i = 1; i < layer; i++) {
    center->next = left;
    left = center;
    center = right;
    right = right->next;
   }
   node[0].next->next = center;
   node[0].next = left;
  }

  Node *nextNode = node[0].next;
  printf("%d", nextNode->id);
  for (int i = 2; i <= k; i++) {
   nextNode = nextNode->next;
   printf(" %d", nextNode->id);
  }
  printf("\n");
 }
}
Debug: I/O
本題可以用來練習連結串列。
將每層的煎餅視為1個Node,每個Node均有獨立的id,其id代表煎餅的大小。
3
5 2 1 3 5 4 0
50 25 14 49 7 32 48 46 37 6 17 36 42 5 33 8 7 7 41 30 20 10 0
1 2 4 5 3
47 48 49 42 41 40 6 17 18 19 46 45 44 43 9 22 23 24 25 11 20 21 29 28 27 26 1 2 3 4 10 39 38 30 31 32 33 34 35 36 37 5 16 15 14 13 7 8 12 50

2017年5月19日 星期五

ITSA 52 - [Problem 2] 天際線資料群 - 參考答案

Difficulty: Eazy
Ref: ITSA 52 - [Problem 2] 天際線資料群
/*******************************************************/
/* [Problem 2] 天際線資料群                             */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/19                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
 int n;
 scanf("%d ", &n);
 int xy[10][2], ans[10] = { 0 };
 for (int i = 0; i < n; i++)
  scanf("%d %d", xy[i], &xy[i][1]);

 for (int i = 0; i < n; i++)
  for (int j = 0; j < n; j++)
   if (xy[i][0] <= xy[j][0] && xy[i][1] <= xy[j][1] && (xy[i][0] < xy[j][0] || xy[i][1] < xy[j][1]) && i != j) {
     ans[i] = 1;
     break;
    }

 printf("%c", ans[0] ? 'N' : 'Y');
 for (int i = 1; i < n; i++)
  printf(" %c", ans[i] ? 'N' : 'Y');
 printf("\n");
}
Debug: I/O
比較大小的經典題目。
需要知道大於、小於、等於排列起來共9種狀態,並加以篩選即為答案。
6
31 55
31 22
1 99
31 55
44 10
69 12
Y N Y Y N Y

2017年5月18日 星期四

ITSA 52 - [Problem 1] 撲克牌大小 - 參考答案

Difficulty: Eazy
Ref: ITSA 52 - [Problem 1] 撲克牌大小
/*******************************************************/
/* [Problem 1] 撲克牌大小                               */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/18                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 36*3 + 16*4

int main() {
 int n;
 scanf("%d ", &n);
 while (n--) {
  char buf[MAX], cards[52][4];
  fgets(buf, MAX, stdin);
  buf[strlen(buf) - 1] = '\0';

  int count = 0;
  char *pch = strtok(buf, " ");
  while (pch != NULL) {
   strcpy(cards[count++], pch);
   pch = strtok(NULL, " ");
  }

  for (int i = 0; i < count - 1; i++)
   for (int j = i + 1; j < count; j++)
    if ((cards[i][0] < cards[j][0]) || (cards[i][0] == cards[j][0]) && (atoi(&cards[i][1]) < atoi(&cards[j][1]))) {
     char temp[4];
     strcpy(temp, cards[i]);
     strcpy(cards[i], cards[j]);
     strcpy(cards[j], temp);
    }

  printf("%s", cards[0]);
  for (int i = 1; i < count; i++)
   printf(" %s", cards[i]);
  printf("\n");
 }
}
Debug: I/O
整行輸入後字串切割,使用泡沫排序法排序。
花色大小剛好對應到花色字元 ASCII 的大小,排序變得更簡單。
排序規則:如果花色比較大或花色相同但數字較大。
4
H5 D4 S2 C13
D8 S3 D10 C12 H7
H6 S3
C5 D11 S1
S2 H5 D4 C13
S3 H7 D10 D8 C12
S3 H6
S1 D11 C5

2017年5月17日 星期三

ITSA 54 - [Problem 5] Momo’s Hanoi Tower - 參考答案

Difficulty: Eazy
Ref: ITSA 54 - [Problem 5] Momo’s Hanoi Tower
/*******************************************************/
/* [Problem 5] Momo’s Hanoi Tower                      */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/17                                 */
/*******************************************************/
import java.util.Scanner;
import java.math.BigInteger;

public class Main{
 public static void main(String args[]) {
  Scanner scanner = new Scanner(System.in);
  int T = scanner.nextInt();
  while (T-- > 0) {
   int K = scanner.nextInt();
   BigInteger sum = new BigInteger("0");
   while(K-- > 0) {
    BigInteger n = scanner.nextBigInteger();
       BigInteger temp = new BigInteger("1");
    temp = temp.shiftLeft(K);
    temp = temp.multiply(n);
    sum = sum.add(temp);
   }
   System.out.println(sum);
  }
 }
}
Debug: I/O
這題說起來也怪,比賽的時候 C code 可以 AC ,但開放練習後再次提交居然變成 WA ...
先寫 Hanoi Tower 來模擬本題的要求,多試幾次後會發現其實是在算 nk*(2^k) 的問題。
特別注意答案為非常大的整數!
2
30
1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
30
9 8 7 6 5 4 3 2 1 9 8 7 6 5 4 3 2 1 9 8 7 6 5 4 3 2 1 9 8 7
8608845891
2126491626

2017年5月16日 星期二

ITSA 54 - [Problem 4] 稀疏矩陣相乘 - 參考答案

Difficulty: Eazy
Ref: ITSA 54 - [Problem 4] 稀疏矩陣相乘
/*******************************************************/
/* [Problem 4] 稀疏矩陣相乘                             */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/16                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main() {
 int n;
 scanf("%d", &n);
 while (n--) {
  int m, v, row, col, val, matrix[11][11] = { 0 };
  scanf("%d %d", &m, &v);
  for (int i = 0; i < v; i++) {
   scanf(" (%d:%d)=%d", &row, &col, &val);
   matrix[row][col] = val;
  }

  for (int i = 1; i <= m; i++) {
   int j = 0;
   while (j < i - 1)
    printf("%d ", matrix[j++][i]);

   matrix[i - 1][++j] = 0;
   for (int k = 1; k <= m; k++)
    matrix[i - 1][j] += matrix[i][k] * matrix[j][k];

   while (j < m) {
    printf("%d ", matrix[i - 1][j]);
    matrix[i - 1][++j] = 0;
    for (int k = 1; k <= m; k++)
     matrix[i - 1][j] += matrix[i][k] * matrix[j][k];
   }
   printf("%d\n", matrix[i - 1][j]);
  }
 }
}
Debug: I/O
此稀疏矩陣又是方陣,所以 A*AT 處理起來相對簡單。
A*AT 又是 Symmetric Matrix,所以有避開重複計算。
1
10 10
(3:2)=3
(2:3)=2
(1:2)=2
(7:2)=3
(8:5)=1
(10:1)=2
(5:9)=2
(9:6)=3
(4:7)=2
(6:6)=3
4 0 6 0 0 0 6 0 0 0
0 4 0 0 0 0 0 0 0 0
6 0 9 0 0 0 9 0 0 0
0 0 0 4 0 0 0 0 0 0
0 0 0 0 4 0 0 0 0 0
0 0 0 0 0 9 0 0 9 0
6 0 9 0 0 0 9 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 9 0 0 9 0
0 0 0 0 0 0 0 0 0 4

2017年5月15日 星期一

ITSA 54 - [Problem 3] 電路板溫度升高問題 - 參考答案

Difficulty: Eazy
Ref: ITSA 54 - [Problem 3] 電路板溫度升高問題
/*******************************************************/
/* ITSA 54 - [Problem 3] 電路板溫度升高問題              */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/15                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main() {
 int m;
 scanf("%d", &m);
 while (m--) {
  double Ti;
  int i;
  scanf("%lf,%d", &Ti, &i);
  printf("%2.4lf\n", Ti + 2.71828 * (1 + i) * i / 2);
 }
}
Debug: I/O
很明顯是在算三角形面積。(底乘高除以2)
10
94.87,9
94.87,8
94.87,7
94.87,6
94.87,5
94.87,4
94.87,3
94.87,2
94.87,1
94.87,0
217.1926
192.7281
170.9818
151.9539
135.6442
122.0528
111.1797
103.0248
97.5883
94.8700

ITSA 54 - [Problem 2] 量販店活動 - 參考答案

Difficulty: Eazy
Ref: ITSA 54 - [Problem 2] 量販店活動
/*******************************************************/
/* ITSA 54 - [Problem 2] 量販店活動                     */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/15                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main() {
 int m;
 scanf("%d", &m);
 while (m--) {
  int i, j, goods[10][2], mx_weight, n, bag[101] = { 0 };
  scanf("%d%d", &mx_weight, &n);
  for (i = 0; i < n; i++){
   scanf("%d%d", goods[i], goods[i] + 1);
   while (getchar() != '\n');
  }
  for (i = 0; i < n; i++)
   for (j = goods[i][0]; j <= mx_weight; j++) {
    int term = bag[j - goods[i][0]] + goods[i][1];
    if (term > bag[j])
     bag[j] = term;
   }
  printf("Total: %d\n", bag[mx_weight]);
 }
}
Debug: I/O
簡單的背包問題,使用動態規劃以減少時間複雜度,才能在限制時間內算出答案。
可以參考背包問題詳細說明:背包問題(Knapsack Problem)
2
15
5
6 6000 watch
4 4500 beverage
5 5500 fruit
3 2500 bread
2 2100 coke
99
9
3 2500 bread
7 6500 bagel
4 4500 yogurt
5 5500 watch
10 11500 beverage
8 8500 fruit
9 1100 jam
2 2200 coke
6 6000 juice
Total: 16600
Total: 113500

2017年5月9日 星期二

ITSA 54 - [Problem 1] 最大值與最小值 - 參考答案

Difficulty: Eazy
Ref: ITSA 54 - [Problem 1] 最大值與最小值
/*******************************************************/
/* ITSA 54 - [Problem 1] 最大值與最小值                 */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/05/09                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
 int n;
 float number, mxterm, mnterm;
 char buf[100], *pch;
 scanf("%d\n", &n);
 while (n--) {
  fgets(buf, 128, stdin);
  pch = strtok(buf, " ");
  mnterm = mxterm = atof(pch);
  while (pch = strtok(NULL, " ")) {
   number = atof(pch);
   if (mxterm < number)
    mxterm = number;
   if (mnterm > number)
    mnterm = number;
  }
  printf("maximum:%.2f\nminimum:%.2f\n", mxterm, mnterm);
 }
}
Debug: I/O
簡單的數字比大小。
2
-2 -15.2 0 89.5 100 25.3 7 30 76 4
0 3 52.7 998 135 -256 79 95 10 16
maximum:100.00
minimum:-15.20
maximum:998.00
minimum:-256.00

2017年4月11日 星期二

POJ 1852 -- Ants - 參考答案

Difficulty: Eazy
Ref: POJ 1852 -- Ants
/*******************************************************/
/* POJ 1852 -- Ants                                    */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/11                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>

using namespace std;
int main() {
 int N, L, n, x, mT, MT;
 scanf("%d", &N);
 while (N-- && scanf("%d%d", &L, &n)){
  mT = MT = 0;
  for (int i = 0; i < n; i++){
   scanf("%d", &x);
   mT = max(mT, min(x, L - x));
   MT = max(MT, max(x, L - x));
  }
  printf("%d %d\n", mT, MT);
 }
}
Debug: I/O
要特別注意的是:線上測資非常大、非常長!!!
2
10 3
2 6 7
214 7
11 12 7 13 176 23 191
4 8
38 207

2017年4月10日 星期一

ITSA 53 - [Problem 5] String - 參考答案

Difficulty: Medium
Ref: ITSA 53 - [Problem 5] String
/*******************************************************/
/* ITSA 53 - [Problem 5] String                        */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/10                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

char buf[81], table[81][10] = { 0 }, output[81], vowel[] = { 'a', 'e', 'i', 'o', 'u' };
int K;

void lookup(int cursor) {
 if (table[cursor][0] == 0) {
  output[cursor] = '\0';
  if (--K)
   printf("%s\n", output);
  else
   printf("%s*\n", output);
 }
 if (table[cursor][0] == 1)
  for (int i = 0; i < 5; i++) {
   output[cursor] = vowel[i];
   lookup(cursor + 1);
  }
 else
  for (int i = 0; table[cursor][i] != '\0'; i++) {
   output[cursor] = table[cursor][i];
   lookup(cursor + 1);
  }
}

int main() {
 int indexX = 0, indexY;
 scanf("%s%d", buf, &K);
 for (int i = 0; buf[i] != '\0'; i++) {
  indexY = 0;
  if (buf[i] == '[')
   for (i++; buf[i] != ']'; i++)
    table[indexX][indexY++] = buf[i];
  else if (buf[i] == '*')
   table[indexX][0] = 1;
  else
   table[indexX][0] = buf[i];
  indexX++;
 }
 for (int i = 0; table[i][0] != '\0'; i++) {
  for (int j = 0; table[i][j + 1] != '\0'; j++) {
   for (int k = j + 1; table[i][k] != '\0'; k++) {
    if (table[i][j] > table[i][k]) {
     int temp = table[i][j]; table[i][j] = table[i][k]; table[i][k] = temp;
    }
   }
  }
 }
 lookup(0);
}
Debug: I/O
[za]xd** 21
axdaa
axdae
axdai
axdao
axdau
axdea
axdee
axdei
axdeo
axdeu
axdia
axdie
axdii
axdio
axdiu
axdoa
axdoe
axdoi
axdoo
axdou
axdua*
axdue
axdui
axduo
axduu
zxdaa
zxdae
zxdai
zxdao
zxdau
zxdea
zxdee
zxdei
zxdeo
zxdeu
zxdia
zxdie
zxdii
zxdio
zxdiu
zxdoa
zxdoe
zxdoi
zxdoo
zxdou
zxdua
zxdue
zxdui
zxduo
zxduu

ITSA 53 - [Problem 4] 窈窕飲食規劃 - 參考答案

Difficulty: Medium
Ref: ITSA 53 - [Problem 4] 窈窕飲食規劃
/*******************************************************/
/* ITSA 53 - [Problem 4] 窈窕飲食規劃                  */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/10                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main() {
 int W, N, P, item;
 float food[10][4], ans[10][3] = { 0 }, sumc = 0, suml = 0, Mc, unit;
 scanf("%d%d%d", &W, &N, &P);
 for (int i = 0; i < N; i++) {
  scanf("%f%f%f", &food[i][3], &food[i][1], &food[i][2]);
  food[i][1] /= food[i][3];
  food[i][2] /= food[i][3];
  food[i][0] = 1;
 }
 for (int i = 1; i <= W; i++) {
  item = -1;
  Mc = -1;
  for (int j = 0; j < N; j++) {
   if (food[j][3] <= 0) continue;
   if (food[j][P] > Mc) Mc = food[item = j][P];
  }
  unit = food[item][3] < 1 ? food[item][3] : 1;
  ans[item][0] += unit;
  ans[item][1] += food[item][1] * unit;
  ans[item][2] += food[item][2] * unit;
  food[item][3] -= 1;
 }

 for (int i = 0; i < N; i++) {
  printf("%.3f ", ans[i][0]);
  sumc += ans[i][1];
  suml += ans[i][2];
 }
 printf("%.3f %.3f\n", sumc, suml);
}
Debug: I/O
5 4 1
1 3 6
2 4 5
3 2 7
2.5 4.5 4
5 4 2
1 3 6
2 4 5
3 2 7
2.5 4.5 4
1.000 2.000 0.000 2.000 10.600 14.200
1.000 2.000 2.000 0.000 8.333 15.667

2017年4月9日 星期日

ITSA 53 - [Problem 3] 號碼鎖最少轉動幾次 - 參考答案

Difficulty: Eazy
Ref: ITSA 53 - [Problem 3] 號碼鎖最少轉動幾次
/*******************************************************/
/* ITSA 53 - [Problem 3] 號碼鎖最少轉動幾次            */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/09                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
 int N, count, temp;
 char init[11], target[11];
 scanf("%d", &N);
 while (N-- && scanf("%s%s", init, target)) {
  count = 0;
  for (int i = 0; i < strlen(init); i++) {
   if (init[i] > target[i]){
    temp = init[i]; init[i] = target[i]; target[i] = temp;
   }
   temp = target[i] - init[i];
   if (temp > 10 - temp) temp = 10 - temp;
   count += temp;
  }
  printf("%d\n", count);
 }
}
Debug: I/O
7
000 217
1234 5678
12389 21354
0 9
9 0
0123911173 9999999999
9897965201 0000000000
6
16
10
1
1
22
20

ITSA 53 - [Problem 2] 洞穴裡的人 - 參考答案

Difficulty: Eazy
Ref: ITSA 53 - [Problem 2] 洞穴裡的人
/*******************************************************/
/* ITSA 53 - [Problem 2] 洞穴裡的人                     */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/09                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main() {
 int n;
 int N, M;
 scanf("%d", &n);
 while (n-- ) {
  scanf("%d%d", &N, &M);
  while ((N/=4) >= M) N += M;  
  printf("%d\n", N);
 }
}
Debug: I/O
4
330
4
12
10
12
15
333
7
2
3
3
3

ITSA 53 - [Problem 1] ISBN驗證 - 參考答案

Difficulty: Eazy
Ref: ITSA 53 - [Problem 1] ISBN驗證
/*******************************************************/
/* ITSA 53 - [Problem 1] ISBN驗證                      */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/09                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main() {
 int n, isbn[10];
 char buf[2];
 scanf("%d", &n); 
 while(n--){
  for (int i = 0; i < 10; i++) {
   scanf("%s", buf);
   if (buf[0] == 'X')
    isbn[i] = 10;
   else
    isbn[i] = atoi(buf);
  }
  for (int i = 0; i < 2; i++)
   for (int j = 9; j > 0; j--)
    for (int k = j - 1; k >= 0; k--)
     isbn[j] += isbn[k];
  if (isbn[9] % 11) 
   printf("NO\n");
  else
   printf("YES\n");
 }
}
Debug: I/O
6
X X X X X X X X X X
0 1 3 1 6 2 9 5 9 X
0 1 1 1 1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
7 8 9 X 1 2 3 X 9 X
YES
YES
NO
YES
YES
NO

2017年4月8日 星期六

UVa 11398 The Base-1 Number System - 參考答案

Difficulty: Eazy
Ref: UVa Online Judge - UVa 11398 The Base-1 Number System
/*******************************************************/
/* UVa 11398 - The Base-1 Number System                */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/08                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
 int output = 0, i = 0, flag, len;
 char buf[31];
 while (scanf("%s", buf)) {
  if (buf[0] == '~') break;
  if (buf[0] == '#') {
   printf("%d\n", output);
   output = i = 0;
   continue;
  }
  switch (len = strlen(buf)) {
  case 1:
   flag = 1;
   break;
  case 2:
   flag = 0;
   break;
  default:
   len -= 2;
   if (flag)
    for (int j = 0; j < len; j++) {
     output <<= 1;
     output++;
    }
   else output <<= len;
  }
 }
}
Debug: I/O, uDebug
0 00 #
0 #
00 000#
0 000#
00 0000#
0 0000#
00 #
00 0 #
0 0000 00 000 0 0000 #
0 0000 000 000 000 0000 #
00 0000 000 0000 0 000 #
~
0
0
199
0
27
127
1

2017年4月7日 星期五

UVa 10071 Back to High School Physics - 參考答案

Difficulty: Eazy
Ref: UVa Online Judge - UVa 10071 Back to High School Physics
/*******************************************************/
/* UVa 10071 - Back to High School Physics             */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/07                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main() {
 int v, t;
 while (scanf("%d%d", &v, &t) != EOF)
  printf("%d\n", 2 * v * t);
}
Debug: I/O, uDebug
0 0
-100 200
-100 0
100 0
100 200
38 22
-31 161
-100 155
-100 163
17 116
0
-40000
0
0
40000
1672
-9982
-31000
-32600
3944

UVa 12650 Dangerous Dive - 參考答案 - 參考答案

Difficulty: Eazy
Ref: UVa Online Judge - UVa 12650 Dangerous Dive
/*******************************************************/
/* UVa 12650 - Dangerous Dive                          */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/07                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main() {
 int n, R, card;
 while (scanf("%d%d", &n, &R) != EOF) {
  bool Return[10001] = { 0 };
  for (int i = 0; i < R; i++) {
   scanf("%d", &card);
   Return[card] = true;
  } 
  if (n == R) {
   printf("*\n");
   continue;
  }
  for (int i = 1; i <= n; i++) {
   if (!Return[i])
    printf("%d ", i);
  }
  printf("\n");
 }
}
Debug: I/O, uDebug
26 6
12 16 18 14 19 10
29 16
11 29 27 8 20 22 1 3 9 24 13 4 23 6 28 15
18 18
4 13 9 17 1 3 11 10 2 12 7 16 8 5 6 15 18 14
78 59
2 42 62 39 14 22 36 25 8 40 77 57 65 76 67 21 7 54 61 5 23 60 72 13 19 68 71 31 75 64 44 4 63 34 29 33 26 43 32 18 50 24 16 45 41 37 55 59 38 70 9 35 27 20 73 15 56 66 58
53 7
24 35 50 14 4 23 51
89 88
23 82 9 8 52 2 53 12 61 72 50 71 62 51 17 66 40 19 60 57 33 58 21 3 65 56 44 45 34 1 77 38 6 30 86 37 16 88 63 70 28 76 36 25 48 13 10 11 78 27 42 87 79 46 41 73 47 67 85 32 5 15 49 59 80 4 24 74 26 81 20 29 68 14 89 55 83 75 43 84 69 64 54 35 18 7 31 22
21 2
3 8
97 86
81 21 10 54 84 92 4 27 58 80 62 67 8 3 36 56 89 85 46 11 71 42 50 35 38 15 68 74 61 23 2 41 12 30 26 9 86 22 72 66 49 31 33 96 79 5 29 40 37 45 1 34 17 70 63 88 48 47 55 59 51 32 73 19 64 53 69 24 83 25 18 20 39 90 82 97 76 91 28 65 13 75 6 77 43 94
49 28
47 66 67 2 4 80 57 49 39 1 61 10 70
1 2 3 4 5 6 7 8 9 11 13 15 17 20 21 22 23 24 25 26 
2 5 7 10 12 14 16 17 18 19 21 25 26 
*
1 3 6 10 11 12 17 28 30 46 47 48 49 51 52 53 69 74 78 
1 2 3 5 6 7 8 9 10 11 12 13 15 16 17 18 19 20 21 22 25 26 27 28 29 30 31 32 33 34 36 37 38 39 40 41 42 43 44 45 46 47 48 49 52 53 
39 
1 2 4 5 6 7 9 10 11 12 13 14 15 16 17 18 19 20 21 
7 14 16 44 52 57 60 78 87 93 95 
3 5 6 7 8 9 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 40 41 42 43 44 45 46 48 

UVa 579 Clock Hands - 參考答案

Difficulty: Eazy
Ref: UVa Online Judge - UVa 579 Clock Hands
/*******************************************************/
/* UVa 579 - Clock Hands                               */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/07                                 */
/*******************************************************/

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main() {
 float H, M, ans;
 char input[6], *token;
 while (scanf("%s", input) != EOF) {
  token = strtok(input, ":");
  H = atof(token);
  token = strtok(0, ":");
  M = atof(token);
  if (H == 0.0 && M == 0.0)
   break;
  H = H * 30 + 0.5 * M;
  M = 6 * M;
  ans = abs(H - M);
  if (ans > 180)
   H = 360 - ans;
  else
   H = ans;
  printf("%.3f\n", H);
 }
}
Debug: I/O, uDebug
1:59
1:01
0:00
65.500
24.500

UVa 10038 Jolly Jumpers - 參考答案

Difficulty: Eazy
Ref: UVa Online Judge - UVa 10038 Jolly Jumpers
/*******************************************************/
/* UVa 10038 - Jolly Jumpers                           */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/07                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main() {
 int n, a, b, diff, flag;
 while (scanf("%d", &n) != EOF) {
  bool jolly[3001] = { 0 };
  scanf("%d", &b);
  flag = 1;
  for (int i = 1; i < n; i++) {
   a = b;
   scanf("%d", &b);
   diff = abs(a - b);
   if (diff >= n || diff <= 0 || jolly[diff])
    flag = 0;
   jolly[diff] = true;
  }
  if (flag)
   printf("Jolly\n");
  else
   printf("Not jolly\n");
 }
}
Debug: I/O, uDebug
1 2000
5 1 4 2 -1 6
2 1999 1998
4 1 4 2 3
4 1 3 2 -2
4 1 4 3 5
4 1 2 5 7
3 4 1 3
4 1 4 2 3
4 1 2 3 6
2 1 3
1 1
Jolly
Not jolly
Jolly
Jolly
Not jolly
Jolly
Jolly
Not jolly
Jolly
Not jolly
Not jolly
Jolly

2017年4月6日 星期四

Uva 488 Triangle Wave - 參考答案

Difficulty: Eazy
Ref: UVa Online Judge - Uva 488 Triangle Wave
/*******************************************************/
/* UVa 488 - Traingle Wave                             */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/06                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main() {
 int n, height, times;
 scanf("%d", &n);
 while (n-- > 0 && scanf("%d%d", &height, &times) != EOF) {
  while (times--) {
   for (int i = 1; i <= height; i++) {
    for (int j = 0; j < i; j++)
     printf("%d", i);
    printf("\n");
   }
   for (int i = height - 1; i >= 1; i--) {
    for (int j = 0; j < i; j++)
     printf("%d", i);
    printf("\n");
   }
   if (times) printf("\n");
  }
  if (n) printf("\n");
 }
}
Debug: I/O, uDebug
2

2
2

3
1
1
22
1

1
22
1

1
22
333
22
1

2017年4月5日 星期三

UVa 12289 One-Two-Three - 參考答案

Difficulty: Eazy
Ref: UVa Online Judge - UVa 12289 One-Two-Three
/*******************************************************/
/* UVa 12289 - One-Two-Three                           */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/04                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main() {
 int n, one;
 char input[6];
 scanf("%d", &n);
 while (n-- > 0 && scanf("%s", &input) != EOF) {
  if (input[3] != '\0') {
   printf("3\n");
   continue;
  }
  one = 0;
  if (input[0] == 'o') one++;
  if (input[1] == 'n') one++;
  if (input[2] == 'e') one++;
  if (one >= 2) printf("1\n");
  else  printf("2\n");
 }
}
Debug: I/O, uDebug
10
owe
too
theee
tne
owo
thfee
ono
twe
one
two
1
2
3
1
2
3
1
2
1
2

UVa 458 The Decoder - 參考答案

Difficulty: Eazy
Ref: UVa Online Judge - UVa 458 The Decoder
/*******************************************************/
/* UVa 458 - The Decoder                               */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/04                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINIGS
#include <stdio.h>
#include <stdlib.h>

int main() {
 char a;
 while ((a = getchar()) != EOF)
  putchar(a == '\n' ? '\n' : a - 7);
}
Debug: I/O, uDebug
[opz'pz'h'x|p{l'zpunsl'wyvislt5
PM'`V\'HYL'YLHKPUN'[OPZ3'`V\'TH`IL'OH]L'MV\UK'[OL'ZVS\[PVU5
Rllw'jvkpun'huk'{yhpupun'{opz'rpuk'vm'wyvisltz5'P['JV\SK'IL'YLHSS`'L_JP[PUN5
Zv555
TH`'[OL'JVKL'IL'^P[O'`V\5
This is a quite single problem.
IF YOU ARE READING THIS, YOU MAYBE HAVE FOUND THE SOLUTION.
Keep coding and training this kind of problems. IT COULD BE REALLY EXCITING.
So...
MAY THE CODE BE WITH YOU.

UVa 272 TEX Quotes - 參考答案

Difficulty: Eazy
Ref: UVa Online Judge - UVa 272 TEX Quotes
/*******************************************************/
/* UVa 272 - TEX Quotes                                */
/* Author: awei0905  [at]  awei0905.blogspot.tw        */
/* Version: 2017/04/04                                 */
/*******************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int main() {
 bool toggle = false;
 char a;
 while ((a = getchar()) != EOF)
  if (a == '"')
   printf((toggle = !toggle) ? "``" : "''");
  else putchar(a);
}
Debug: I/O, uDebug
Is branched in ""my up strictly "remember. "
Songs but chief has ham widow downs. Genius or so up vanity cannot. 
'''```Large do tried ``goi"'``''ng" about water defer by. "Silent" son man she wished mother. 
Distrusts allowance do knowledge eagerness assurance additions to. 
We """"diminution preference "thoroughly if. "Joy deal pain ';`392view" much her time. Led young gay would now state." 
Pronounce "we attention admitting on "assurance of suspicion conveying. That his west quit had met ""till"". Of advantage he 
attending "hous""ehold" at do perceived"." Middleton in objection "discovery" as agreeable. Edward thrown dining so he my around to. 
Is branched in ``''my up strictly ``remember. ''
Songs but chief has ham widow downs. Genius or so up vanity cannot. 
'''```Large do tried ``goi``'``''ng'' about water defer by. ``Silent'' son man she wished mother. 
Distrusts allowance do knowledge eagerness assurance additions to. 
We ``''``''diminution preference ``thoroughly if. ''Joy deal pain ';`392view`` much her time. Led young gay would now state.'' 
Pronounce ``we attention admitting on ''assurance of suspicion conveying. That his west quit had met ``''till``''. Of advantage he 
attending ``hous''``ehold'' at do perceived``.'' Middleton in objection ``discovery'' as agreeable. Edward thrown dining so he my around to. 

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