C语言问题:从键盘输入十个整数,用选择排序法对输入的数据从小到大的顺序进行排序,将排序后的结果输出

#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int a[10],t,i,j;
for(i=0;i<=9;i++)
scanf("%d\n",&a[i]);
for(i=0;i<=9;i++)
{for(j=i+1;j<=9;j++)
if(a[i]>a[j])
{t=a[i];a[i]=a[j];a[j]=t;}
}
for(i=0;i<=9;i++)
printf("%d",a[i]);
return 0;
}
这个程序执行的时候输入的是11个数,求解答

#include<stdio.h>

void SelectSort(int a[],int n)

{

int i,j,temp,min;

for(i=0;i<n-1;i++)

{

min=i;

for(j=i+1;j<n;j++)//找到最小元素的位置

while(a[j]<a[min])

min=j;

if(min!=i)

{

temp=a[min];//元素的交换

a[min]=a[i];

a[i]=temp;

}

}

}

void main()

{

int a[10],i;

printf("please input 10 numbers:\n");

for(i=0;i<10;i++)

scanf("%d",&a[i]);

printf("The array is:\n");

for(i=0;i<10;i++)

printf("%-4d",a[i]);

SelectSort(a,10);

printf("\nAfter sort the array is:\n");

for(i=0;i<10;i++)

printf("%-4d",a[i]);

printf("\n");

}

运行效果:

扩展资料:

scanf函数用法:

scanf("输入控制符",输入参数);

功能:将从键盘输入的字符转化为“输入控制符”所规定格式的数据,然后存入以输入参数的值为地址的变量中。

用scanf()函数以%s格式读入的数据不能含有空白符时,所有空白符都被当做数据结束的标志。所以题中函数输出的值只有空格前面的部分。

如果想要输出包括空格在内的所有数据,可以使用gets()函数读入数据。gets()函数的功能是读取字符串,并存放在指定的字符数组中,遇到换行符或文件结束标志时结束读入。换行符不作为读取串的内容,读取的换行符被转换为字符串结束标志'\0'。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-05-16
问题是在scanf("%d\n",&a[i]);,其实还是识别10个输入数。

问题关键是输入格式,\n,输入完10个数必须要有一个\n,但是单独的\n是不能识别的,所以需要至少加一个字符,引出\n,也就是你误以为的第十一个数。
修改意见:scanf("%d\n",&a[i]);改为scanf("%d",&a[i]);本回答被提问者采纳
第2个回答  2018-02-24
//该带的括号都带好 注意编码规范
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) 
{
int a[10],t,i,j;

for(i=0;i<=9;i++)
{
scanf("%d",&a[i]); // \n去掉
}

for(i=0;i<=9;i++)
{
for(j=i+1;j<=9;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;

}
}
for(i=0;i<=9;i++)
{
printf("%d ",a[i]);
}
return 0;
}
23 45 67 43 87 88 86
43 87 69
23 43 43 45 67 69 86 87 87 88 Press any key to continue

第3个回答  2018-02-25
scanf("%d\n",&a[i]);你这里输入格式不要加‘\n’,直接写scanf("%d",&a[i]);
第4个回答  2018-02-25
#include <stdio.h>
#include <stdlib.h>
#define N 10
/* run this program using the console pauser or add your 
own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
  int a[N],t,i,j;
  for(i=0;i<N;i++)
    scanf("%d",&a[i]);
    
  for(i=0;i<N;i++)
    for(j=i+1;j<N;j++)
      if(a[i]>a[j])
      {
          t=a[i];a[i]=a[j];a[j]=t;
      }
      
  for(i=0;i<N;i++)
    printf("%d",a[i]);
    
return 0;
}