Problem L: 插入一个数到数列中
Time Limit: 1 Sec Memory Limit: 64 MB
Submit: 15181 Solved: 8594
Description
已有一个排序好的数列:0 10 20 30 40 50 60 70 80,输入一个任意整数m,按序插入到正确位置,输出插入m后的数列。
Input
输入一个整数m
Output
输出插入m后的数列
Sample Input
35
-5
90
Sample Output
0 10 20 30 35 40 50 60 70 80
-5 0 10 20 30 40 50 60 70 80
0 10 20 30 40 50 60 70 80 90
#include <stdio.h>
#include <string.h>
int main()
{
int n;
while (scanf("%d",&n) != EOF)
{
int num[10],i;
int flag = 0;
for (i = 0;i < 9;i++)
{
num[i] = i*10;
}
for (i = 0;i < 8;i++)
{
if (n > num[i])
{
printf("%d ",num[i]);
}
else if (n < num[i] && flag == 0)
{
flag = 1;
printf("%d %d ",n,num[i]);
}
else
printf("%d ",num[i]);
}
if (flag == 0)
{
if (n > num[8])
printf("%d %d\n",num[8],n);
else
printf("%d %d\n",n,num[8]);
}
else
printf("%d\n",num[8]);
}
return 0;
}
这里注意分情况讨论把,加油