1、 str, int n)bool del;for (int i = n; i 0; i-)del = false;/ 每次删除第一个比下一个数字大的数for (string:iterator it = str.begin(); it != str.end()-1; it+)if (*it *(it+1)str.erase(it);del = true;break;/如果所有数字递增,则删除最后几个数字直接返回if (!del)str.erase(str.end()-i, str.end();return str;int main()int n;string str;cin str; n;cou
2、t greedy(str, n) endl;return 0;代码2:#includestdlib.hstring.hint num = 0;char str100;int a100;scanf(“%d”,&num); /数字n); /个数itoa(num, str, 10); /转换为字符串int flag=0; /是否是递增序列int i;int length=strlen(str); /长度int x=length-n;while(length!=x)flag=0; /递增for(i=0;i(stri+1-0) /比后一个数大 删掉 for(int temp=i;temp0) /*循环减
3、s次*/i=0;/*每次删除后重头开始*/while(istrlen(N) & Ni=Ni+1)i+;/*算法核心*/for(j=i;jinj?inj:sum;case 2:sum = in1*10+in0;for (k=j+1;kk+)(ink*10+inj)?(ink*10+inj):case 3:sum = in2*100+in1*10+in0;for (l=k+1;ll+)(inl*100+ink*10+inj)?(inl*100+ink*10+inj):case 4:sum = in3*1000+in2*100+in1*10+in0;for (m=l+1;mm+)(inm*1000+
4、inl*100+ink*10+inj)?(inm*1000+inl*100+ink*10+inj):case 5:case 6:default:输入错误或超限!nreturn -1;return sum;代码5:#include stdafx.hconio.hstring.h#define strMax 1000void GreedDelNumber( char str , int len , int s )/for( int i = 0 ; i len ; i+ )/ printf( %c , stri );/printf( );if( s = 0 )char num strMax = 0
5、;int j = 0;for( int i = 0 ;if( stri != 0 )numj+ = stri;strcpy( str , num );int j = i + 1;while( strj = 0 & j = len | stri strj )stri = 0;GreedDelNumber( str , len , s - 1 );int _tmain(int argc, _TCHAR* argv)char str = 41235;int s = 2;printf( 原数为: %sn , str );长度为: %dn待删数: %dn , strlen( str ) , s );GreedDelNumber( str , strlen( str ) , s );删除后:getch();代码6:const int Limit_Size =
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1