在二十进制中,我们除了使用数字0-9以外,还使用字母a-j(表示10-19),给定两个二十进制整数,求它们的和。
输入是两个二十进制整数,且都大于0,不超过100位;
输出是它们的和(二十进制),且不包含首0。我们用字符串来表示二十进制整数。
为何这组数据通过不了:
9c, de5h53hi0id437bbf796ih39bff86igeii10e8i2c983eg00c9ac1a678b80h0bf3a86cf9cb2h6e8830292cef88eg3ga312c
代码如下:
#include<stdio.h>
char* calculate (char* x,char* y)
{
char c[200];
char cc[]={'0','1','2','3','4','5','6','7','8','9'
,'a','b','c','d','e','f','g','h','i','j'};
int alen,blen,i,j,p,temp=0,k=0;
alen=strlen(x);
blen=strlen(y);
i=alen-1;j=blen-1;
// printf("%d %d %c %d",i,j,cc[19],'a'-'a');
while(i>=0&&j>=0){
int sum,a1,b1;
if(x[i]-'a'>=0){
a1=x[i]-'a'+10;
}else{
a1=x[i]-'0';
}
if(y[i]-'a'>=0){
b1=y[j]-'a'+10;
}else{
b1=y[j]-'0';
}
sum=a1+b1+temp;
c[k++]=cc[sum%20];
temp=sum/20;
--i;--j;
}
while(i>=0){
int sum,a1,b1;
if(x[i]-'a'>=0){
a1=x[i]-'a'+10;
}else{
a1=x[i]-'0';
}
sum=a1+temp;
c[k++]=cc[sum%20];
temp=sum/20;
--i;
}
while(j>=0){
int sum,a1,b1;
if(y[i]-'a'>=0){
b1=y[j]-'a'+10;
}else{
b1=y[j]-'0';
}
sum=b1+temp;
c[k++]=cc[sum%20];
temp=sum/20;
--j;
}
if(temp)c[k]=cc[temp];
// if(!temp) k=k-1;
c[k+1]='\0';
/* for(p=k;p>=0;p--){
printf("%c",c[p]);
}*/
printf("\n");
for(p=0;p<strlen(c)/2;++p){
char ch=c[p];
c[p]=c[strlen(c)-p-1];
c[strlen(c)-1-p]=ch;
}
// printf("%s\n",c);
return c;
}
int main()
{
return 0;
}
8 个解决方案
#1
谁给置顶一个各个编译器的debug步骤 我送500可用分!
#2
楼主这个程序不完整。没有引用string.h,主函数也没有写完,你是怎么测试数据不通过的?
#3
确实很多新手不喜欢调试, 很多问题单步调试, 问题就简单明了的摆在哪里.
#4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
int main() {
int i,v;
char bs[33];
char b[33];
char hs[9];
char h[9];
char s[4];
char *e;
// 十进制整数转二进制串;
i=1024;
ltoa(i,b,2);
sprintf(bs,"%032s",b);
printf("i=%d,bs=%s\n",i,bs);
// 十进制整数转十六进制串;
i=1024;
ltoa(i,h,16);
sprintf(hs,"%08s",h);
printf("i=%d,hs=%s\n",i,hs);
// 十六进制字符串转成十进制数
strcpy(hs,"00000400");
sscanf(hs,"%x",&i);
printf("hs=%s,i=%d\n",hs,i);
// 二进制字符串转化为十六进制字符串;
strcpy(bs,"00000000000000000000010000000000");
i=strtol(bs,&e,2);
ltoa(i,h,16);
sprintf(hs,"%08s",h);
printf("bs=%s,hs=%s\n",bs,hs);
// 二进制字符串转化为十进制数;
strcpy(bs,"00000000000000000000010000000000");
i=strtol(bs,&e,2);
printf("bs=%s,i=%d\n",bs,i);
// 十六进制字符串转成二进制串
strcpy(hs,"00000400");
sscanf(hs,"%x",&i);
ltoa(i,b,2);
sprintf(bs,"%032s",b);
printf("hs=%s,bs=%s\n",hs,bs);
// ASC\GBK字符串转十六进制串
strcpy(s,"a汉");
i=0;
while (1) {
if (0==s[i]) break;
sprintf(hs+i*2,"%02X",(unsigned char)s[i]);
i++;
}
setlocale(LC_ALL,"chs");
printf("s=%s,hs=%s\n",s,hs);
// 十六进制字符串转成汉字(GBK)及字符(ASC)
strcpy(hs,"61BABA");
i=0;
while (1) {
if (1!=sscanf(hs+i*2,"%2x",&v)) break;
s[i]=(char)v;
i++;
}
s[i]=0;
printf("hs=%s,s=%s\n",hs,s);
return 0;
}
//i=1024,bs=00000000000000000000010000000000
//i=1024,hs=00000400
//hs=00000400,i=1024
//bs=00000000000000000000010000000000,hs=00000400
//bs=00000000000000000000010000000000,i=1024
//hs=00000400,bs=00000000000000000000010000000000
//s=a汉,hs=61BABA
//hs=61BABA,s=a汉
#5
base is between 2 and 36:
long strtol( const char *nptr, char **endptr, int base );
char *_ltoa( long value, char *string, int base );
#6
没有明白,请大神详细一点
#7
意思就是这两个函数支持2~36进制的转换。
你要实现20进制的加法,只要先用strtol将两个要加的20进制数转为10进制数,相加,再将结果用ltoa函数转为20进制数即可。
#8
谢谢。。。。
#1
谁给置顶一个各个编译器的debug步骤 我送500可用分!
#2
楼主这个程序不完整。没有引用string.h,主函数也没有写完,你是怎么测试数据不通过的?
#3
确实很多新手不喜欢调试, 很多问题单步调试, 问题就简单明了的摆在哪里.
#4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
int main() {
int i,v;
char bs[33];
char b[33];
char hs[9];
char h[9];
char s[4];
char *e;
// 十进制整数转二进制串;
i=1024;
ltoa(i,b,2);
sprintf(bs,"%032s",b);
printf("i=%d,bs=%s\n",i,bs);
// 十进制整数转十六进制串;
i=1024;
ltoa(i,h,16);
sprintf(hs,"%08s",h);
printf("i=%d,hs=%s\n",i,hs);
// 十六进制字符串转成十进制数
strcpy(hs,"00000400");
sscanf(hs,"%x",&i);
printf("hs=%s,i=%d\n",hs,i);
// 二进制字符串转化为十六进制字符串;
strcpy(bs,"00000000000000000000010000000000");
i=strtol(bs,&e,2);
ltoa(i,h,16);
sprintf(hs,"%08s",h);
printf("bs=%s,hs=%s\n",bs,hs);
// 二进制字符串转化为十进制数;
strcpy(bs,"00000000000000000000010000000000");
i=strtol(bs,&e,2);
printf("bs=%s,i=%d\n",bs,i);
// 十六进制字符串转成二进制串
strcpy(hs,"00000400");
sscanf(hs,"%x",&i);
ltoa(i,b,2);
sprintf(bs,"%032s",b);
printf("hs=%s,bs=%s\n",hs,bs);
// ASC\GBK字符串转十六进制串
strcpy(s,"a汉");
i=0;
while (1) {
if (0==s[i]) break;
sprintf(hs+i*2,"%02X",(unsigned char)s[i]);
i++;
}
setlocale(LC_ALL,"chs");
printf("s=%s,hs=%s\n",s,hs);
// 十六进制字符串转成汉字(GBK)及字符(ASC)
strcpy(hs,"61BABA");
i=0;
while (1) {
if (1!=sscanf(hs+i*2,"%2x",&v)) break;
s[i]=(char)v;
i++;
}
s[i]=0;
printf("hs=%s,s=%s\n",hs,s);
return 0;
}
//i=1024,bs=00000000000000000000010000000000
//i=1024,hs=00000400
//hs=00000400,i=1024
//bs=00000000000000000000010000000000,hs=00000400
//bs=00000000000000000000010000000000,i=1024
//hs=00000400,bs=00000000000000000000010000000000
//s=a汉,hs=61BABA
//hs=61BABA,s=a汉
#5
base is between 2 and 36:
long strtol( const char *nptr, char **endptr, int base );
char *_ltoa( long value, char *string, int base );
#6
没有明白,请大神详细一点
#7
意思就是这两个函数支持2~36进制的转换。
你要实现20进制的加法,只要先用strtol将两个要加的20进制数转为10进制数,相加,再将结果用ltoa函数转为20进制数即可。
#8
谢谢。。。。