[NYOJ] [acm-135] Sort Three Numbers in Ascending Order

Problem Description

Loading...

Problem Analysis

I’m wondering what the point of uploading this problem is…
Hmm…
Mainly because I feel that since we’re slacking off anyway
we might as well pick a fancy slacking-off pose…
Consider it making up for problems I hadn’t written up before~

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#include<stdio.h>
#include<algorithm>
using namespace std;
int main(){
	int a[3];
	while(scanf("%d %d %d",&a[0],&a[1],&a[2])!=EOF){
		sort(a,a+3);
		printf("%d %d %d\n",a[0],a[1],a[2]);
	}
}

Only the reference solution is still somewhat worth discussing
This one is really rough…
At least it trains program-reading ability…

The reference solution’s approach is to search through loops
and then perform sorting

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
int main() {
	int a,b,c,an[3],i,t,j,max,flag;
	scanf ("%d %d %d",&an[0],&an[1],&an[2]);
	for (i=0; i<3; i++) {
		t=max=an[i];
		flag=i;
		for (j=i; j<3; j++)
			if (an[j]>t) {
				max=an[j];
				flag=j;
			}; //加不加;应该都行 
		t=an[i];
		an[i]=max;
		an[flag]=t;
	}
	for (i=2; i>=0; i--)
		printf ("%d ",an[i]);
	return 0;
}

Problem link: #135. Sort Three Numbers in Ascending Order

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...