天天看點

5-題目1054:字元串内排序

http://ac.jobdu.com/problem.php?pid=1054

題目描述:

輸入一個字元串,長度小于等于200,然後将輸出按字元順序升序排序後的字元串。

有兩個版本,一個是自己寫的判斷char數組長度,還有一個是<string.h>頭檔案裡面含有的length = strlen(arr);函數,但是VS和OJ的判斷系統不大一樣,在VS裡可以直接寫<string>,但是在OJ裡不識别,必須寫<string.h>

代碼1:

#include<stdio.h>  
#include<iostream>  
#include<algorithm>
#include<string>  
using namespace std;
 
int main()
{
    int i ,length = 0;
    char arr[201];
 
    while (cin >> arr)
    {
        for (i = 0; arr[i] != '\0'; i++)
            length++;   //統計字元串的長度
        sort(arr,arr+length);
        for (i = 0; i < length; i++)
            cout << arr[i];
        cout << endl;
        arr[0] = '\0'; //清空char數組
        length = 0;
    }
//  system("pause");
    return 0;
}
/**************************************************************
    Problem: 1054
    User: yy4869
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1520 kb
****************************************************************/
           

代碼2:

#include<stdio.h>  
#include<iostream>  
#include<algorithm>
#include<string.h>  
using namespace std;
 
int main()
{
    int length = 0;
    char arr[201];
 
    while (cin >> arr)
    {
        length = strlen(arr);
        sort(arr,arr+length);
        cout << arr << endl;
    }
    return 0;
}
/**************************************************************
    Problem: 1054
    User: yy4869
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1520 kb
****************************************************************/