小出一道题(乘法运算的程序)
描述从键盘读入个100以内的正整数,进行乘法计算并输出。
输入
多组测试数据,每组测试数据包括两个整数m,n仅一行,以0 0 结尾。两个数的第一位和最后一位都不是0.
输出
输出格式: 两个乘数的末位数对齐 乘号后面紧跟着第二个乘数,线的长度以最长的数的位数为准,每两组测试数据中间,输出一个空行。
样例输入
89 13
2 3
0 0
样例输出
89
x13
----
267
89
----
1157
2
x3
--
6
#include <stdio.h> #include <stdlib.h> int main(void) { int m, n, i = 0, j, k, inputs_size = 10, * inputs = (int *)malloc(sizeof(int) * inputs_size); int m_width, n_width, m_n_width, m_n_10_width, result_width, temp; while(scanf("%d%d", &n, &m) == 2 && n != 0 && m != 0) { if(n > 100 || m > 100 || n < 0 || m < 0) continue; if(i >= inputs_size) { inputs = (int *)realloc(inputs, inputs_size += 10); } inputs[i] = m; inputs[i + 1] = n; i += 2; } for(j = 0; j < i; j += 2) { m = inputs[j]; n = inputs[j + 1]; m_width = 0, n_width = 0, m_n_width = 0, m_n_10_width = 0, result_width = 0; temp = m; while(temp) { temp /= 10; m_width++; } temp = n; while(temp) { temp /= 10; n_width++; } temp = n * (m % 10); while(temp) { temp /= 10; m_n_width++; } temp = n * (m/ 10 % 10); while(temp) { temp /= 10; m_n_10_width++; } temp = n * m; while(temp) { temp /= 10; result_width++; } result_width = result_width < 2 ? 2 : result_width; for(k = 0; k < result_width - m_width; k++) printf(" "); printf("%d\n", n); for(k = 0; k < result_width - n_width - 1; k++) printf(" "); printf("x%d\n", m); for(k = 0; k < result_width; k++) printf("-"); printf("\n"); for(k = 0; k < result_width - m_n_width; k++) printf(" "); printf("%d\n", n * (m % 10)); if(n / 10 != 0) { if(result_width == 4 && m_n_10_width < 3) printf(" "); printf("%d\n", n * (m / 10 % 10)); for(k = 0; k < result_width; k++) printf("-"); printf("\n%d\n\n", n * m); } else { printf("\n"); } } return 0; }