本题求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. 最短路(Dijkstra模板题)

    就不写题目链接了 Sample Input 5 5 点个数a,边个数b 1 2 20 点,点,权值 2 3 30 3 4 20 4 5 20 1 5 100 求出1到a的最短距离 Sample Out ...

  2. 黑马程序员 1、C语言32个关键字整理分类

    ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------ C语言一共有32个关键字 一.数据类型关键字(共20个) A.基本数据类型(5个)void :声明 ...

  3. connectionStrings基本配置

    常用connectionStrings配置: <connectionStrings>   <add       name="LocalSqlServer"     ...

  4. asm.uew

    /L16"ASM" Nocase Line Comment = ; File Extensions = ASM INC DEF /Colors = ,,,,, /Colors Ba ...

  5. DOS命令行 定时关机&取消定时关机

      命令行关机命令----shutdown   Windows XP的关机是由Shutdown.exe程序来控制的,位于Windows\System32文件夹中.   如果你输入"shutd ...

  6. Linux学习,在线版

    考虑加入Linux教派,最初被Linux吸引是看了<Unix编程艺术>,虽然里面的个别非常教条,极端.但是里面大部分的设计思想我还是认同的. 下面是我整理的一些Linux资料,其实我打算折 ...

  7. 关于HTML5 语音搜索的问题

    以前语音输入功能虽然没用过,但是看上去很高级的样子,有了智能手机之后,在ios和android中的语音输入,特别是iPhone的Siri,让一切变得如此简单,嘎嘎. 当然我不是在这里要实现如此强悍的功 ...

  8. 剑指offer-面试题15.链表中倒数第k个结点

    题目:输入一个链表,输出该链表的倒数第K个结点.为了符合大多数人的习惯,本题 从1开始计数,即链表的尾结点是倒数第1个节点.例如有一个链表有6个节点,从 头节点开始他们的值依次是1,2,3,4,5,6 ...

  9. iphone 6s pp助手 越狱

    https://www.apple.com/iphone-6/合适初高中学习英语http://www.travelchinaguide.comhttp://jailbreak.25pp.com/ ip ...

  10. 机器学习 1、R语言

    R语言 R是用于统计分析.绘图的语言和操作环境.R是属于GNU系统的一个自由.免费.源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具. 特点介绍 •主要用于统计分析.绘图.数据挖掘 •R内置 ...