思路
此题较为简单,简述一下思路。
从小到大排序,每次选取最小值,只要不为0即可
每次都为序列减去一个数字太慢,但每个数又减去的数字一样,所以可以用minus记录每个数要减去的数
C++代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=100010;
int n,m;
int a[N];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
sort(a+1,a+n+1);
int minus=0,i=1;
while(m--)
{
if(i>n)
{
printf("0\n");
continue;
}
if(a[i]-minus!=0)
{
printf("%d\n",a[i]-minus);
}
else
printf("0\n");
minus+=a[i]-minus;
i++;
while(a[i]-minus==0&&i<=n)i++;
}
return 0;
}
标签:int,题解,4495,减去,printf,include,minus,AcWing
From: https://www.cnblogs.com/PlayWithCPP/p/17182831.html