| ||||||||||
| 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 | |||||||||
WA很多次..后人可以参考参考#include<iostream>
#include<string>
using namespace std;
#define maxn 2008
int sum,len,tot;
char str[maxn];
void Print(int p,int q) {
char ans[maxn];
int l=0;
for (int i=0; i<len; i++) if (i!=p && i!=q) ans[l++]=str[i];
while (str[l-1]=='0' && l>1) l--;
if (ans[0]!='0') {
for (int i=0; i<l; i++) if (ans[i]=='5') {
swap(ans[0],ans[i]);
break;
}
if (l>1) sort(ans+1,ans+l);
}
for (int i=l-1; i>=0; i--) printf("%c",ans[i]);
printf("\n");
}
int main() {
freopen("i","r",stdin);
freopen("o","w",stdout);
scanf("%s",str);
len=strlen(str);
sort(str,str+len);
for (int i=0; i<len; i++) sum+=str[i]-'0';
if (str[0]!='0') {
tot=0;
for (int i=0; i<len; i++) if (str[i]=='5') tot++;
if (!tot) {
puts("impossible");
return 0;
}
}
if (sum%3==0) {
Print(len,len);
return 0;
}
if (sum%3==1) {
for (int i=0; i<len; i++) if ((str[i]-'0')%3==1) {
Print(i,len);
return 0;
}
for (int i=1; i<len; i++) if ((str[i]-'0')%3==2)
for (int j=0; j<i; j++) if ((str[j]-'0')%3==2) {
int tmp=0;
if (str[i]=='5') tmp++;
if (str[j]=='5') tmp++;
if (str[0]=='0' || tmp<tot) {
Print(i,j);
return 0;
}
}
puts("impossible");
return 0;
}
if (sum%3==2) {
for (int i=0; i<len; i++) if ((str[i]-'0')%3==2)
if (str[0]=='0' || str[i]!='5' || tot>1) {
Print(i,len);
return 0;
}
for (int i=1; i<len; i++) if ((str[i]-'0')%3==1)
for (int j=0; j<i; j++) if ((str[j]-'0')%3==1) {
Print(i,j);
return 0;
}
puts("impossible");
return 0;
}
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator