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