swjtu 1962 A+B(模拟)

时间:2021-06-26 18:24:37

题目链接: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[][MAX_N];
char example[][] =
{
"+-+ ++-++-++ ++-++-++-++-++-+",
"| | | | || || | || || |",
"+ + ++-++-++-++-++-+ ++-++-+",
"| | || | | || | || | |",
"+-+ ++-++-+ ++-++-+ ++-++-+",
}; int CharToNum(int char_i, int exap_i)
{
int c_i = char_i;
int e_j = exap_i; if (character[][c_i] == ' ' &&
character[][c_i + ] == ' ' &&
character[][c_i + ] == ' ')
return -; for (int i = ; i < ; ++i)
{
for (int j = ; j < ; ++j)
{
if (character[i][c_i + j] !=
example[i][e_j + j])
return ;
}
} return ;
} void output(int ans)
{
int digit = ;
int num[MAX_N];
int divide = ans;
int rest = ; memset(num, , sizeof(num));
for (int i = ; divide != || rest != ; ++i)
{
num[i] = divide % ;
divide /= ;
digit = i;
} for (int i = ; i < ; ++i)
{
for (int j = digit; j >= ; --j)
{
int num_line = num[j] * ; for (int k = ; k < ; ++k)
printf("%c", example[i][num_line + k]); if (j == )
printf("\n");
else
printf(" ");
}
}
} int main()
{
int len = ;
int flag = false; for (int i = ; i < ; ++i)
gets(character[i]); len = strlen(character[]); for (int i = ; i < len; i += )
{
for (int j = ; j < ; j += )
{
int result = CharToNum(i, j); if (result == -)
{
flag = true;
break;
} if (!flag && result != )
{
num_a[digit_a++] = j / ;
break;
}
else
if (flag && result != && result != -)
{
num_b[digit_b++] = j / ;
break;
}
}
} int a = , b = ; for (int i = ; i < digit_a; ++i)
a = a * + num_a[i];
for (int i = ; i < digit_b; ++i)
b = b * + num_b[i]; output(a + b); return ;
}