本题求n个数组成的大数,要求是2,3,5的倍数。

因为是2 和5 的倍数,所以个位为 0;所以若n个数中没有0,直接输出-1;

难点就是要求为3 的倍数。

因为若某个数为3的倍数,则其各位数的和必然是3的倍数。

当n个数的和为3的倍数时从大到小输出便可;

当n个数的和不为3的倍数时,若n个数中有模3余数与和模3余数相同时,去掉其中最小的,否则去掉两个模3余数不为0且与和模3余数不同的数中最小的。若没有这些数,输出-1;

注意当结果为0时,不能输出前导0;

ps:开始写了一个很复杂的代码,后来看过大牛们60ms的算法后又敲了一遍。。。结果还有92ms。给大牛跪了 orz    ~~o(>_<)o ~~

附:(第二遍稍简洁代码)

 #include <iostream>
#include <cstring>
using namespace std; int main (){
int n;
int sign[];
int num[];
int sum;
while (cin>>n){
sum=;
sign[]=sign[]=sign[]=;
memset (num,,sizeof num);
for (int i=;i<n;i++){
int x;
cin>>x;
num[x]++;
sum+=x;
sign[x%]=min (sign[x%],x);
}
if (num[]==){ //n个数中没有0;
cout<<-<<endl;
continue ;
}
if (sum%){
if (sign[sum%]==){
int temp=;
for (int i=;i<;i++){//cout<<i<<" "<<num[i]<<endl;
if (i%!=&&num[i]){
num[i]--;
temp--;
n--;      //为末尾去前导零准备;
}
if (i%!=&&num[i]){
num[i]--;
temp--;
n--;
}
if (temp==)
break ;
}
if (temp){
cout<<-<<endl;
continue ;
}
}
else num[sign[sum%]]--,n--;
}
if (num[]==n){  //若结果为0,不能输出前导零;
cout<<<<endl;
continue ;
}
for (int i=;i>=;i--)
while (num[i]--)
cout<<i;
cout<<endl;
}
return ;
}

附:(第一遍复杂代码)

 #include <iostream>
#include <cstring>
#include <algorithm> #define maxn 100000+10 using namespace std; int sum,ans;
int n;
int sign[];
int a[maxn]; int main (){
while (cin>>n){
sum=;
sign[]=sign[]=sign[]=;
for (int i=;i<n;i++){
cin>>a[i];
sum+=a[i];
int temp;
temp=a[i]%;
if (temp){
sign[temp]=min (sign[temp],a[i]);
}
}
sort (a,a+n);
if (a[]!=){
cout<<"-1"<<endl;
continue ;
}
if (sum%==){
int flag=;
for (int i=n-;i>=;i--){
if (flag==&&a[i]==)
break ;
cout<<a[i];
flag=;
}
if (flag==)
cout<<"";
cout<<endl;
continue ;
}
else if (sum%==){
if (sign[]==){
int temp=;
for (int i=;i<n;i++){
if (a[i]%==){
a[i]=;
temp--;
}
if (temp==)
break ;
}
if (temp){
cout<<"-1"<<endl;
continue ;
}
int flag=;
for (int i=n-;i>=;i--){
if (a[i]==)
continue ;
if (flag==&&a[i]==)
break ;
cout<<a[i];
flag=;
}
if (flag==)
cout<<"";
cout<<endl;
}
else {
int temp=;
int flag=;
for (int i=n-;i>=;i--){
if (a[i]==sign[]&&temp){
temp=;
continue ;
}
if (flag==&&a[i]==)
break ;
cout<<a[i];
flag=;
}
if (flag==)
cout<<"";
cout<<endl;
}
}
else if (sum%==){
if (sign[]==){
int temp=;
for (int i=;i<n;i++){
if (a[i]%==){
a[i]=;
temp--;
}
if (temp==)
break ;
}
if (temp){
cout<<"-1"<<endl;
continue ;
}
int flag=;
for (int i=n-;i>=;i--){
if (a[i]==)
continue ;
if (flag==&&a[i]==)
break ;
cout<<a[i];
flag=;
}
if (flag==)
cout<<"";
cout<<endl;
}
else {
int temp=;
int flag=;
for (int i=n-;i>=;i--){
if (a[i]==sign[]&&temp){
temp=;
continue ;
}
if (flag==&&a[i]==)
break ;
flag=;
cout<<a[i];
}
if (flag==)
cout<<"";
cout<<endl;
}
}
}
return ;
}

CodeForces 214B Hometask的更多相关文章

  1. CF 214B Hometask(想法题)

    题目链接: 传送门 Hometask Time Limit: 2 seconds     Memory Limit: 256 megabytes Description Furik loves mat ...

  2. CodeForces 154A Hometask dp

    题目链接: http://codeforces.com/problemset/problem/154/A 题意: 给你一个字符串,和若干模板串(长度为2),至少删除多少个字母,使得字符串的字串里面没有 ...

  3. Codeforces Round #131 (Div. 2) B. Hometask dp

    题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...

  4. CodeForces - 404A(模拟题)

    Valera and X Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit ...

  5. Codeforces Round #237 (Div. 2) A

    链接:http://codeforces.com/contest/404/problem/A A. Valera and X time limit per test 1 second memory l ...

  6. Codeforces Round #260 (Div. 2)AB

    http://codeforces.com/contest/456/problem/A A. Laptops time limit per test 1 second memory limit per ...

  7. 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem H. Hometask 水题

    Problem H. Hometask 题目连接: http://codeforces.com/gym/100714 Description Kolya is still trying to pass ...

  8. xtu summer individual 2 C - Hometask

    Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...

  9. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

随机推荐

  1. regular expression 基本语法

    正则检测网址: http://regexpal.isbadguy.com/   (不包含界定符)   或  http://tool.chinaz.com/regex unicode转换工具 : htt ...

  2. python运维开发(二十五)---cmdb开发

    内容目录: 浅谈ITIL CMDB介绍 Django自定义用户认证 Restful 规范 资产管理功能开发 浅谈ITIL TIL即IT基础架构库(Information Technology Infr ...

  3. awk之7 常用函数的解析

    1.区域获取 substr(区域f,起始位置n1,获取范围n2) 解析:获取某个区域f内,从起始位置n1开始算起的n2个字符组成的字符串.如果n2不存在,则返回从n1开始到区域结束的内容. 例子:获取 ...

  4. Mysql 6.7.7 + EntityFramework 5.0 Code First 不能 Update-Database 问题的解决

    1.修改 Migrations/Configuration.cs 文件 namespace DataModel.Migrations { using System; using System.Data ...

  5. Mysql基本类型(字符串类型)——mysql之二

    转自: http://www.cnblogs.com/doit8791/archive/2012/05/28/2522556.html 1.varchar类型的变化 MySQL 数据库的varchar ...

  6. 将QT开发的界面程序封装成DLL,在VC中成功调用

    最近手头的一个项目需要做一个QT界面,并且封装成DLL,然后再动态调用DLL给出的接口函数,使封装在DLL内部的QT界面跑起来,在网上查了很多资料,今天终于成功了,经验不敢独享,因为CSDN给了我很多 ...

  7. 如何不保存Word即可用Word打开想显示的内容!

    今天遇到客户需要将数据用Word预览,原来做的时候是保存到本地然后打开,客户说这样不好,因为不一定预览了一定要保存,于是我翻箱倒柜的发现真没资料啊!!幸好经过我的大量实验(好吧,我是挨个方法试的!), ...

  8. Arcgis api For silverlight 加载QQ地图

    原文 http://www.cnblogs.com/thinkaspx/archive/2012/11/07/2759079.html //本篇博客仅在技术上探讨可行性   //如果要使用Q 地图,请 ...

  9. 解决问题之,wp项目中使用MatchCollection正则表达式匹配出错

    在最近,出现了这么一个问题 本人使用正则表达式代码,解析响应output,意图获得周边的CMCC热点 代码如下: //output="<?xml version=\"1.0\ ...

  10. Amazon Redshift and Massively Parellel Processing

    Today, Yelp held a tech talk in Columbia University about the data warehouse adopted by Yelp. Yelp u ...