Topic 1135: String sorting java/c++

Source: Internet
Author: User

Title Description:

Enter the number of strings you want to enter first. Then enter the group string with a newline. Each string ends with a carriage return, and each string is less than 100 characters.
If a string entered during the input process is "stop", the input is also ended.
The string that is entered is then sorted by the length of each string, from small to large, and the string is output by the sorted result.

Input:

The number of strings, as well as the group string. Each string ends with ' \ n '. If the input string is "stop", the input is also ended.

Output:

There may be multiple sets of test data, for each set of data,
Sorts all the strings entered by the length from the small to the large sort output (if there is "stop", does not output "stop").

Sample input:
5sky is Greycoldvery coldstop3it are good enough to being proud ofgoodit is quite good
Sample output:
Coldvery Coldsky is Greygoodit is quite goodit are good enough to be proud of
Tips:

Dynamically allocates storage space (using the new () function) based on the number of strings entered. Each string is less than 100 characters.
There are several groups of test data, and note the use of the while () loop input.



C + + code:

#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std ;    BOOL CMP (string x,string y) {return x.length () <y.length ();} int main () {string str;    int i,j,k;    int n;        while (cin>>n&&n) {cin.ignore ();        vector<string>vecstr;            for (i=0;i<n;i++) {getline (CIN,STR);            if (str== "Stop") {break;        } vecstr.push_back (str);        } sort (Vecstr.begin (), Vecstr.end (), CMP); /* for (Vector<string>::iterator i=vecstr.begin (); I!=vecstr.end (); i++) {cout<<*i<        <endl;        } */For (i=0;i<vecstr.size (); i++) {cout<<vecstr[i]<<endl; }}//while return 1;} /************************************************************** problem:1135 User:carvin language:c++ Resul t:accepted time:10 Ms Memory:1528 kb****************************************************************/ 

Java code:

Import Java.util.scanner;import java.*;        public class main{public static void Main (String args[]) {string str[]=new string[100];        String tempstr = "";        String str1;        int i,j,k;        Scanner cin=new Scanner (system.in);            while (Cin.hasnext ()) {int num=cin.nextint ();            int count=0;                for (i=0;i<=num;i++) {str1=cin.nextline ();                    if (!str1.equals ("Stop")) {str[i]=str1;                count++;                } else {break;                    }}//for for (i=0;i<count;i++) {for (j=0;j<i;j++) {                    if (Str[j].length () <str[i].length ()) {tempstr=str[j];str[j]=str[i];str[i]=tempstr;            }}}//arrays.sort (str);            SYSTEM.OUT.PRI5NTLN (arrays.tostring (str)); for (i=count-2;i>=0;i--) {System.out.println (str[i]);    }//for}//while}//main}//main/************************************************************** problem:1135 User:carvin Language:java result:accepted time:90 Ms memory:15772 kb************************************* ***************************/



Topic 1135: String sorting java/c++

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.