Little Chris is bored during his physics lessons (too easy), so he has built a toy box to keep himself occupied. The box is special, since it has the abilitytochange gravity.
There are n columns of toy cubes in the box arranged in a line. The i-th column contains ai cubes. At first, the gravity in the box is pulling the cubes downwards. When Chris switches the gravity, it begins to pull all the cubes to the right side of the box. The figure shows the initial and final configurations of the cubes in the box: the cubes that have changed their position are highlighted with orange.
Given the initial configuration of the toy cubes in the box, find the amounts of cubes in each of the n columns after the gravity switch!
Input
The first line of input contains an integer n (1 ≤ n ≤ 100), the number of the columns in the box. The next line contains n space-separatedintegernumbers. The i-th number ai (1 ≤ ai ≤ 100) denotes the number of cubes in the i-th column.
Output
Output n integer numbers separated by spaces, where the i-th number is the amount of cubes in the i-th column after the gravity switchEg4 2 34 5 4 PUTOUT 2 4 5 34
解题思路 最初朝着把n个数输出,减去其中最小的这个方向一直想,没想出来,后来看到右边这图从左到右逐渐增大所以联系右边图,感觉是一个比较大小的程序,又理解题意明白其实就是一个排序题,1循环输入2从小到大排列3,循环输出
#include<stdio.h#define num 101int main(){ int a,b,d,e,f,ss;
int c[100];scanf("%d",&a); { for(b=1; b<=a; b++) { scanf("%d",&c[b]); }
for(d=1; d<a; d++) { for(e=d+1; e<=a; e++)
{ if(c[d]>c[e])
{ ss=c[d];c[d]=c[e]; c[e]=ss;
}
}
}
for(f=1; f<=a; f++) { printf("%d ",c[f]); } }return 0;}
A Gravity Flip
最新推荐文章于 2021-02-23 22:55:58 发布