I am doing a problem about calcuating first digit of a^p form(a and p are non-negative integer. The total length of a^p never exceed 200). a and p may be a very large number. So I wanna make variable a to double type. and then a always range between 0 < a < 10. Like a = 1234 --> 1.234. But the answer is wrong explained by http://acm.hnu.cn:8080/online/?actio...=show&id=10695. Notice: The input and output field can be seperated itself.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_S 300
double str2double(char *s)
{
double a;
int i;
s[strlen(s)] = '\0';
if (strlen(s) > 1) {
s[strlen(s)+1] = '\0';
for (i = strlen(s); i >= 2; i--)
s[i] = s[i-1];
s[1] = '.';
}
a = atof(s);
return a;
}
int firstDigit(double a, int p)
{
int i;
double x = a;
for (i = 1; i < p; i++) {
x *= a;
if (x >= 10) x /= 10;
}
return (int)x;
}
int main()
{
char s[MAX_S];
int n, p, x, i;
double a;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%s %d", s, &p);
printf("%s\n", s);
a = str2double(s);
x = firstDigit(a, p);
printf("%d\n", x);
}
return 0;
}

I don't know what is the error here. if everyone know, please reply. Thanks.