题目链接:http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=1962
问题思路:考察编程基础的问题,涉及到字符串转为数字的问题。
代码如下:
#include <stdio.h> #include <string.h> #define MAX_N 1000 int num_a[MAX_N], num_b[MAX_N]; int digit_a, digit_b; char character[5][MAX_N]; char example[5][40] = { "+-+ ++-++-++ ++-++-++-++-++-+", "| | | | || || | || || |", "+ + ++-++-++-++-++-+ ++-++-+", "| | || | | || | || | |", "+-+ ++-++-+ ++-++-+ ++-++-+", }; int CharToNum(int char_i, int exap_i) { int c_i = char_i; int e_j = exap_i; if (character[0][c_i] == ‘ ‘ && character[0][c_i + 1] == ‘ ‘ && character[0][c_i + 2] == ‘ ‘) return -1; for (int i = 0; i < 5; ++i) { for (int j = 0; j < 3; ++j) { if (character[i][c_i + j] != example[i][e_j + j]) return 0; } } return 1; } void output(int ans) { int digit = 0; int num[MAX_N]; int divide = ans; int rest = 0; memset(num, 0, sizeof(num)); for (int i = 0; divide != 0 || rest != 0; ++i) { num[i] = divide % 10; divide /= 10; digit = i; } for (int i = 0; i < 5; ++i) { for (int j = digit; j >= 0; --j) { int num_line = num[j] * 3; for (int k = 0; k < 3; ++k) printf("%c", example[i][num_line + k]); if (j == 0) printf("\n"); else printf(" "); } } } int main() { int len = 0; int flag = false; for (int i = 0; i < 5; ++i) gets(character[i]); len = strlen(character[0]); for (int i = 0; i < len; i += 4) { for (int j = 0; j < 30; j += 3) { int result = CharToNum(i, j); if (result == -1) { flag = true; break; } if (!flag && result != 0) { num_a[digit_a++] = j / 3; break; } else if (flag && result != 0 && result != -1) { num_b[digit_b++] = j / 3; break; } } } int a = 0, b = 0; for (int i = 0; i < digit_a; ++i) a = a * 10 + num_a[i]; for (int i = 0; i < digit_b; ++i) b = b * 10 + num_b[i]; output(a + b); return 0; }
时间: 2024-10-21 16:27:15