|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
a+b用m进制输出
样例输入
8 1300 48
2 1 7
0
样例输出
2504
1000
- #include <stdio.h>
- #include <math.h>
- int main()
- {/*输入两个不超过整形定义的非负10进制数A和B( <= 2^31-1),输出A+B的M进制数(1<m<10)。
- 当m为0时输入结束*/
- long long a,b;
- long long tmp,ans;
- int m;
- while (scanf("%d",&m) != EOF)
- {
- ans = 0;
- if (m == 0)
- {
- break;
- }
- scanf("%lld%lld",&a,&b);
- a = a + b;
- int i = 0;
- while(a != 0)
- {
- tmp = a % m;//求余,位数从低到高
- a = a / m;//求商
- ans = ans + tmp * pow(10,i); //求出的m进制数各位数字作十进制乘以权值以便输出
- i++;//i记录此时求出的数字从低位到高位的位序,以便求权值
- }
- printf("%lld\n",ans);
- }
- return 0;
- }
复制代码
按照这个代码我试了下没感觉出啥问题,
但是在九度oj(1026)上就是一直wrong answer。
附上正确答案,你参考吧
- #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];
- }
- }
复制代码
|
|