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

第一个样例都输出不正确的代码为什么可以ac?

Posted by shishaobing at 2011-08-08 17:51:34 on Problem 3340
在我的编译器中,这个代码输出第一个样例是99.难道pow的精度跟编译器有关?


#include "stdio.h"
#include "memory.h"
#include "string.h"
#include "math.h"

char w[12],x[12];

int countddz()
{
    int len,m,i,j,pos[12],h,t,r,result,numx,numw,d;
    char w1[12];

    len = strlen(w);
    m = 0;
    for (i = 0; i <= len - 1; i ++)
        if (w[i] == '?')
        {
            m ++;
            pos[m] = i;
        }
    h = 0;
    t = (int)pow(10.0,m);

    result = -1;

    if (m == 0)
        result = 0;
    if (result == -1)
    {
        strcpy(w1,w);
        for (i = 0; i <= len - 1; i ++)
            if (w[i] == '?')
                w1[i] = 9 + '0';
        if (strcmp(w1,x) < 0) result = 0;
        for (i = 0; i <= len - 1; i ++)
            if (w[i] == '?')
                w1[i] = 0 + '0';
        if (strcmp(w1,x) > 0)
            result = t;
    }

    if (result == -1)
    {
        numx = 0;
        for (i = len - 1; i >= 0; i --)
            numx += (x[i] - '0') * (int)pow(10.0,len - 1 - i);

        strcpy(w1,w);

        while (t - h > 1)
        {
            r = (t + h) / 2;
            for (i = m; i >= 1; i --)
            {
                d = r % 10;
                r = r / 10;
                w1[pos[i]] = d + '0';
            }
            if (strcmp(w1,x) > 0)
                t = (t + h) / 2;
            else
                h = (t + h) / 2;
        }

        r = t;
        for (i = m; i >= 1; i --)
        {
            d = r % 10;
            r = r / 10;
            w1[pos[i]] = d + '0';
        }
        if (strcmp(w1,x) <= 0)
            result = (int)pow(10.0,m) - 1 - t;
        r = h;
        for (i = m; i >= 1; i --)
        {
            d = r % 10;
            r = r / 10;
            w1[pos[i]] = d + '0';
        }
        if (strcmp(w1,x) <= 0)
            result = (int)pow(10.0,m) - 1 - h;
    }

    printf("%d\n",result);

    return 0;
}

int main()
{
    while (scanf("%s",w) != EOF && strcmp(w,"#") != 0)
    {
        scanf("%s",x);
        countddz();
    }
    return 0;
}

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