题解:P15063 [UOI 2024 II Stage] Creating an Array

· · 题解

水题

本蒟蒻的第一发题解,求过

传送门

题目大意:

题面比较复杂,其实就是输入数字0~9的个数,然后排序输出。注意要是倒序输出,因为题目要求是最大的,你也可以在排序时按从大到小排,注意要在排序函数加一个语句,具体实现方法:

sort(a+1,a+n+1,greater<int>());//a为你的数组名,n为数组元素个数,如果你的下标是从0开始的,要把+1去掉,否则会越界。

代码:

#include<bits/stdc++.h>
using namespace std;
int a[10];
int main(){
    for(int i=0;i<10;i++) cin>>a[i];
    for(int i = 9; i >= 0; i--)for(int j = 1; j <= a[i]; j++)cout << i << " ";
    return 0;
}

AC记录