• <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

            好友

            同學

            網友

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            热RE99久久精品国产66热| 久久久久人妻一区精品果冻| 久久精品国产亚洲AV不卡| 色诱久久久久综合网ywww | 国产精品久久久亚洲| 久久国产精品99久久久久久老狼| 国产精品伦理久久久久久| 欧美亚洲国产精品久久高清| 久久久久国产精品熟女影院 | 成人亚洲欧美久久久久| 国内精品伊人久久久久妇| 97久久超碰国产精品旧版| 精品国产青草久久久久福利| 国产精品VIDEOSSEX久久发布| 亚洲中文字幕无码久久综合网| 久久久久国产亚洲AV麻豆| 99999久久久久久亚洲| 亚洲国产另类久久久精品黑人| 色天使久久综合网天天| 国产999精品久久久久久| 国产产无码乱码精品久久鸭| 久久久久国产精品嫩草影院| 手机看片久久高清国产日韩| 国产精品九九久久免费视频 | 久久久久久国产精品免费免费 | 久久亚洲精品国产亚洲老地址 | 中文无码久久精品| 久久精品桃花综合| 久久久国产99久久国产一| 伊人久久成人成综合网222| 九九久久精品无码专区| 99久久国产综合精品网成人影院| 久久青草国产手机看片福利盒子| 欧美午夜精品久久久久免费视| 亚洲国产一成人久久精品| 国内精品综合久久久40p| 狠狠色丁香久久婷婷综合_中| 欧美麻豆久久久久久中文| 中文字幕精品无码久久久久久3D日动漫 | 亚洲中文字幕无码久久精品1| 国产成人精品久久|