注册 登录
编程论坛 C语言论坛

杭电OJ1063,测试样例成功,提交不过,欢迎大家测试排错。

zbjzbj 发布于 2023-09-19 20:00, 820 次点击
Problem Description
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.
 

Input
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.
 

Output
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.
 

Sample Input
95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12
 

Sample Output
548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201
程序代码:
#include <stdio.h>
#include <string.h>
#define length 200
char* mult(char *p, long m);
int main()
{
    char str[length + 2];
    char in[10];
    int point;
    long num;
    int n;
    while (scanf("%s%d", in, &n) != EOF)
    {
        memset(str, 0, length + 2);
        int len = strlen(in);
        point = 0;
        for (int i = 0; i < 8; i++)
        {
            if (in[i] == '.')
            {
                point = i;
                break;
            }
        }
        if (point)
        {
            for (int i = point; i < len; i++)//暂去小数点,化成整数运算
            {
                in[i] = in[i + 1];
            }
            point = len - point - 1;//计算小说位数
        }
        sscanf(in, "%ld", &num);
        if (num == 0)
        {
            printf("0\n");
            continue;
        }
        char *p = str + length - 1;
        *(p + 0) = '1';
        for (int i = 0; i < n; i++)
        {
            p = mult(p, num);
        }
        point *= n;
        if (point)
        {
            for (int i = length; i > length - point; i--)//添加小数点
                str[i] = str[i - 1];
            str[length - point] = '.';
            for (int i = length; i > length - point; i--)//消除后位0
            {
                if (str[i] == '0')
                    str[i] = 0;
                else break;
            }
            for (int i = length - point; i < length; i++)//避免小数点后无效
            {
                if (str[i + 1] == 0)
                    str[i + 1] = '0';
                else break;
            }
        }
        p = str;//重置p
        while (*(p + 0) == '0' || *(p + 0) == '\0')//消除前导0
            p++;
        printf("%s\n", p);
    }
}

char* mult(char *p, long m) //乘法函数
{
    int len = strlen(p);
    int i = len - 1;
    long int n, n2 = 0/*进位*/;
    while (i >= 0)
    {
        n = *(p + i) - '0';
        n = n * m + n2;
        n2 = n / 10;
        n %= 10;
        *(p + i) = n + '0';
        i--;
    }
    while (n2 != 0)
    {
        *(p - 1) = n2 % 10 + '0';
        n2 /= 10;
        p--;
    }
    return p;
}



6 回复
#2
rjsp2023-09-20 08:44
我输入 .1 2 应当输出 .01,但你输出乱码
假如 “.1 2” 不是一个合格输入的话(我不能确定),那我输入 “10.0 2”,应该输出 100,但你输出 100.00
#3
zbjzbj2023-09-20 11:21
回复 2楼 rjsp
谢谢,我测试了好多长数据,竟然没有发现问题。
我修改一下。
#4
rjsp2023-09-20 11:26
回复 3楼 zbjzbj
char *p = str + length - 1;
当 length 等于0时,是不是越界了?我没细看,不确定
#5
zbjzbj2023-09-20 14:09
#include <stdio.h>
#include <string.h>
#define length 200
char* mult(char *p, long m);
int main()
{
    char str[length + 2];
    char in[10];
    int point;
    long num;
    int n;
    while (scanf("%s%d", in, &n) != EOF)
    {
        memset(str, 0, length + 2);
        int len = strlen(in);
        point = 0;
        for (int i = 0; i < 8; i++)
        {
            if (in[i] == '.')
            {
                point = i + 1;
                break;
            }
        }
        if (point)
        {
            for (int i = point; i <= len; i++)//暂去小数点,化成整数运算
                in[i - 1] = in[i];
            point = len - point;//计算小说位数
        }
        sscanf(in, "%ld", &num);
        if (num == 0)
        {
            printf("0\n");
            continue;
        }
        char *p = str + length - 1;//初始化被乘数
        *(p + 0) = '1';
        for (int i = 0; i < n; i++)
            p = mult(p, num);
        point *= n;
        if (point)
        {
            for (int i = length; i > length - point; i--)//添加小数点
                str[i] = str[i - 1];
            str[length - point] = '.';
            for (int i = length; i > length - point - 1; i--) //消除后位0或者小数点
            {
                if (str[i] == '0' || str[i] == '.')
                    str[i] = 0;
                else break;
            }
            if (str[length - point - 1] == 0)
            {
                for (int i = length - point; i < length; i++)//没有整数位时,避免小数点后无效
                {
                    if (str[i + 1] == 0)
                        str[i + 1] = '0';
                    else break;
                }
            }
        }
        p = str;//重置p
        while (*(p + 0) == '0' || *(p + 0) == '\0')//消除前导0
            p++;
        printf("%s\n", p);
    }
}

char* mult(char *p, long m) //乘法函数
{
    int len = strlen(p);
    int i = len - 1;
    long int n, n2 = 0/*进位*/;
    while (i >= 0)
    {
        n = *(p + i) - '0';
        n = n * m + n2;
        n2 = n / 10;
        n %= 10;
        *(p + i) = n + '0';
        i--;
    }
    while (n2 != 0)
    {
        *(p - 1) = n2 % 10 + '0';
        n2 /= 10;
        p--;
    }
    return p;
}



AC
再一次谢谢版主!!

[此贴子已经被作者于2023-9-20 14:10编辑过]

#6
zbjzbj2023-09-20 14:17
回复 4楼 rjsp
str只是开辟了缓存,p是借用这个地盘进行运算。
根据题意,6位数,最高25次方运算,最大位数150位,只要后面不越界,前面没有越界问题。
p在str缓存里面是右对齐的,p的尾部对齐str的尾减二。留一个字节给 '\0',一个字节给添加小数点后的调整。
杭电OJ不反馈错误内容,只反馈对错认定,费脑筋。

[此贴子已经被作者于2023-9-20 14:27编辑过]

#7
forever742023-09-20 15:51
一位测试工程师走进酒吧
应该很有启发
1