Uva10905:children ' s Game (sort)

Source: Internet
Author: User
Tags strcmp

Title: Http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/A

Topic Requirements:, give n numbers, reorder them to get a maximum number, as if given 123 456 789 to spell 789456123 Max

This problem can be regarded as a sort of problem, but the rules of the sort are fastidious

If you want to sort by dictionary order, obviously wrong, like 999123 999, sorted by dictionary order 999123 in front, get the number 999123999, obviously not enough 999999123 big

Problem analysis: Bubble sort, first sorted by dictionary order, so that the number of exchange of bubble sort can be greatly reduced.

If there are two numbers 90 9, if sorted by dictionary order then 90>9, at this time compare strcat ("90", "9") with ("9", "90") size, make large with small position exchange. Please look at the code for specific actions, you will understand what the meaning of.

#include <iostream>#include<string.h>#include<stdio.h>#include<algorithm>#include<math.h>#include<queue>#defineEPS 1e-9typedefLong Longll;using namespacestd;intN;Charstr[101];Chara[ -][101];Chars2[202],s3[202];intcmpConst void*a,Const void*b) {     returnstrcmp ((Char*) B, (Char*) a);}intMain () { while(SCANF ("%d", &n)!=eof&&n!=0)    {         for(intI=0; i<n;i++) scanf ("%s", A[i]); Qsort (A,n,sizeof(a[0]), CMP);  for(intI=1; i<n;i++)        {             for(intj=0; j<n-i;j++) {strcpy (s2,a[j+1]);                strcat (S2,a[j]);                strcpy (S3,a[j]); strcat (S3,a[j+1]); if(strcmp (S2,S3) >0) {strcpy (str,a[j]); strcpy (A[j],a[j+1]); strcpy (A[j+1],str); //printf ("%s%s\n", a[j],a[j+1]);                }            }        }         for(intI=0; i<n;i++) {printf ("%s", A[i]); } cout<<Endl; }    return 0;}

Uva10905:children ' s Game (sort)

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.