苦惱啊!
Description
設(shè)有n個正整數(shù),將他們連接成一排,組成一個最大的多位整數(shù)。例如:n=3時,3個整數(shù)13,312,343,連成的最大整數(shù)為:34331213
又如:n=4時,4個整數(shù)7,13,4,246連接成的最大整數(shù)為7424613
Input
N
N個數(shù)
Output
連接成的多位數(shù)
Sample Input
3
13 312 343
0
Sample Output
34331213
這個題目意思應(yīng)該很好理解,至少會想有兩種解題思路,
一、把數(shù)組從大到小排列,這樣是最大嗎? 顯然不是,例如:123 9,應(yīng)該輸出9123;
二、把數(shù)組按字符串排序,這樣是最大嗎?這問題可能會然我們困惑,但是這也是錯的,例如:120,12;應(yīng)該輸出12120;
這個題目不知道毒害了多少人(當(dāng)然我指的是ACM新人),尤其是第二種思路去寫代碼的。。這只是一個悲劇的開始,你把一條彎路在直走!
其實這個題目應(yīng)該是屬于動態(tài)規(guī)劃的最簡單應(yīng)用!子問題,給你兩個數(shù),讓你連成最大數(shù),一定非常簡單,只能組成兩個數(shù),比較一下大小就成!這就是解題思路!
如果給你三個數(shù)呢?一直遞推下去!悲劇啊,盡然怎么簡單!
代碼如下:


#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()


{
int n,i,j,l,k;
char s[1000][100];
char s1[200],s2[200];
while(scanf("%d",&n)!=EOF&&n!=0)

{
for(i=0;i<n;i++)

{
scanf("%s",s[i]);
while(s[i][0]=='0')

{
if(strlen(s[i])==1)
break;
strcpy(s[i],s[i]+1);
}
}
for(i=0;i<n;i++)

{
for(j=i+1;j<n;j++)

{
strcpy(s1,s[i]);
strcat(s1,s[j]);
strcpy(s2,s[j]);
strcat(s2,s[i]);
if(strcmp(s1,s2)<0)

{
strcpy(s1,s[i]);
strcpy(s[i],s[j]);
strcpy(s[j],s1);
}
}
}
for(i=0;i<n;i++)

{
if(s[0][0]=='0')

{
printf("0");
break;
}
else

{
printf("%s",s[i]);
}
}
printf("\n");
}
return 0;
}



本來我也是第二種思路的,我看了感覺不像,因為以前寫過了!
早上的幾點收獲:
1、<string>頭文件和<string.h>頭文件是不一樣的!運行時的一個錯誤,弄了一個早上,最后發(fā)現(xiàn)這什么個問題!
2、運用容器,排序字符串
vector<string> words;
string str;
while(scanf("%s",str)!=EOF)
words.push_back(str);

sort(words.begin(),words.end(),greater<string>());


for(vector<string>::size_type i=0;i<words.size();i++)

{
cout<<words[i]<<endl;
}
不過會有很多警告,讓我很苦惱!有誰知道呢?
警告如下:
--------------------Configuration: rongqi - Win32 Debug--------------------
Compiling
rongqi.cpp
c:\documents and settings\administrator\桌面\rongqi\rongqi.cpp(81) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char
> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
c:\documents and settings\administrator\桌面\rongqi\rongqi.cpp(81) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,st
d::basic_string<char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information

rongqi.obj - 0 error(s), 0 warning(s)

3、在程序頭上寫#pragma warning (disable: 4786),可以注銷4786以后警告!
posted on 2010-09-06 11:52
jince 閱讀(889)
評論(0) 編輯 收藏 引用 所屬分類:
Questions