Submission

Status:

----------

Subtask/Task Score:

0/100

Score: 0

User: Whatthepoop

Problemset: กราฟสัญญาณดิจิทัล

Language: c

Time: 0.002 second

Submitted On: 2025-10-10 13:58:20

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void reverse(char str[], int start, int end) {
    while (start < end) {
        char temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        start++;
        end--;
    }
}

void ToBinary(char c, char *binary) {
    int ascii = (int)c;
    int i = 0;

    if (ascii == 0) {
        binary[i++] = '0';
    } else {
        while (ascii > 0) {
            binary[i++] = (ascii % 2) + '0';
            ascii /= 2;
        }
    }

    while (i < 8) {
        binary[i++] = '0';
    }

    binary[i] = '\0';
    reverse(binary, 0, i - 1);
}

int main(){
    char input[101];
    char result[1001] = "";
    int h;

    scanf("%s", input);
    scanf("%d", &h);

    if (input[0] == '1' || input[0] == '0') {
        strcat(result, input);
    } 
	else {
        char binChar[9];
        for (int i = 0; input[i]; i++) {
            ToBinary(input[i], binChar);
            strcat(result, binChar);
        }
    }
    
    int len = strlen(result);
    int row = h, col = (h-1)*len + 1;
    char arr[row][col];
    for(int i = 0; i < row; i++){
    	for(int j = 0; j < col; j++){
    		arr[i][j] = '_';
		}
	}
	
	arr[0][0] = 'X';
	int i = 0, j = 0;
	for(int k = 0; k < len; k++){
		if(result[k] == '0' && i != h-1){
			int counti = h-1;
			while(counti>0){
				arr[++i][j] = 'X';
				counti--;
			}
			
		}
		else if(result[k] == '1' && i != 0){
			int counti = h-1;
			while(counti>0){
				arr[--i][j] = 'X';
				counti--;
			}
		}
		
		int countj = h-1;
		while(countj>0){
			arr[i][++j] = 'X';
			countj--;
		}
	}
    
    for(int i = 0; i < row; i++){
    	for(int j = 0; j < col; j++){
    		printf("%c", arr[i][j]);
		}
		printf("\n");
	}

    return 0;
}