| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 843 人关注过本帖
标题:杭电OJ1063,测试样例成功,提交不过,欢迎大家测试排错。
只看楼主 加入收藏
zbjzbj
Rank: 12Rank: 12Rank: 12
来 自:郑州
等 级:贵宾
威 望:52
帖 子:633
专家分:3045
注 册:2011-4-22
结帖率:100%
收藏
 问题点数:0 回复次数:6 
杭电OJ1063,测试样例成功,提交不过,欢迎大家测试排错。
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;
}



搜索更多相关主题的帖子: length str int point the 
2023-09-19 20:00
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:0 
我输入 .1 2 应当输出 .01,但你输出乱码
假如 “.1 2” 不是一个合格输入的话(我不能确定),那我输入 “10.0 2”,应该输出 100,但你输出 100.00
2023-09-20 08:44
zbjzbj
Rank: 12Rank: 12Rank: 12
来 自:郑州
等 级:贵宾
威 望:52
帖 子:633
专家分:3045
注 册:2011-4-22
收藏
得分:0 
回复 2楼 rjsp
谢谢,我测试了好多长数据,竟然没有发现问题。
我修改一下。
2023-09-20 11:21
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9031
专家分:54061
注 册:2011-1-18
收藏
得分:0 
回复 3楼 zbjzbj
char *p = str + length - 1;
当 length 等于0时,是不是越界了?我没细看,不确定
2023-09-20 11:26
zbjzbj
Rank: 12Rank: 12Rank: 12
来 自:郑州
等 级:贵宾
威 望:52
帖 子:633
专家分:3045
注 册:2011-4-22
收藏
得分:0 
#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编辑过]

2023-09-20 14:09
zbjzbj
Rank: 12Rank: 12Rank: 12
来 自:郑州
等 级:贵宾
威 望:52
帖 子:633
专家分:3045
注 册:2011-4-22
收藏
得分:0 
回复 4楼 rjsp
str只是开辟了缓存,p是借用这个地盘进行运算。
根据题意,6位数,最高25次方运算,最大位数150位,只要后面不越界,前面没有越界问题。
p在str缓存里面是右对齐的,p的尾部对齐str的尾减二。留一个字节给 '\0',一个字节给添加小数点后的调整。
杭电OJ不反馈错误内容,只反馈对错认定,费脑筋。

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

2023-09-20 14:17
forever74
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:CC
等 级:版主
威 望:58
帖 子:1705
专家分:4345
注 册:2007-12-27
收藏
得分:0 
一位测试工程师走进酒吧
应该很有启发

对宇宙最严谨的描述应该就是宇宙其实是不严谨的
2023-09-20 15:51
快速回复:杭电OJ1063,测试样例成功,提交不过,欢迎大家测试排错。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019368 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved