1. 程式人生 > >NOIP 2017普及組複賽C/C++詳細題解報告

NOIP 2017普及組複賽C/C++詳細題解報告

一、題目

https://wenku.baidu.com/view/f3fe5a326ad97f192279168884868762cbaebb71.html?from=search

二、題解

第1題

這題很簡單,因為輸入的三個數都是十的整數倍,所以計算結果不會有小數。

#include <cstdio>

int main()
{
    FILE *fp1 = fopen("score.in", "r");
    FILE *fp2 = fopen("score.out", "w");

    int a, b, c, ans;
    fscanf(fp1, "%d %d %d", &a, &b, &c);
    ans = a / 5 + b * 3 / 10 + c / 2;
    fprintf(fp2, "%d\n", ans);

    fclose(fp1);
    fclose(fp2);

    return 0;
}

欲購完整答案請加微信307591841

第2題

(1)n <= 1000,則用氣泡排序的話,最多計算次數為1000 * 1000 = 100萬,複雜度沒有問題。
(2)所有的圖書編碼和需求碼均不超過1000萬,用整型儲存即可。

#include<stdio.h>
#include<math.h>
//using namespace std;

int n,q;
int a[1010];

void bubble_sort(int a[], int n)
{
    for(int round = 0; round < n - 1; round++)
    {
        for(int pos = 0; pos < n - round - 1; pos++)
        {
            if(a[pos] > a[pos + 1])
            {
                a[pos] ^= a[pos + 1];
                a[pos + 1] ^= a[pos];
                a[pos] ^= a[pos + 1];
            }
        }
    }
}

int main()
{
    freopen ("librarian.in", "r", stdin);
    freopen ("librarian.out", "w", stdout);
    scanf ("%d %d", &n, &q);
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }

    bubble_sort(a, n);

    for (int i = 0; i < q; i++)
    {
        int l, x;
        bool flag = 0;
        scanf("%d %d", &l, &x);
        int t = pow(10, l);             //計算t,即末尾0的個數
        for(int i = 0; i < n; i++)
        {
            if (0 == (a[i] - x) % t)    //重點:若x是a[i]的尾數,那麼(a[i]-x)%t==0
            {
                flag = 1;
                printf("%d\n", a[i]);
                break;
            }
        }
        if (!flag)
        {
            printf("-1\n");
        }
    }

    return 0;
}

想了解小朋友學程式設計請加QQ群581357582。
關注公眾號請掃描二維碼
qrcode_for_kidscode_258.jpg