• <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 閱讀(1518) 評論(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无码精品人妻糸列| 久久99热狠狠色精品一区| 人妻中文久久久久| 97精品久久天干天天天按摩 | 欧美国产成人久久精品| 中文国产成人精品久久不卡| 国产69精品久久久久99| 亚洲午夜久久久影院| 久久se精品一区二区影院| 成人免费网站久久久| 亚洲国产精品狼友中文久久久 | 久久精品成人免费看| 国产69精品久久久久久人妻精品 | 无码人妻久久一区二区三区免费丨 | 精品国产一区二区三区久久久狼| 爱做久久久久久| 久久国产欧美日韩精品| 亚洲精品乱码久久久久久蜜桃图片| 国产亚洲成人久久| 久久精品国产99国产电影网 | 色噜噜狠狠先锋影音久久| 亚洲中文久久精品无码ww16| 亚洲国产成人精品久久久国产成人一区二区三区综| 免费久久人人爽人人爽av| 天天综合久久一二三区| 久久久久久A亚洲欧洲AV冫| 国产精品热久久毛片| 久久成人国产精品一区二区| 国产三级精品久久| 久久久久无码中| 久久亚洲国产精品五月天婷| 久久久久亚洲AV成人网人人网站| 亚洲伊人久久大香线蕉苏妲己| 2020最新久久久视精品爱| 国产精品久久久久久久午夜片| 久久se这里只有精品| 国产精品99久久久久久宅男小说|