[Output] acm2016 exchange data

*** problems:
Problem the Description
input n (n <100) number, find the smallest number wherein the exchanging data with it the foremost outputs these numbers.

Input
input multiple sets of data, each representing one line in each line is an integer n, the number values in this test case, is followed by n integers. n = 0 indicates the end of input, not treated.

Output
For each case, the number of columns after the switching output, each output per line.

Sample Input
4 2 1 3 4
5 5 4 3 2 1
0

Sample Output
1 2 3 4
1 4 3 2 5***

Code:

#include<iostream>
using namespace std;

int main(){
	int a[100];
	int n,min;
	while(scanf("%d",&n)!=EOF){
		int sign=0;
		if(n==0){
			break;
		}
		cin>>a[0];
		min=a[0];
		for(int i=1;i<n;i++){
			cin>>a[i];
			if(a[i]<min){
				min=a[i];
				sign=i;//记录下标 
			}
		}
		if(sign!=0){
				int temp=a[0];
		a[0]=min;
		a[sign]=temp;
		}
	
		for(int i=0;i<n;i++){
			if(i!=n-1)
			cout<<a[i]<<" ";
			else cout<<a[i]<<endl;
		}
	}
	return 0;
}

Analysis: The first value of the input array to bring the first thing to note here, let him become the default minimum value, and then compared with the latter. Remember to find the minimum value of the index, and only when the latter has a smaller value when it needs to be exchanged, so that a determination here

Published 42 original articles · won praise 18 · views 405

Guess you like

Origin blog.csdn.net/weixin_42918559/article/details/104041705