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