求助:北大ACM1001: Exponentiation为什么老是Wrong Answer?
#include<stdio.h>#include<string.h>#include<stdlib.h>char*multi(char*a,char*b){intla=...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* multi(char* a, char* b){
int la = strlen(a);
int lb = strlen(b);
int lc = la+lb;
char* c = (char*)malloc(lc+2);
int i, j, k;
memset(c,0,lc+2);
for(i = lb-1;i >= 0; i--)
for(j = la-1, k = lc - (lb - i); j >= 0; j--,k--){
int tmp;
c[k] += (a[j]-'0') * (b[i]-'0');
tmp = c[k]/10;
if(tmp > 0){
c[k-1] +=tmp;
c[k] -=tmp*10;
}
}
for(i = 0; i < lc; i++)
c[i]+='0';
return c;
}
int main(void){
char s[7];
int n;
char* m, *e;
int lm,ls;
//FILE* f = fopen("test.txt","r");
while(scanf("%s%d",s,&n) != EOF){
//while(fscanf(f,"%s%d",s,&n) != EOF){
int dot = 0;
int i;
int lm;
if(n == 0){
printf("%d\n",1);
continue;
}
if(n == 1){
printf("%s\n",s);
continue;
}
ls = strlen(s);
while(s[dot] != '.'&& dot < ls)
dot++;
for(i = dot; i < ls; i++)
s[i] = s[i+1];
s[i] = '\0';
dot = ls - dot - 1;
dot *=n;
m = multi(s,s);
n--;
while(n > 1){
e = multi(m,s);
free(m);
m = e;
n--;
}
if(dot > 0){
lm = strlen(m);
i = lm;
while(dot>0){
m[i] = m[i-1];
i--;
dot--;
}
m[i] = '.';
i = lm;
while(m[i] == '0'){
m[i] = '\0';
i--;
}
if(m[i] == '.')
m[i] = '\0';
i = 0;
while(m[i] == '0')
i++;
e = m+i;
}
else{
while(m[i] == '0')
i++;
e = m+i;
}
printf("%s\n",e);
free(m);
}
return 0;
} 展开
#include <string.h>
#include <stdlib.h>
char* multi(char* a, char* b){
int la = strlen(a);
int lb = strlen(b);
int lc = la+lb;
char* c = (char*)malloc(lc+2);
int i, j, k;
memset(c,0,lc+2);
for(i = lb-1;i >= 0; i--)
for(j = la-1, k = lc - (lb - i); j >= 0; j--,k--){
int tmp;
c[k] += (a[j]-'0') * (b[i]-'0');
tmp = c[k]/10;
if(tmp > 0){
c[k-1] +=tmp;
c[k] -=tmp*10;
}
}
for(i = 0; i < lc; i++)
c[i]+='0';
return c;
}
int main(void){
char s[7];
int n;
char* m, *e;
int lm,ls;
//FILE* f = fopen("test.txt","r");
while(scanf("%s%d",s,&n) != EOF){
//while(fscanf(f,"%s%d",s,&n) != EOF){
int dot = 0;
int i;
int lm;
if(n == 0){
printf("%d\n",1);
continue;
}
if(n == 1){
printf("%s\n",s);
continue;
}
ls = strlen(s);
while(s[dot] != '.'&& dot < ls)
dot++;
for(i = dot; i < ls; i++)
s[i] = s[i+1];
s[i] = '\0';
dot = ls - dot - 1;
dot *=n;
m = multi(s,s);
n--;
while(n > 1){
e = multi(m,s);
free(m);
m = e;
n--;
}
if(dot > 0){
lm = strlen(m);
i = lm;
while(dot>0){
m[i] = m[i-1];
i--;
dot--;
}
m[i] = '.';
i = lm;
while(m[i] == '0'){
m[i] = '\0';
i--;
}
if(m[i] == '.')
m[i] = '\0';
i = 0;
while(m[i] == '0')
i++;
e = m+i;
}
else{
while(m[i] == '0')
i++;
e = m+i;
}
printf("%s\n",e);
free(m);
}
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询