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

               這是一道很簡單的題吧,大數(shù)都不需要用到,可是很悲劇wa了很久。確實(shí)寫題太不嚴(yán)謹(jǐn)了,出了好多bug,甚至題意都沒注意清楚。
               這種題我一直忘記忽略前導(dǎo)'0'。
               還有題目沒有給出最長的數(shù)字的長度,所以最好用string類。
               使用longlong之前最好已經(jīng)測試了OJ,是用%lld還是%I64d,如果OJ后臺是linux下的g++,只能是%lld,Windows下的MinGW32
            (Dev-C++也一樣用的是這個庫)要用%I64d才能正確。所以預(yù)賽之前需要對普通題進(jìn)行測試下。
               還有注意復(fù)合邏輯表達(dá)式是否寫正確了,最近經(jīng)常寫錯了,太郁悶了。
               給自己提個醒吧,校賽這種題再不能迅速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 閱讀(1521) 評論(2)  編輯 收藏 引用 所屬分類: 字符串

            評論

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

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

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

            StripHeadZero 全為0是返回2,一些地方有點(diǎn)bug  回復(fù)  更多評論   

            <2012年6月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            導(dǎo)航

            統(tǒng)計

            公告

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            me

            好友

            同學(xué)

            網(wǎng)友

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            色偷偷久久一区二区三区| 亚洲一级Av无码毛片久久精品| 蜜桃麻豆WWW久久囤产精品| 99久久综合国产精品免费| 国产精品对白刺激久久久| 久久99精品免费一区二区| 麻豆精品久久久久久久99蜜桃| 久久国产一区二区| 日韩人妻无码一区二区三区久久99| 99久久99这里只有免费费精品 | 日韩AV毛片精品久久久| 久久久无码精品亚洲日韩京东传媒 | 久久91综合国产91久久精品| 久久久久久久综合综合狠狠| 亚洲成色WWW久久网站| 国产精品熟女福利久久AV| 精品久久久久久亚洲精品| 亚洲精品成人久久久| 欧美日韩中文字幕久久伊人| 久久无码AV中文出轨人妻| 久久久精品久久久久特色影视| 国产精品一久久香蕉国产线看| 香蕉久久夜色精品国产2020 | 国产美女久久久| 亚洲伊人久久成综合人影院| 久久国产热这里只有精品| 成人久久久观看免费毛片| 久久精品a亚洲国产v高清不卡| 久久精品桃花综合| 午夜精品久久久久久久无码| 91性高湖久久久久| 99久久夜色精品国产网站| 久久AV高清无码| 久久久一本精品99久久精品66| 久久精品国产亚洲AV久| 婷婷久久五月天| 欧美日韩精品久久久免费观看| 99久久这里只精品国产免费| 久久久久久国产精品无码下载| 99久久做夜夜爱天天做精品| 久久99精品久久久大学生|