| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
用STL 0ms#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <functional>
using namespace std;
#define INT_LEN 32
struct greate:public binary_function<char,char,bool>{
bool operator() (const char& c1,const char& c2) const { return c2<c1; }
};
int main()
{
int I, J, len,
_i, pow2;
char a[INT_LEN], b[INT_LEN];
while (scanf("%d", &I) && I != 0)
{
//算法:先计算I的二进制字符串,然后利用althorithm中next_perputation函数产生下一个排序字符串,后转化为整数输出
//首先排除当前排序最大的情况
J = 0;
_itoa(I, a, 2);
strcpy(b, a);
len = strlen(a);
sort(b, b+len, greate());
if (strcmp(b,a) == 0)
{
//增加一位,然后将最高位置1:
a[len] = '0';
a[len + 1] = '\0';
if (a[0] != '1')
{
a[0] = '1';
for (_i=1; _i<len; _i+=1)
if (a[_i] == '1')
{
a[_i] = '0';
break;
}
}
sort(a+1, a + len+1);
}
else
next_permutation(a, a+len);
_i=strlen(a)-1; pow2 = 1;
while (_i >= 0)
{
J += pow2 * (a[_i] - '0');
_i -= 1;
pow2 *= 2;
}
printf("%d\n", J);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator