Problem Link: UVA10905 children ' s Game. basic level exercises, written in C + + language program.
Test Instructions Description: Enter n positive integers to connect them to one of the largest integers.
Problem analysis: There are three main points, one is not to put large integers in front, such as 12 and 3, the largest integer is 312, and the second is not able to simply use the function strcmp () to compare, for example 9 and 90, the largest integer is 990 instead of 909, three is n integers, Some may be large integers, wrote a C language program, always AC, and finally understand.
The AC C + + language program is as follows:
/* <span style= "Font-family:simsun;" >uva</span>10905 Children ' s Game */#include <iostream> #include <cstdio> #include <algorithm >using namespace std; #define MAXN 50string Val[maxn];bool cmp (string A, string b) { return a+b > B+a;} int main () { int n; while (scanf ("%d", &n)!=eof && n! = 0) {for (int i=0; i<n; i++) cin >> Val[i]; Sort (val, Val + N, CMP); for (int i=0; i<n; i++) cout << val[i]; cout << Endl; } return 0;}
The C language program without AC is as follows:
/* <span style= "Font-family:simsun;" >uva</span>10905 Children ' s Game */#include <stdio.h> #include <string.h> #include <stdlib.h >typedef unsigned long long ULL; #define MAXN 50int N; ULL Val[maxn];char sa[20], sb[20], sab[40], sba[40];int cmp (const void * A, const void * b) { sprintf (SA, "%llu", * (ULL *) a); sprintf (SAB, "%llu", * (ULL *) a); sprintf (SB, "%llu", * (ULL *) b); sprintf (SBA, "%llu", * (ULL *) b); strcat (SAB, SB); strcat (SBA, SA); Return strcmp (SBA, SAB);} int main (void) { int i; while (scanf ("%d", &n)! = EOF && n! = 0) { for (i=0; i<n; i++) scanf ("%llu", &val[i]); Qsort (Val, N, sizeof (val[0]), CMP); for (i=0; i<n; i++) printf ("%llu", Val[i]); printf ("\ n"); } return 0;}
UVA10905 children ' s Game