【題解】ZeroJudge a828: 間隔數 ( number )

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=a828

#include <iostream>
#include <cmath>
using namespace std;

int main(){
    int num;
    while (cin >> num){
        num -= 1;
        int count = -1, n = num % 81, a, b;
        for (int i = 1; i < 10; i++){
            for (int j = 0; j < 10; j++){
                if (i != j){
                    count++;
                }
                if (count == n){
                    a = i;
                    b = j;
                    break;
                }
            }
            if (count == n){
                break;
            }
        }
        n = (num / 81) + 3;
        for (int i = 0; i < n; i++){
            if (i % 2 == 0){
                cout << a;
            }else{
                cout << b;
            }
        }
        cout << "\n";
    }
}

Python code (credit: Amy Chou)

"""
N = ['10', '12', '13', '14', '15', '16', '17', '18', '19',
     '20', '21', '23', '24', '25', '26', '27', '28', '29',
     '30', '31', '32', '34', '35', '36', '37', '38', '39',
     '40', '41', '42', '43', '45', '46', '47', '48', '49',
     '50', '51', '52', '53', '54', '56', '57', '58', '59',
     '60', '61', '62', '63', '64', '65', '67', '68', '69',
     '70', '71', '72', '73', '74', '75', '76', '78', '79',
     '80', '81', '82', '83', '84', '85', '86', '87', '89',
     '90', '91', '92', '93', '94', '95', '96', '97', '98',]
"""
N = []
for i in range(1, 10):
    for j in range(10):
        if (i != j):
            N.append(i*10 + j)

while True:
    try:
        n = int(input())
        n -= 1
        q = n // 81
        p = n - q * 81
        q += 3 # length of the output number

        for i in range(q // 2):
            print(N[p], end='')

        if (q % 2 == 1):
            print(N[p] // 10, end='')
        print()

    except:
        break
分享本文 Share with friends