Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

快要崩溃了,谁帮忙看一下

Posted by ahlongxp at 2006-10-05 15:54:29 on Problem 1001
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned long long _100k;
int findchar(char* s, char c, int startPos)
{
    int len = strlen(s);
    int i;
    for(i = startPos;i<len; ++i)
    {
        if(s[i]==c)
        {
            return i;
        }
    }
    return -1;
}
char* delzero(char*s)
{
    int pos = findchar(s,46,0);
    if(pos == -1) return s;
    int len = strlen(s);
    while(s[--len]=='0')
    {
        s[len] = 0;
    }
    return s;
}
int getdotPos(char*s, int n)
{
    int pos = findchar(s,46,0);
    if(pos == -1) return 0;
    return(strlen(s+pos+1)*n);
}
char* deldot(char*s)
{
    int len = strlen(s);
    if(len<1)return 0;
    int pos = findchar(s,46,0);
    if(pos == -1) return s;
    char tmpstr[200]={0};
    strncpy(tmpstr, s, pos);
    tmpstr[pos] = 0;
    strcat(tmpstr, s+pos+1);
    return strdup(tmpstr);
}
char* docalc(char*s, int n)
{
    long base = atol(s);
    _100k re[2][40];
    memset((void*)re, 0, sizeof(re));
    re[1][0]=base;//
    int i,j;
    int psize = 1;
    int newsize = 0;
    _100k overflow = 0;
    for(i=0; i<n-1; ++i)
    {
        memset((void*)re[i%2], 0, sizeof(re[0]));
        for(j=0; j<psize; ++j)
        {
            re[i%2][j] = re[1-(i%2)][j] * base + overflow;                                                                ;
            if(re[i%2][j]>99999)
            {
                overflow = re[i%2][j] / 100000;
                re[i%2][j] %= 100000;
                re[i%2][j+1] += overflow;
                if (psize <= j+1)
                {
                    newsize = psize + 1;
                    if (psize > sizeof(re[0]))
                    {
                        printf("overflow\n");
                        return;
                    }
                        //continue;
                }
            }
        }
        if (newsize > psize) psize = newsize;
        overflow = 0;
    }
    char tmpstr[200] = {0};
    char tmptmp[20];
    for( j=psize; j>0; --j)
    {
        sprintf(tmptmp, j!=psize?"%05d":"%d", re[1-i%2][j-1]);
        strcat(tmpstr, tmptmp);
    }
    return strdup(tmpstr);
}

char* addzero(char*s, int dotpos)
{
    if(dotpos==0)return s;
    int len = strlen(s);
    if(len<dotpos)
    {
        char tmpstr[200];
        memset((void*)tmpstr, '0', dotpos-len);
        tmpstr[dotpos+1-len] = 0;
        strcat(tmpstr, s);
        return strdup(tmpstr);

    }
    return s;
}
char* adddot(char*s, int dotpos)
{
    if(dotpos ==0)return s;
    int len = strlen(s);
    char tmpstr[200] ={0};
    strncat(tmpstr, s, len-dotpos);
    tmpstr[len-dotpos] = '.';
    tmpstr[len-dotpos+1] = 0;
    strcat(tmpstr, s+len-dotpos);
    return strdup(tmpstr);

}
char* doit (char s[], int n)
{
    if (!n || (atoi(s) == 0 &&findchar(s,46,0)==-1) || atof(s)==0.0)
    {
        return "1";
    }
    char* output;
    output = delzero(s);
    int dotpos = getdotPos(output, n);
    //printf("dotpos:%d \n", dotpos);
    output = deldot(output);
    output = docalc(output, n);
    output = addzero(output, dotpos);
    output = adddot(output, dotpos);
    return strdup(output);
}

int main (int argc, char *argv[])
{
	char s[7];
	int n;
	char output[200];
	while (scanf("%s %d", s, &n) == 2)
	{
	    puts(doit(s, n));
	}
	return 0;
}
测试数据:
95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12

0000 0
000.02 0
00001 1
000002 0
2.0 3
0000.50 3
测试结果:
548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201
1
1
1
1
8
.125

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator