(算法练习)——C语言-数字交换

要求:
http://codeup.cn/problem.php?cid=100000600&pid=0
说明:
这么简单的题都做不了满分真是让人惆怅。。。(代码提示答案错误= =)

#include 
#include 
using namespace std;

int numrecord[20];
int nummax[20];
void shuru(int numrecord[],int nummax[]){
	for(int i = 0;i <10;i++){
		scanf("%d",&numrecord[i]);
		nummax[i] = numrecord[i];	
	}
}

void chuli(int numrecord[],int nummax[]){
	sort(nummax,nummax + 10);
	int Maxnum = nummax[9];//max
	int Minnum = nummax[0];//min
	int signal0 = numrecord[0];
	int signal9 = numrecord[9];
	for(int i = 0;i<10;i++){
		if(numrecord[i] == Maxnum){
			numrecord[i] = signal9;
		}
		else if(numrecord[i] == Minnum){
			numrecord[i] = signal0;
		}
		else{
			;
		}
	}
	numrecord[0] = Minnum;
	numrecord[9] = Maxnum;
}

void shuchu(int numrecord[]){
	for(int i = 0;i <10;i++){
		printf("%d ",numrecord[i]);
	}
}

int main(){
	shuru(numrecord,nummax);
	chuli(numrecord,nummax);
	shuchu(numrecord);
}

改了改,依然答案错误。。。随他去吧= =(可能是没考虑最大最小不止一个的情况??

#include 
#include 
#include 
using namespace std;

int numrecord[10];
int nummax[10];
void shuru(int numrecord[],int nummax[]){
	for(int i = 0;i <10;i++){
		nummax[i] = numrecord[i];	
	}
}

void chuli(int numrecord[],int nummax[]){
	sort(nummax,nummax + 10);
	int Maxnum = nummax[9];//max
	int Minnum = nummax[0];//min
	int signal0 = numrecord[0];
	int signal9 = numrecord[9];
	for(int i = 0;i<10;i++){
		if(numrecord[i] == Maxnum){
			numrecord[i] = signal9;
		}
		else if(numrecord[i] == Minnum){
			numrecord[i] = signal0;
		}
		else{
			;
		}
	}
	numrecord[0] = Minnum;
	numrecord[9] = Maxnum;
}

void shuchu(int numrecord[]){
	for(int i = 0;i <10;i++){
		printf("%d ",numrecord[i]);
	}
}

int main(){
	int i = 0;
	while(scanf("%d",&numrecord[i]) != EOF){
		if(getchar() =='\n'){
			shuru(numrecord,nummax);
			chuli(numrecord,nummax);
			shuchu(numrecord);
			printf("\n");
			i = 0;
			memset(numrecord,0,sizeof(numrecord));
			memset(nummax,0,sizeof(nummax));
		}
		else{
			i++;
		}
		
	}
}

你可能感兴趣的:(C++标准模板库STL)