输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若干个‘0’组成的,这时这个整数就是0)。
你的任务是:对这些分割得到的整数,依从小到大的顺序排序输出。
初玩can,接触第三天的一道水题
第一程序为最初所写,第二个为参考优化后,
#include"stdio.h"
main()
{
char c;
while((scanf("%c",&c))!=EOF)
{
char a[1300];
long long int b[1300],sum=0;
int state=0,temp,k=0,i,j,l;
for(i=0;c!='\t'&&c!='\n'&&c!=' ';i++)
{
a[i]=c;
scanf("%c",&c);
}
a[i]='\0';
for(j=0;j<i;j++)
{
if(a[j]=='5')
{
if(state==1)
b[k++]=sum;
state=0;
sum=0;
}
else if(a[j]=='0')
{
state=1;
sum=sum*10+a[j]-'0';
}
else
{
sum=sum*10+a[j]-'0';
state=1;
}
}
if(state==1)
b[k++]=sum;
k--;
for(j=0;j<k;j++)
for(l=0;l<k-j;l++)
{
if(b[l]>b[l+1])
{
temp=b[l];
b[l]=b[l+1];
b[l+1]=temp;
}
}
for(i=0;i<=k-1;i++)
{
printf("%lld ",b[i]);
}
printf("%lld\n",b[i]);
}
}
2.
十一月 | ||||||
---|---|---|---|---|---|---|
日 | 一 | 二 | 三 | 四 | 五 | 六 |
27 | 28 | 29 | 30 | 31 | 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
2023年2月01日 20:21
Are you looking for a way to sort and output non-negative integers in ascending order? Look no further! This program provides a simple solution. It takes a line of numbers diamond rings and, by treating the '5' as a space, splits it into non-negative integers which are then sorted and output in ascending order. Written originally on the first day and optimized for reference on the third, this program is an effective solution for your sorting needs!