本题求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. cobbler之详细配置

    目录 作用 组件 命令 安装过程     安装包     配置文件     启动cobbler服务     配置cobbler服务     使用cobbler_web 作用:系统自动化安装,支持lin ...

  2. 关于tuple的只读特性

    a = (1,3,[5,4,1]) a[2][1] = 2 print(a) 结果是:(1,3,[5,2,1]) 可以看到,在这里tuple的内容被修改了. 原因就是tuple的“只读”属性是指tup ...

  3. MVC4 + EF + System.Threading.Thread 出现的问题记录

    项目要求是页面监测到后台数据库用户数据(Users)变化,前台做出相应的响应和操作. 一.参考很多资料,大概有几种方式: 参考资料地址:http://www.cnblogs.com/hoojo/p/l ...

  4. [置顶] SPL讲解(6)--Condition篇

    SmartPersistenceLayer 2.0 之 Condition篇 原理        强大的Condition功能是SPL的一个特性,可以使用Condition完成绝大部分的条件定义,使用 ...

  5. C语言的本质(36)——makefile基础

    除了Hello World这种极简单的程序之外,一般的程序都是由多个源文件编译链接而成的,这些源文件的处理步骤通常用Makefile来管理.makefile带来的好处就是--"自动化编译&q ...

  6. House Robber 解答

    Question You are a professional robber planning to rob houses along a street. Each house has a certa ...

  7. LeeCode-Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  8. 【错误】:MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'

    错误:MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决 ...

  9. Scala--样例类(case)详解

    概述: case类在模式匹配和actor中经常使用到,当一个类被定义成为case类后,Scala会自动帮你创建一个伴生对象并帮你实现了一系列方法且带来了不少好处,如下: 1.实现了apply方法,意味 ...

  10. Oracle SQL函数之日期函数

    sysdate [功能]:返回当前日期. [参数]:没有参数,没有括号 [返回]:日期 SQL> SELECT SYSDATE FROM DUAL; SYSDATE ----------- // ...