【題解】ZeroJudge e799: p6. 資工系的浪漫

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

  • 2 ≤ N、M ≤ 62
  • 0 ≤ S ≤2^M-1:需宣告成long long
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int N, M;
    cin >> N >> M;
    char C;
    cin >> C;
    long long Si;
    for (int i=0; i<N; i++){
        cin >> Si;
        vector <int> v;
        for (int j=0; j<M; j++){
            v.push_back(Si % 2);
            Si /= 2;
        }
        reverse(v.begin(), v.end());
        cout << '\n';
        for (auto j: v){
            if (j) cout << C << ' ';
            else cout << ". ";
        }
        cout << '\n';
    }
    return 0;
}

Python code (credit: Amy Chou)

N, M = map(int, input().split())
C = input().strip()
for _ in range(N):
    Si = int(input())
    lst = []
    for j in range(M):
        lst.append(Si % 2)
        Si //= 2

    lst.reverse()
    for i in lst:
        if i:
            print(C, " ", sep='', end='')
        else:
            print(". ", end='')
    print()

分享本文 Share with friends