2020:
#include <bits/stdc++.h>
using namespace std;
#define fr(i, r, n) for(int i = r; i < n; ++i)
const int maxn = (int)1e5 +10;
bool cmp(int a, int b)
{
return abs(a)>abs(b);
}
int main()
{
int n;
int flag;
while(scanf("%d", &n), n)
// while(scanf("%d%d", &n, &m), n&&m)
{
int arr[105];
fr(i, 0, n)
{
scanf("%d", &arr[i]);
}
sort(arr, arr+n, cmp);//定义cmp排序
fr(i, 0, n)
{
printf("%d", arr[i]);
if(i < n-1)
printf(" ");
}
printf("\n");
}
return 0;
}
这里用cmp定义一下排序就可以了。
2021:
#include <bits/stdc++.h>
using namespace std;
#define fr(i, r, n) for(int i = r; i < n; ++i)
const int maxn = (int)1e5 +10;
int main()
{
int n, sum;
int flag;
while(scanf("%d", &n), n)
// while(scanf("%d%d", &n, &m), n&&m)
{
sum = 0;
int arr[105];
fr(i, 0, n)
{
scanf("%d", &arr[i]);
}
fr(i, 0, n)
{
while(arr[i]>=100)
{
arr[i] -= 100;
sum++;
}
if(arr[i]>=50)
{
arr[i] -= 50;
sum++;
}
while(arr[i]>=10)
{
arr[i] -= 10;
sum++;
}
if(arr[i]>=5)
{
arr[i] -= 5;
sum++;
}
while(arr[i]>=2)
{
arr[i] -= 2;
sum++;
}
if(arr[i]>=1)
{
arr[i]--;
sum++;
}
}
printf("%d\n", sum);
}
return 0;
}
这里用了最简单的方法,就是从大到小一张一张去除,直至为零。但是感觉很不美观啊,下面定义了一个数组存取人民币面值,然后双for完事儿。
#include <bits/stdc++.h>
using namespace std;
#define fr(i, r, n) for(int i = r; i < n; ++i)
const int maxn = (int)1e5 +10;
int main()
{
int n, sum;
int flag;
int rmb[6] = {100, 50, 10, 5, 2, 1};
while(scanf(&#