Detailed answers to 2008 Beijing primary school students' program design friendship competition

First questions

Analysis: because the number of decimal places is not fixed, double cannot be used, so only character array can be used.

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    freopen("first.in", "r", stdin);
    freopen("first.out", "w", stdout);

    string a; // Or char a[10000];
    cin >> a;
    cout << a;

    return 0;
}

Second questions

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
//    freopen("count.in", "r", stdin);
//    freopen("count.out", "w", stdout);

    int n;
    cin >> n;
    int num = 1;    // Number of each floor
    int total = 1;  // Number of all layers
    for(int i = 2; i <= n; i++)
    {
        num += i;
        total += num;
    }

    cout << total << endl;

    return 0;
}

Third questions

Analysis: This paper investigates bubble sorting.

Solution 1:

#include <iostream>
#include <cstdio>
using namespace std;

#define swapTube(i, j) swap(len[i], len[j]);swap(dia[i], dia[j]);swap(code[i], code[j])

int main()
{
//    freopen("best.in", "r", stdin);
//    freopen("best.out", "w", stdout);

    int n;
    cin >> n;
    int len[n], dia[n], code[n];
    for(int i = 0; i < n; i++)
    {
        cin >> len[i] >> dia[i] >> code[i];
    }

    for(int i = 0; i < n - 1; i++)
    {
        for(int j = i + 1; j < n; j++)
        {
            if(len[i] < len[j])
            {
                // Length from large to small
                swapTube(i, j);
            }
            else if(len[i] == len[j])
            {
                if(dia[i] > dia[j])
                {
                    // Diameter from small to
                    swapTube(i, j);
                }
                else if(dia[i] == dia[j])
                {
                    if(code[i] < code[j])
                    {
                        // Number from large to small
                        swapTube(i, j);
                    }
                }
            }
        }
    }

    cout << code[0] << endl;

    return 0;
}

Solution two:

#include <iostream>
#include <cstdio>
using namespace std;

struct steelTube
{
    int len;
    int dia;
    int code;
};

int main()
{
    freopen("best.in", "r", stdin);
    freopen("best.out", "w", stdout);

    int n;
    cin >> n;
    steelTube tube[n];
    for(int i = 0; i < n; i++)
    {
        cin >> tube[i].len >> tube[i].dia >> tube[i].code;
    }

    for(int i = 0; i < n - 1; i++)
    {
        for(int j = i + 1; j < n; j++)
        {
            if(tube[i].len < tube[j].len)
            {
                // Length from large to small
                swap(tube[i], tube[j]);
            }
            else if(tube[i].len == tube[j].len)
            {
                if(tube[i].dia > tube[j].dia)
                {
                    // Diameter from small to large
                    swap(tube[i], tube[j]);
                }
                else if(tube[i].dia == tube[j].dia)
                {
                    if(tube[i].code < tube[j].code)
                    {
                        // Number from large to small
                        swap(tube[i], tube[j]);
                    }
                }
            }
        }
    }

    cout << tube[0].code << endl;

    return 0;
}

Reflection:

1 why can I use the swap() function directly for the custom structure steelTube?
2. If you define a function swap (? Try it.

Please add wechat 307591841 or QQ307591841 for complete answer


Public address.jpg

Posted by jaku78 on Wed, 04 Dec 2019 01:23:08 -0800