请教一下各位大佬关于poj1001超时问题
用的是vs2017的环境,我在自己的电脑中测试并没有超时啊!程序代码:
/* 命名法: 函数:蛇形命名 变量:小驼峰法 思路:定义一个结构体,包含字符串指针与和两个整型变量,一个记录小数点位置,一个记录数字末尾(小数点并不存放于字符串中) */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define BUFFER_SIZE_MAX 200 typedef struct { char *data;//指向字符串 int pi;//数字末尾 int decimalPi;//小数点位置 }FloatNum; void float_square_calculattion(char *data, int pow); int main() { char douleStr[200]; int pow; while (scanf("%200s%d", douleStr, &pow) == 2) float_square_calculattion(douleStr, pow); return 0; } FloatNum *str_to_FloatNum(char *data);//将字符串转换为结构体 FloatNum *reverse(FloatNum *Object);//逆序 FloatNum *float_creat_image(const FloatNum *constData);//声明拷贝函数 FloatNum *float_operation(const FloatNum *dataNum, FloatNum *dataImage, FloatNum *resultContainer, int pow);//声明运算函数 void str_set_data(char *, char val, int num); void float_square_calculattion(char *str, int pow) { if (*str == '-') {//如果为负则先打印负号,并跳过字符串中的负号 putchar('-'); ++str; } FloatNum *dataNum = str_to_FloatNum(str); FloatNum *dataImage = float_creat_image(dataNum);//创建dataNum的镜像文件 FloatNum *resultContainer = (FloatNum *)malloc(sizeof(FloatNum));//创建一个结果容器 resultContainer->data = (char *)malloc(BUFFER_SIZE_MAX); str_set_data(resultContainer->data, '0', BUFFER_SIZE_MAX); FloatNum *result = float_operation(dataNum, dataImage, resultContainer, pow);//由于不确定最后到底返回容器还是镜像文件,故用另一个指针接收 int resultPi;//result的字符串偏移量 if (result->data[0] != '0') for (resultPi = 0; resultPi < result->decimalPi; resultPi++) putchar(result->data[resultPi]); else resultPi = 1; if (result->decimalPi < result->pi) putchar('.'); printf("%s\n", result->data + resultPi); free(dataNum); free(dataImage->data); free(dataImage); free(resultContainer->data); free(resultContainer); } FloatNum *str_to_FloatNum(char *str) { FloatNum *temp = (FloatNum *)malloc(sizeof(FloatNum)); temp->data = (char *)malloc(BUFFER_SIZE_MAX); int tempPi; for (tempPi = 0; str[tempPi] != '.' && str[tempPi]; tempPi++) temp->data[tempPi] = str[tempPi]; temp->decimalPi = tempPi; if (str[tempPi]) {//判断字符串整数部分后面是小数点还是结束符 ++str; while (str[tempPi]) temp->data[tempPi] = str[tempPi++]; } temp->data[tempPi--] = 0; if (temp->decimalPi <= tempPi) while (temp->data[tempPi] == '0') temp->data[tempPi--] = 0; temp->pi = tempPi; return temp; } FloatNum* reverse(FloatNum *Object) { char temp; for (int i = Object->pi, g = 0; g < i; g++, i--) { temp = Object->data[i]; Object->data[i] = Object->data[g]; Object->data[g] = temp; } return Object; } FloatNum *float_creat_image(const FloatNum *constData) { FloatNum *tempBuffer = (FloatNum *)malloc(sizeof(FloatNum)); tempBuffer->data = (char *)malloc(BUFFER_SIZE_MAX); strcpy(tempBuffer->data, constData->data); tempBuffer->decimalPi = constData->decimalPi; tempBuffer->pi = constData->pi; str_set_data(tempBuffer->data + tempBuffer->pi + 2, '0', BUFFER_SIZE_MAX - 2 - tempBuffer->pi); return tempBuffer; } FloatNum *float_operation(const FloatNum *dataNum, FloatNum *dataImage, FloatNum *resultContainer, int pow) { int tempBuffer = 0; for (int dataPi = dataNum->pi; dataPi >= 0; dataPi--) { resultContainer->pi = dataNum->pi - dataPi; for (int imagePi = dataImage->pi; imagePi >= 0; imagePi--) { tempBuffer = (dataNum->data[dataPi] - '0') * (dataImage->data[imagePi] - '0'); resultContainer->data[resultContainer->pi] += tempBuffer % 10; tempBuffer /= 10; while (resultContainer->data[resultContainer->pi] > '9') { resultContainer->data[resultContainer->pi] -= 10; tempBuffer++; } resultContainer->data[++resultContainer->pi] += tempBuffer; tempBuffer = 0; } } if (resultContainer->data[resultContainer->pi] != '0') resultContainer->data[resultContainer->pi + 1] = 0; else resultContainer->data[resultContainer->pi--] = 0; if (resultContainer->decimalPi) resultContainer->decimalPi = resultContainer->pi - dataNum->pi + dataNum->decimalPi - dataImage->pi + dataImage->decimalPi - 1;//计算小数点位置:容器末尾+1 分别减去 原数和镜像的末尾 + 1 - 二者小数点位置 if (pow == 2) return reverse(resultContainer); str_set_data(dataImage->data, '0', dataImage->pi + 2); return float_operation(dataNum, reverse(resultContainer), dataImage, pow - 1);//进行递归调用,容器作为镜像,镜像清空数据作为容器 } void str_set_data(char *str, char val, int num) { for (int i = 0; i < num; i++) str[i] = val; }