• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Uva 465 - Overflow

               這是一道很簡單的題吧,大數都不需要用到,可是很悲劇wa了很久。確實寫題太不嚴謹了,出了好多bug,甚至題意都沒注意清楚。
               這種題我一直忘記忽略前導'0'。
               還有題目沒有給出最長的數字的長度,所以最好用string類。
               使用longlong之前最好已經測試了OJ,是用%lld還是%I64d,如果OJ后臺是linux下的g++,只能是%lld,Windows下的MinGW32
            (Dev-C++也一樣用的是這個庫)要用%I64d才能正確。所以預賽之前需要對普通題進行測試下。
               還有注意復合邏輯表達式是否寫正確了,最近經常寫錯了,太郁悶了。
               給自己提個醒吧,校賽這種題再不能迅速A掉基本太丟人了。

               代碼如下:
            #include <stdio.h> 
            #include <limits.h>
            #include <string.h>
            #include <algorithm>
            using namespace std;
            #define MAX (10000)
            char szIntMax[20];
            char szLine[MAX];
            char szOne[MAX];
            char szTwo[MAX];
            char szOper[10];

            char* MyItoa(int nNum, char* pszNum, int nBase)
            {
                int nLen = 0;
                while (nNum)
                {
                    pszNum[nLen++] = nNum % nBase + '0';
                    nNum /= nBase;
                }
                reverse(pszNum, pszNum + nLen);
                pszNum[nLen] = '\0';
                
                return pszNum;
            }

            bool IsBigger(char* pszOne, int nLenOne, char* pszTwo, int nLenTwo)
            {
                //printf("pszOne:%s, pszTwo:%s\n", pszOne, pszTwo);
                if (nLenOne != nLenTwo)
                {
                    return nLenOne > nLenTwo;
                }
                else
                {
                    for (int i = 0; i < nLenOne; ++i)
                    {
                        if (pszOne[i] != pszTwo[i])
                        {
                            return pszOne[i] > pszTwo[i];
                        }
                    }
                    return false;
                }
            }

            int StripHeadZero(char* pszNum)
            {
                int nLen = strlen(pszNum);
                int i;
                
                for (i = 0; i < nLen && pszNum[i] == '0'; ++i);
                if (i == nLen)
                {
                    pszNum[0] = '0';
                    pszNum[1] = '\0';
                    nLen = 2;
                }
                else
                {
                    char* pszWrite = pszNum;
                    char* pszRead = pszNum + i;
                    nLen = 0;
                    while (*pszRead)
                    {
                        *pszWrite++ = *pszRead++;
                        ++nLen;
                    }
                    *pszWrite = '\0';
                }
                
                return nLen;
            }

            int main()
            {
                int nIntMax = INT_MAX;
                MyItoa(nIntMax, szIntMax, 10);
                int nLenMax = strlen(szIntMax);
                
                while (gets(szLine))
                {
                    if (szLine[0] == '\0')
                    {
                        continue;
                    }
                    
                    sscanf(szLine, "%s%s%s", szOne, szOper, szTwo);
                    printf("%s %s %s\n", szOne, szOper, szTwo);
                    StripHeadZero(szOne);
                    StripHeadZero(szTwo);
                    
                    int nLenOne = strlen(szOne);
                    int nLenTwo = strlen(szTwo);
                    bool bFirst = false;
                    bool bSecond = false;
                    
                    if (IsBigger(szOne, nLenOne, szIntMax, nLenMax))
                    {
                        printf("first number too big\n");
                        bFirst = true;
                    }
                    
                    if (IsBigger(szTwo, nLenTwo, szIntMax, nLenMax))
                    {
                        printf("second number too big\n");
                        bSecond = true;
                    }
                    
                    if (bFirst || bSecond)
                    {
                        if (szOper[0] == '+' || (szOper[0] == '*' && szOne[0] != '0' && szTwo[0] != '0'))
                        {
                            printf("result too big\n");
                        }
                    }
                    else
                    {
                        long long nOne, nTwo;
                        sscanf(szLine, "%lld%s%lld", &nOne, szOper, &nTwo);
                        long long nResult;

                        if (szOper[0] == '+')
                        {
                            nResult = nOne + nTwo;
                        }
                        else if (szOper[0] == '*')
                        {
                            nResult = nOne * nTwo;
                        }
                        //printf("%I64d\n", nResult);
                        if (nResult > INT_MAX)
                        {
                            printf("result too big\n");
                        }
                    }
                }
                
                return 0;
            }

            posted on 2012-04-03 17:11 yx 閱讀(1509) 評論(2)  編輯 收藏 引用 所屬分類: 字符串

            評論

            # re: Uva 465 - Overflow 2012-04-04 11:19 alafeizai

            。。。居然還有前置0問題。。。  回復  更多評論   

            # re: Uva 465 - Overflow 2012-06-27 23:01 Backer

            StripHeadZero 全為0是返回2,一些地方有點bug  回復  更多評論   

            <2012年4月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導航

            統計

            公告

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            me

            好友

            同學

            網友

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲国产精品无码久久久久久曰| 久久久久亚洲AV无码网站| 91精品国产91久久久久福利| 国产精品视频久久| 久久久不卡国产精品一区二区| 久久国产AVJUST麻豆| 久久精品视频免费| 99精品国产免费久久久久久下载| 99久久99这里只有免费费精品| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久精品一区二区三区AV| 亚洲精品乱码久久久久久按摩| 欧美精品一本久久男人的天堂| 久久亚洲AV成人无码| 国产精品午夜久久| 精品久久久久久久久午夜福利| 亚洲综合久久久| 国内精品免费久久影院| jizzjizz国产精品久久| 久久精品国产亚洲av麻豆蜜芽| 国产成人久久精品麻豆一区| 国产Av激情久久无码天堂| 一本色综合久久| 久久夜色精品国产| 精品人妻伦一二三区久久| 国产国产成人精品久久| 国产成人久久精品一区二区三区| 久久免费99精品国产自在现线 | 理论片午午伦夜理片久久| 日本免费久久久久久久网站| 久久久噜噜噜久久熟女AA片| 亚洲精品乱码久久久久久久久久久久 | 99热成人精品免费久久| 狼狼综合久久久久综合网| 久久精品国产乱子伦| 亚洲国产成人久久笫一页| 久久精品国产一区二区三区| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 伊人久久精品无码二区麻豆| 日韩欧美亚洲综合久久| 奇米综合四色77777久久|