前言
题目描述:
输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数。
输入:
当m为0时输入结束。
输出:
样例输入:
8 1300 48 2 1 7 0
样例输出:
2504 1000
AC代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define max_size 1000 struct stack { int data[max_size]; int top; }; void initStack(struct stack *); void pushStack(struct stack *, int ); int popStack(struct stack *); int main() { unsigned int m, num; unsigned long int a, b, plus, remainder; struct stack *pstack; pstack = (struct stack *)malloc(sizeof(struct stack)); while(scanf("%d", &m) != EOF && m != 0) { scanf("%ld %ld", &a, &b); //初始化 plus = a + b; initStack(pstack); //考虑a+b=0的情况 if(plus == 0) { pushStack(pstack, 0); } //求m进制数 while(plus) { remainder = plus % m; pushStack(pstack, remainder); plus /= m; } //打印输出 while(pstack->top > 0) { num = popStack(pstack); printf("%d", num); } printf("\n"); } return 0; } void initStack(struct stack *s) { s->top = 0; } void pushStack(struct stack *s, int data) { if(s->top < max_size + 1) { s->data[s->top ++] = data; } } int popStack(struct stack *s) { if(s->top > 0) { return s->data[-- s->top]; } }