zoukankan      html  css  js  c++  java
  • ASCII码排序

    Problem Description

      输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。

    Input

      输入数据有多组,每组占一行,有三个字符组成,之间无空格。

    Output

      对于每组输入数据,输出一行,字符中间用一个空格分开。

    Sample Input

      qwe asd zxc

    Sample Output

      e q w a d s c x z

    要让三个数从小到大排,顺序就是:

    比较1,2两个数。如果第一个数比第二数大,把这两个数交换,来保证前面两个数按升序排列。

    比较2,3两个数。如果第二个数比第三数大,把这两个数交换,来保证后面两个数按升序排列。

    经过上面两步,最大的数已经被移到最后。再重复一次第一步。保证三个数都是按升序来排列。

    虽然你也可以用排序算法对它进行排序,但那就把问题复杂化了。做ACM的题目,就是要充分利用题目给出的条件(本题一明确指出输入的只有三个字符)。选择最优算法,而不是最通用的算法。

     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     char n[4];
     8 
     9     while (cin >> n)
    10     {
    11         if (n[0] > n[1]) swap(n[0], n[1]);
    12         if (n[1] > n[2]) swap(n[1], n[2]);
    13         if (n[0] > n[1]) swap(n[0], n[1]);
    14         cout << n[0] <<“ ”<< n[1] <<“ ”<< n[2] << endl;
    15     }
    16 
    17     return 0;
    18 }
  • 相关阅读:
    如何在iTerm2中配置oh my zsh?
    sublime中格式化jsx文件
    ES6 new syntax of Literal
    ES6 new syntax of Rest and Spread Operators
    How to preview html file in our browser at sublime text?
    ES6 new syntax of Default Function Parameters
    ES6 new syntax of Arrow Function
    七牛云2018春招笔试题
    Spring-使用注解开发(十二)
    Spring-声明式事物(十一)
  • 原文地址:https://www.cnblogs.com/NYNU-ACM/p/4238568.html
Copyright © 2011-2022 走看看