• <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 閱讀(1527) 評論(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影片| 久久精品国产亚洲AV电影| 久久夜色精品国产网站| 久久精品国产精品青草app| 国产精品青草久久久久福利99| 久久综合成人网| 久久精品aⅴ无码中文字字幕重口 久久精品a亚洲国产v高清不卡 | 久久乐国产综合亚洲精品| 久久久久久久尹人综合网亚洲| 久久久久国产精品嫩草影院| 欧美日韩精品久久久久| 久久精品中文无码资源站| 污污内射久久一区二区欧美日韩| 97久久精品午夜一区二区| 久久综合伊人77777麻豆| 国产精品美女久久久| 中文国产成人精品久久不卡| 久久国产免费观看精品| 亚洲AV无码一区东京热久久| 久久国产成人午夜AV影院| 国产成人精品白浆久久69| 久久亚洲日韩看片无码| 九九久久精品无码专区| 99久久人妻无码精品系列蜜桃| 欧美久久一级内射wwwwww.| 狠狠色丁香久久婷婷综合五月 | 一本色道久久88综合日韩精品| 久久精品人成免费| 丰满少妇高潮惨叫久久久| 久久久久国产精品人妻| 久久这里有精品| 久久93精品国产91久久综合| 久久青青草原国产精品免费| avtt天堂网久久精品| 久久久无码精品亚洲日韩按摩| 狠狠色丁香久久婷婷综合蜜芽五月 | 办公室久久精品| 久久精品9988| 久久综合九色综合97_久久久| 99久久精品国产综合一区|