POJ 1001 总结

#include <stdio.h>
#include <stdlib.h>


//#define DEBUG


static float R = 0;
static int n = 0;
static int slotP = -1;
static int iRArray[7] = {0};
static int iRLen = 0;
static int iResultArray[150] = {0};
static int iResultLen = 0;


void exponentiation();
void deleteSlot(char *);
void addSlot();
void init();


void init() {
    R = 0;
    n = 0;
    slotP = -1;
    iResultLen = 0;
    iRLen = 0;
    for (int i = 0; i < 7; i++) {
        iRArray[i] = 0;
    }


    for (int i = 0; i < 150; i++) {
        iResultArray[i] = 0;
    }
}


int main() {
    char cTempArray[7] = {0};


    while (scanf("%f%d", &R, &n) != EOF) {
        if (R == 0) {
            printf("0\n");
            continue;
        }


        snprintf(cTempArray, 7, "%f", R);
        deleteSlot(cTempArray);
#ifdef DEBUG
        printf("cTempArray=%s\n", cTempArray);
        fflush(stdout);
#endif
        for (int i = 6, j = 0; i >= 0; i--) {
            if (cTempArray[i] != 0) {
                iRArray[j] = cTempArray[i] - '0';
                j++;
                iRLen = iResultLen = j;
            }
        }
#ifdef DEBUG
        for (int i = 0; i < iRLen; i++) {
            printf("iRArray[%d]=%d\n", i, iRArray[i]);
            fflush(stdout);
        }
#endif


        exponentiation();
        addSlot();


        if (iResultArray[0] == 0) {
            for (int i = 0; i < iResultLen; i++) {
                if (i >= slotP) {
                    break;
                }


                if (iResultArray[i] != 0) {
                    break;   
                }


                iResultArray[i] = -2;
            }
        }


        for (int i = iResultLen -1; i >= 0; i--) {
            if (iResultArray[i] == -2) {
                continue;
            } else if (iResultArray[i] == -1) {
                printf(".");
            } else {
                printf("%d", iResultArray[i]);
            }
        }


        printf("\n");
        init();
    }
}


void exponentiation() {
    int iTempResultArray[150] = {0}; 
    int iTempResultLen = 0;


    for (int i = 0; i < iResultLen; i++) {
        iTempResultArray[i] = iRArray[i];
        iTempResultLen = iRLen;
    }


    for (int k = 0; k < n - 1; k++) {
#ifdef DEBUG
        printf("k=%d, iRLen=%d, iTempResultLen=%d\n", k, iRLen, iTempResultLen);
        printf("iTempResultArray=");
        for (int i = iTempResultLen - 1; i >= 0; i--) {
            printf("%d", iTempResultArray[i]);
        }
        printf("\n");
        fflush(stdout);
#endif
        for (int i = 0; i < iRLen; i++) {
            for (int j = 0; j < iTempResultLen; j++) {
                iResultArray[i + j] = iTempResultArray[j] * iRArray[i] + iResultArray[i + j];
            }
        }


        int iResultMaxLen = iTempResultLen + iRLen;
        for (int i = iResultMaxLen; i >= 0; i--) {
            if (iResultArray[i] != 0) {
                iResultLen = i + 1;
            }
        }


        for (int i = 0; i < iResultMaxLen; i++) {
            iResultArray[i + 1] = iResultArray[i]/10 + iResultArray[i + 1];
            iResultArray[i] = iResultArray[i] % 10;
        }


        for (int i = 0; i < iResultMaxLen; i++) {
            iTempResultArray[i] = iResultArray[i];
            iResultArray[i] = 0;
        }


        for (int i = iResultMaxLen; i >= 0; i--) {
            if (iTempResultArray[i] != 0) {
                iResultLen = iTempResultLen = i + 1;
                break;
            }
        }
    }


    for (int i = 0; i < iTempResultLen; i++) {
        iResultArray[i] = iTempResultArray[i];
    }
#ifdef DEBUG
    printf("iResultArray=");
    for (int i = iResultLen - 1; i >= 0; i--) {
        printf("%d", iResultArray[i]);
    }
    printf("\n");
    fflush(stdout);
#endif
}


void deleteSlot(char* cArray) {
    for (int i = 5; i >= 0; i--) {
        slotP++;
        if(cArray[i] == '.') {
            for (int j = 5; j > i; j--) {   //delete 0 in the end
                if (cArray[j] != '0') {
                    break;
                } else {
                    slotP--;
                    cArray[j] = 0;
                }
            }


            for(int j = i; j < 6; j++) {    //move array without slot
                cArray[j] = cArray[j + 1];
            }


            for (int j = 0; j <= 6; j++) { //delete 0 in the front
                if (cArray[j] == 0) {
                    continue;
                } else if (cArray[j] == '0') {
                    cArray[j] = 0;
                    continue;
                } else {
                    break;
                }
            }


            return;
        }
    }


    slotP = 0;
}


void addSlot() {
    if (slotP == 0) {
        return;
    }


    iResultLen++;
    slotP = slotP * n;
#ifdef DEBUG
    printf("slotP=%d\n", slotP);
    fflush(stdout);
#endif
    if (slotP >= iResultLen) {
        for (int i = iResultLen - 1; i < slotP; i++) {
            iResultArray[i] = 0;
        }


        iResultArray[slotP] = -1;
        iResultLen = slotP + 1;


        return;
    }


    int temp = iResultArray[slotP];
    int temp2;
    iResultArray[slotP] = -1;


    for (int i = slotP + 1; i < iResultLen; i++) {
        temp2 = iResultArray[i];
        iResultArray[i] = temp;
        temp = temp2;
    }
#ifdef DEBUG
    printf("iResultArray=");
    for (int i = iResultLen - 1; i >= 0; i--) {
        printf("%d", iResultArray[i]);
    }
    printf("\n");
    fflush(stdout);
#endif

}


1、花了一两个礼拜把这题AC了,期间研究过long double,了解了精度和取值范围的差别。虽然取值范围能够覆盖到本题,但精度无法满足。

2、最后几次提交由于code中存在一个不起眼的bug导致了几次WA,最后百度了一把,说要关注100.00  2这条用例,这条用例没跑过不是说没考虑到这场景,而是一个很low的bug导致的。用这条用例发现bug也算带点运气。更正成下面这段代码后就AC了。

        if (iResultArray[0] == 0) {
            for (int i = 0; i < iResultLen; i++) {
                if (i >= slotP) {
                    break;
                }


                if (iResultArray[i] != 0) {
                    break;   
                }


                iResultArray[i] = -2;
            }
        }

3、还了解了melloc和realloc,如果字符串数组申请了一定长度,需要改变长度是需要重新申请内存的,一般情况下不会直接在字符串后面增加加内存。

4、用字符数组无法直接处理本题,一定要使用整形数组,不然字符数组存的只能是-127到128范围的数值。

5、使用全局变量要注意初始化,不然会影响下一条case

6、数组最好跟一个有效数值长度的变量,不要每次都遍历数组,而且估算有效数值长度尽量要准确,如果长度预估太长,在遍历的时候很容易发生数组越界,第一次跑99.999 25 时就发生了数组越界

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25. 输入说明 The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9. 输出说明 The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer. 输入样例 95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12 输出样例 548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201 小提示 If you don't know how to determine wheather encounted the end of input: s is a string and n is an integer C++ while(cin>>s>>n) { ... } c while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want /*while(scanf(%s%d",s,&n)!=EOF) //this also work */ { ... } 来源 East Central North America 1988 北大OJ平台(代理

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值