PAT 1060 Are They Equal?

来源:互联网 发布:java 工程师要考吗 编辑:程序博客网 时间:2024/05/20 06:38

题目链接:https://www.patest.cn/contests/pat-a-practise/1060

做题感受:这道题目考察的是字符串的处理能力,不过case太坑了,因为很多输入格式都没有说明,而且输出指定不明。比如需要考虑如下几种情况的输入:0, 0.0, 0.00, 或者是

0.00000123这种。而且输出的要求是比如n=5,输入0,输出应该为0.00000*10^0。第一次提交的时候只有19分,错了3个case,然后回来苦思冥想发现了很多地方自己没有考虑,改来改去提交上去还是19分...只能说明自己的思路有点混乱了,代码写的比较僵硬。最后看了看网上的思路,然后自己理清思路重写终于AC。

代码如下:(AC)

#include <iostream>#include <cstring>using namespace std;int convert(char c[], int n, char res[]){    int point, count;    point = count = 0;    int i = 0, j = 0;    while (c[i] == '0') i++;//略去前导0    if (c[i] == '.') //说明是0.###格式,有可能是0.123,也有可能是0.000000123,也有可能是0.00000..    {        i++;        bool isZero = true; //声明了一个变量来判断是不是0,如果遍历结束isZero仍然为true,就把指数置为0.        while (c[i] != '\0')        {            if (c[i] == '0' && isZero)            {                i++;                point--;            }             if (c[i] != '0' && c[i] != '\0')            {                isZero = false;            }            if (!isZero)            {                if (count < n)                {                    res[count++] = c[i++];                }                else                    break;            }        }        if (isZero)            point = 0;    }    else //说明是##.###的格式,当然有可能还是0    {        while (c[i] != '.' && c[i] != '\0')        {            if (count < n)            {                res[count++] = c[i];            }            point++;            i++;        }        i++;        while (c[i] != '\0')        {            if (count < n)            {                res[count++] = c[i++];            }            else                break;        }    }    while (count < n) //补后置0    {        res[count++] = '0';    }    return point;}int n;char A[105], B[105];char resA[105], resB[105];int main(){    scanf("%d %s %s",&n,A,B);    int eA = convert(A, n, resA);    int eB = convert(B, n, resB);    if (eA == eB && strcmp(resA, resB) == 0)    {        printf("YES 0.%s*10^%d\n",resA,eA);    }    else    {        printf("NO 0.%s*10^%d 0.%s*10^%d\n",resA,eA,resB,eB);    }    return 0;}
总结:水平有待提高,还得多敲代码。


0 0
原创粉丝点击