acm_チャレンジプログラミング:入門

22157 ワード

  • n+1問題
  • /* ************************************** 3n+1   **************************************    n  ,  n   ,    2;  n    ,    3 1.            。    n=1   。 **************************************     : // 1.           int   long (   int   long     4       )       //      ,      long long (8       )   (                //        ,                 )。 // 2.             ,      ,           WA      。 // 3.                ,          。 ************************************** */
    
    #include <iostream>
    using namespace std;
    
    #define min(a, b) ((a) <= (b) ? (a) : (b)) 
    #define max(a, b) ((a) >= (b) ? (a) : (b)) 
    #define MAXSIZE 10000
    int cache[MAXSIZE];
    
    //      
    int count(long long number)
    {
        if(number == 1)
            return 1;
        // 2        ,  2       
        if(number & 1)
            number += (number << 1)+1;
        else
            number >>= 1;
    
        // number    ,     
        if(number < MAXSIZE)
        {
            if(!cache[number])
                cache[number]=count(number);
            return 1+cache[number];
        }
    
        return 1+count(number);
    }
    
    int main(int ac, char* av[])
    {
        int first,second,start,end;
        while(cin>>first>>second){
            //     
             start = min(first, second);  
            end = max(first, second);  
    
            //      
            int  result=0, steps;
            for(int i=start; i <= end; i++)
                if((steps = count(i)) > result)
                    result=steps;
            //
            cout<<first << " " << second << " " << result << endl;  
        }
    
        return 0;
    }
  • 掃雷
  • // Minesweeper (  ) 
    
    // 
    // 
    // 
    //               ,               。        , 
    //         ,       bounds,                          8   
    //  ,                   。 
    
    #include <iostream> 
    #include <cstring> 
    
    using namespace std;  
    
    #define MAXSIZE 100 
    
    inline bool range_checking(int x, int y, int row, int column)  
    {  
        return (0 <= x && x < row) && (0 <= y && y < column);  
    }  
    
    void display(char matrix[][MAXSIZE], int row, int column)  
    {  
        int bounds[8][2] =  
            { {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0},  
            {1, 1} };  
    
        for (int i = 0; i < row; i++)  
        {  
            for (int j = 0; j < column; j++)  
            {  
                //                   。 
                if (matrix[i][j] == '*')  
                    cout << "*";  
                else  
                {  
                    //        8       。 
                    int mines = 0;  
                    for (int k = 0; k < 8; k++)  
                    {  
                        int m = i + bounds[k][0];  
                        int n = j + bounds[k][1];  
    
                        if (range_checking(m, n, row, column)  
                            && matrix[m][n] == '*')  
                            mines++;  
                    }  
    
                    cout << mines;  
                }  
            }  
    
            cout << endl;  
        }  
    }  
    
    int main(int ac, char *av[])  
    {  
        char matrix[MAXSIZE][MAXSIZE];  
        int row, column, field = 0;  
    
        while ((cin >> row >> column, row && column))  
        {  
            memset(matrix, 0, sizeof(matrix));  
    
            for (int i = 0; i < row; i++)  
                for (int j = 0; j < column; j++)  
                    cin >> matrix[i][j];  
    
            if (field > 0)  
                cout << endl;  
            field++;  
    
            cout << "Field #" << field << ":" << endl;  
    
            display(matrix, row, column);  
        }  
    
        return 0;  
    }  
    
  • 旅行
  • // The Trip (  )
    // PC/UVa IDs: 110103/10137, Popularity: B, Success rate: average Level: 1
    // Verdict: Accepted
    // Submission Date: 2011-04-09
    // UVa Run Time: 0.016s
    //
    //     (C)2011,  。metaphysis # yeah dot net
    //
    //     “    ”,               ,           1     ,  
    //               ,                  ,        9.666
    // 666,   9.67。         n,      n   。       ,     。   
    //      “    ”,           ,               ,        
    //      。            ,         。    ,         。  ,
    //            :0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,0.01,
    // 0.01,0.01,0.01,0.01,0.03,           0.01,           0.03    
    //     0.02   ,          1    ,         0.03        0.01   
    //             1    ,                          。
    //
    //            ,     ,             $100000,           ,
    //           ,    7      ,            $100000.00   ,    
    //         ,          ,        。
    
    #include <iostream>
    
    using namespace std;
    
    #define SIZE 1000
    
    
    double findChange(double *money, int total)  
    {  
        double average = 0.0, changeDown = 0.0, changeUp = 0.0, change = 0.0;  
    
        for (int i = 0; i < total; i++)  
            average += money[i];  
    
        average /= total;  
        average = (long) (average * 100 + 0.5) / 100.00;  
    
        for (int i = 0; i < total; i++)  
        {  
            if (money[i] > average)  
                changeUp += (money[i] - average);  
            else  
                changeDown += (average - money[i]);  
        }  
    
        if (changeDown > 0 && changeUp > 0)  
        {  
            if (changeDown > changeUp)  
                change = changeUp;  
            else  
                change = changeDown;  
        }  
        else  
        {  
            if (changeUp == 0 && changeDown == 0)  
                change = 0.0;  
            else  
            {  
                if (changeDown == 0)  
                {  
                    for (int i = 0; i < total; i++)  
                        if (money[i] > average)  
                            change +=  
                                (money[i] - average -  
                                0.01);  
                }  
                else  
                {  
                    for (int i = 0; i < total; i++)  
                        if (money[i] < average)  
                            change +=  
                                (average - money[i] -  
                                0.01);  
                }  
            }  
        }  
    
        return change;  
    }  
    
    int  main(int ac, char* av[])
    {
        int total;
        double money[SIZE], result=0.0;
    
    
        while(cin>>total,total){
            for (int i = 0; i < total; i++)  
                cin >> money[i];  
             result = findChange(money, total);  
    
            cout.precision(2);  
            cout.setf(ios::fixed | ios::showpoint);  
            cout << "$" << result << endl;  
        }
    
    }
  • LCD
  • // LC-Display (     )
    // PC/UVa IDs: 110104/706, Popularity: A, Success rate: average Level: 1
    // Verdict: Accepted 
    // Submission Date: 2011-04-09
    // UVa Run Time: 0.012s
    //
    //     (C)2011,  。metaphysis # yeah dot net
    
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    #define MAXLENGTH 8
    
    void lcd_display (long size, long number)
    {
        //  number        。
        int digits[MAXLENGTH];
    
        memset (digits, -1, sizeof (digits));
        if (number == 0)
            digits[MAXLENGTH - 1] = 0;
        else
        {
            for (int i = MAXLENGTH - 1; number > 0; i--)
            {
                digits[i] = number % 10;
                number /= 10;
            }
        }
    
        //            。
        string outline[5][10] = { 
            " - ", " ", " - ", " - ", " ", " - ", " - ", " - ", " - ", " - ",
            "| |", " |", " |", " |", "| |", "| ", "| ", " |", "| |", "| |",
            " ", " ", " - ", " - ", " - ", " - ", " - ", " ", " - ", " - ",
            "| |", " |", "| ", " |", " |", " |", "| |", " |", "| |", " |",
            " - ", " ", " - ", " - ", " ", " - ", " - ", " ", " - ", " - "
        };
    
        //     size         ,            size 。
        for (int row = 1; row <= (2 * size + 3); row++)
        {
            for (int i = 0; i < MAXLENGTH; i++)
                if (digits[i] != -1)
                {
                    string line;
                    if (row == 1)
                        line = outline[0][digits[i]];
                    if (2 <= row && row < (size + 2))
                        line = outline[1][digits[i]];
                    if (row == (size + 2))
                        line = outline[2][digits[i]];
                    if ((size + 3) <= row && row <= (2 * size + 2))
                        line = outline[3][digits[i]];
                    if (row == (2 * size + 3))
                        line = outline[4][digits[i]];
    
                    //       。
                    cout << line[0];
                    for (int j = 0; j < size; j++)
                        cout << line[1];
                    cout << line[2];
    
                    //             。
                    if (i < (MAXLENGTH - 1))
                        cout << " ";
                }
            cout << '
    '
    ; } } int main (int ac, char *av[]) { long size, number; while ((cin >> size >> number, size || number)) { lcd_display (size, number); cout << endl; } return 0; }