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