C语言:从键盘上输入一批数,分别输出器最大值和最小值(数据结束标识为-1...
发布网友
发布时间:2023-12-28 00:10
我来回答
共3个回答
热心网友
时间:2024-08-01 18:07
#include<stdio.h>
int *using_new_int(int num)
{
int *arr = new int[num]; //动态申请空间
return arr;
}
void sort( int *arr , int length)
{
int max,min,i;
if( length <= 0 )
{
printf("The array is NULL!\n");
return ;
}
max = arr[0];
min = arr[0];
for( i = 1 ; i < length ; i++ )
{
if( arr[i] > max )
{
max = arr[i];
}
if( arr[i] < min )
{
min = arr[i];
}
}
printf("The max number is %d , the min number is %d.\n" , max ,min );
}
int main()
{
int amount , i = 0 ,temp ;
printf("Please input the amount of the number you want to input:");
scanf("%d" , &amount );//输入数字个数
if(amount <= 0 )
printf("The amount could not be little than 0!\n");
printf("Please input the numbers:\n");
int *array = NULL;
array = using_new_int(amount);
while( i < amount )
{
scanf("%d" , &temp );//每输入一个数字就enter(回车)一下
if(temp == -1)
break;
else
array[i] = temp;
i++;
}
/*For debug*/
for( i = 0 ; i < amount ; i++)
printf("%d " , array[i]);
printf("\n");
/*debug end*/
sort(array , amount);//排序
delete array;
return 0;
}
可自定义需要输入的数字个数,并且包含排错。
热心网友
时间:2024-08-01 18:07
#include "StdAfx.h"
#include<iostream>
using namespace std;
int main()
{
int list[100];
cout<<"请输入待排序的整数数列:"<<endl;
for(int i=0; i<100; i++)
{
cin>>list[i];
}
int j=0;
while (j<100)
{
if(list[j]>list[j+1])
{
int tmp=list[j];
list[j]=list[j+1];
list[j+1]=tmp;
j=0;
}
else j++;
}
cout<<"排序后的整数序列是:";
for (i = 0; i < 100; i++)
{
cout << list[i]<<endl;
}
system("pause");
return 0;
}
热心网友
时间:2024-08-01 18:09
#include<stdio.h>
int main()
{
//假设输入数的范围是0-10000
int min = 10000;
int max = 0;
int num; //输入的数
scanf("%d",&num);
while(num != -1)
{
if(num < min)
min = num;
if(num > max)
max = num;
scanf("%d",&num);
}
printf("最大值是: %d\n",max);
printf("最小值是: %d\n",min);
return 0;
}