糟烂的代码啊...  这个题目思路很简单——末位只可能为0和5,所有数字的和肯定被3整除

没有0和5的肯定不行

否则,把所有数字求和

如果被3整除,则从大到小输出

如果除3余1,则按以下顺序——删1;删4;删7;删2、5、8中的2个(特别注意如果没有0要保留一个5)

如果除3余2,则按以下顺序——删2;删5(特别注意如果没有0要保留);删8;删1、4、7中的2个

下面是糟烂的代码——

//糟烂代码总结 —— 没有章法,思路不清,逻辑性太高,导致找不出错

//以后写代码引以为戒

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
char s[1010];
int num[12];
int check(int sum)
{
if(!num[0]&&!num[5])return 0;
if(sum==0)return 1;
if(num[0])return 1;
if(sum%3==1)
{
if(num[1]==0&&num[4]==0&&num[7]==0&&(num[2]+num[5]+num[8]<2))
{
return 0;
}
}
else if(sum%3==2)
{
if(num[5])
{
if(num[2]==0&&num[5]<=1&&num[8]==0&&(num[1]+num[4]+num[7]<2))
{
return 0;
}
}
else
{
if(num[2]==0&&num[5]==0&&num[8]==0&&(num[1]+num[4]+num[7]<2))
{
return 0;
}
}
}
return 1;
}
int solve(int sum)
{
int ct=0;
if(sum==0)
{
printf("0\n");return 0;
}
if(sum%3==1)
{
ct=1;
if(num[1]){num[1]--;sum-=1;ct--;}
else if(num[4]){num[4]--;sum-=4;ct--;}
else if(num[7]){num[7]--;sum-=7;ct--;}
else
{
ct=2;
if(num[0]==0)
{
for(int i=2; i<=8; i+=3)
{
if(ct==0)break;
if(i==5&&num[i]==1)continue;//此处写错导致错误(原来是写的num[5]==1,影响到了8)
while(num[i]>0)
{
num[i]--;
ct--;
sum-=i;
if(ct==0)break;
if(i==5&&num[i]==1)break;
}
}
}
else
{
for(int i=2; i<=8; i+=3)
{
if(ct==0)break;
while(num[i]>0)
{
num[i]--;
ct--;
sum-=i;
if(ct==0)break;
}
}
} }
if(ct>0)
{
printf("impossible\n");
return 0;
}
}
else if(sum%3==2)
{
ct=1;
if(num[2]){num[2]--;sum-=2;ct--;}
else if(num[5]==1&&num[0]>0){num[5]--;sum-=5;ct--;}
else if(num[5]>1){num[5]--;sum-=5;ct--;}
else if(num[8]){num[8]--;sum-=8;ct--;}
else
{
ct=2;
for(int i=1; i<=7; i+=3)
{
if(ct==0)break;
while(num[i]>0)
{
num[i]--;
ct--;
sum-=i;
if(ct==0)break;
}
}
}
if(ct>0)
{
printf("impossible\n");
return 0;
}
}
if(num[0])
{
if(sum==0)printf("0");
else
for(int i=9; i>=0; i--)
{
for(int j=0; j<num[i]; j++)
printf("%d",i);
}
printf("\n");
}
else
{
num[5]--;
for(int i=9; i>=0; i--)
{
for(int j=0; j<num[i]; j++)
printf("%d",i);
}
printf("5\n");
}
}
int main()
{
int T;
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&T);
while(T--)
{ scanf("%s",s);
int l=strlen(s);
memset(num,0,sizeof(num));
int sum=0;
for(int i=0; i<l; i++)
{
num[s[i]-'0']++;
sum+=s[i]-'0';
}
if(!check(sum))
{
printf("impossible\n");
}
else
{
solve(sum);
}
}
return 0;
}

SPOJ 416 - Divisibility by 15(贪心)的更多相关文章

  1. SPOJ 416 Divisibility by 15 细节题

    一个结论:一个数,如果它的所有数字之和能被3整除,那么这个数也能被3整除. 最后一位肯定是0或者5,如果没有就impossible. 剩下的就是,如何删除尽量少的数,使所有数字之和为3的倍数. 情况比 ...

  2. spoj 416

    又臭又长的烂代码 ...... #include <iostream> #include <cstdio> #include <cstring> #include ...

  3. 【贪心】【Uva11729】 Commando War

    你有n个部下,每个部下需要完成一项任务.第i个部下需要你花Bi分钟交待任务,然后他会立刻独立地.无间断地执行Ji分钟后完成任务.你需要选择交待任务的顺序,使得所有任务尽早执行完毕(即最后一个执行完的任 ...

  4. Team Foundation Server 2013 with Update 3 Install LOG

    [Info   @10:14:58.155] ====================================================================[Info   @ ...

  5. 「PKUSC2018」主斗地(暴搜)

    这道斗地主比 \(PKUWC\) 那道可做多了... 我们用 \(NOIP\) 那道斗地主的思路:暴搜出三代和四代,贪心出散牌. 还有jry为什么要出xx网友而不出他的另一个老婆 我们发现两个人的每回 ...

  6. (2) 我的结果- spec2006中精确的simulation points运行点

    spec06中获取simpoints的环境说明: spec的版本号为spec2006v1.0; 使用ref input with runspec; 100millions为周期生成的simpoints ...

  7. Bayesian Optimization使用Hyperopt进行参数调优

    超参数优化 Bayesian Optimization使用Hyperopt进行参数调优 1. 前言 本文将介绍一种快速有效的方法用于实现机器学习模型的调参.有两种常用的调参方法:网格搜索和随机搜索.每 ...

  8. SPOJ Find the max XOR value(二进制,贪心即可)

    You have two integers L and R, and you are required to find the max xor value of a and b where L < ...

  9. 【2019.8.15 慈溪模拟赛 T1】插头(plugin)(二分+贪心)

    二分 首先,可以发现,最后的答案显然满足可二分性,因此我们可以二分答案. 然后,我们只要贪心,就可以验证了. 贪心 不难发现,肯定会优先选择能提供更多插座的排插,且在确定充电器个数的情况下,肯定选择能 ...

随机推荐

  1. Html.Action、html.ActionLink与Url.Action的区别

    1.html.ActionLink返回的指向指定controller.指定action的超链接标签<a>标签.如果没有指定controller,则默认为本页面对应的Controller. ...

  2. linux下查看所有用户及所有用户组

    groups 查看当前登录用户的组内成员groups gliethttp 查看gliethttp用户所在的组,以及组内成员whoami 查看当前登录用户名 /etc/group文件包含所有组/etc/ ...

  3. mysql的perror

    eg: 执行:perror 1064 返回:MySQL error code 1064 (ER_PARSE_ERROR): %s near '%-.80s' at line %d           ...

  4. JavaScript 类、构造函数、原型

    类.构造函数.原型  :本质均为函数 利用的原理是:词法作用域,调用对象及作用域链  闭包  属性查找方式    设计和new运算符一起使用的函数叫做构造函数. 构造函数的工作:初始化一个新创建的对象 ...

  5. oracle PL/SQL程序设计

    declare 说明部分    (变量说明,光标申明,例外说明 ] begin 语句序列   (DML语句]… exception 例外处理语句 End; /

  6. 使用Scanner来解析文件

    前面的流是全部流进来再处理,空间换取时间 我们用Scanner来解析文件,先处理再输入数据,时间换取空间 两种方法 Scanner scanner1=new Scanner(file1); for(; ...

  7. uva 482 - Permutation Arrays

    <int, double> --> <int, string> 从而避免了输出格式: #include <vector> #include <strin ...

  8. MetInfo操作笔记

    1.去版权(前台) 文件路径:templates/模板名称/foot.php <div class="powered_by_metinfo">Powered by &l ...

  9. curl 转载

    CURLOPT_RETURNTRANSFER 选项: curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 如果成功只将结果返回,不自动输出任何内容. 如果失败返回F ...

  10. validate()的配置项

    1.submitHandler //通过验证成功后运行的函数 代码: $("#mainForm").validate({ ...... rules:{ username:{//此处 ...