本题求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 ~~

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

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4
 5 int main (){
 6 int n;
 7 int sign[5];
 8 int num[20];
 9 int sum;
10 while (cin>>n){
11 sum=0;
12 sign[0]=sign[1]=sign[2]=100;
13 memset (num,0,sizeof num);
14 for (int i=0;i<n;i++){
15 int x;
16 cin>>x;
17 num[x]++;
18 sum+=x;
19 sign[x%3]=min (sign[x%3],x);
20 }
21 if (num[0]==0){  //n个数中没有0;
22 cout<<-1<<endl;
23 continue ;
24 }
25 if (sum%3){
26 if (sign[sum%3]==100){
27 int temp=2;
28 for (int i=0;i<10;i++){//cout<<i<<" "<<num[i]<<endl;
29 if (i%3!=0&&num[i]){
30 num[i]--;
31 temp--;
32 n--;      //为末尾去前导零准备;
33 }
34 if (i%3!=0&&num[i]){
35 num[i]--;
36 temp--;
37 n--;
38 }
39 if (temp==0)
40 break ;
41 }
42 if (temp){
43 cout<<-1<<endl;
44 continue ;
45 }
46 }
47 else num[sign[sum%3]]--,n--;
48 }
49 if (num[0]==n){  //若结果为0,不能输出前导零;
50 cout<<0<<endl;
51 continue ;
52 }
53 for (int i=9;i>=0;i--)
54 while (num[i]--)
55 cout<<i;
56 cout<<endl;
57 }
58 return 0;
59 }

附:(第一遍复杂代码)

  1 #include <iostream>
  2 #include <cstring>
  3 #include <algorithm>
  4
  5 #define maxn 100000+10
  6
  7 using namespace std;
  8
  9 int sum,ans;
 10 int n;
 11 int sign[5];
 12 int a[maxn];
 13
 14 int main (){
 15 while (cin>>n){
 16 sum=0;
 17 sign[0]=sign[1]=sign[2]=100;
 18 for (int i=0;i<n;i++){
 19 cin>>a[i];
 20 sum+=a[i];
 21 int temp;
 22 temp=a[i]%3;
 23 if (temp){
 24 sign[temp]=min (sign[temp],a[i]);
 25 }
 26 }
 27 sort (a,a+n);
 28 if (a[0]!=0){
 29 cout<<"-1"<<endl;
 30 continue ;
 31 }
 32 if (sum%3==0){
 33 int flag=0;
 34 for (int i=n-1;i>=0;i--){
 35 if (flag==0&&a[i]==0)
 36 break ;
 37 cout<<a[i];
 38 flag=1;
 39 }
 40 if (flag==0)
 41 cout<<"0";
 42 cout<<endl;
 43 continue ;
 44 }
 45 else if (sum%3==1){
 46 if (sign[1]==100){
 47 int temp=2;
 48 for (int i=0;i<n;i++){
 49 if (a[i]%3==2){
 50 a[i]=100;
 51 temp--;
 52 }
 53 if (temp==0)
 54 break ;
 55 }
 56 if (temp){
 57 cout<<"-1"<<endl;
 58 continue ;
 59 }
 60 int flag=0;
 61 for (int i=n-1;i>=0;i--){
 62 if (a[i]==100)
 63 continue ;
 64 if (flag==0&&a[i]==0)
 65 break ;
 66 cout<<a[i];
 67 flag=1;
 68 }
 69 if (flag==0)
 70 cout<<"0";
 71 cout<<endl;
 72 }
 73 else {
 74 int temp=1;
 75 int flag=0;
 76 for (int i=n-1;i>=0;i--){
 77 if (a[i]==sign[1]&&temp){
 78 temp=0;
 79 continue ;
 80 }
 81 if (flag==0&&a[i]==0)
 82 break ;
 83 cout<<a[i];
 84 flag=1;
 85 }
 86 if (flag==0)
 87 cout<<"0";
 88 cout<<endl;
 89 }
 90 }
 91 else if (sum%3==2){
 92 if (sign[2]==100){
 93 int temp=2;
 94 for (int i=0;i<n;i++){
 95 if (a[i]%3==1){
 96 a[i]=100;
 97 temp--;
 98 }
 99 if (temp==0)
100 break ;
101 }
102 if (temp){
103 cout<<"-1"<<endl;
104 continue ;
105 }
106 int flag=0;
107 for (int i=n-1;i>=0;i--){
108 if (a[i]==100)
109 continue ;
110 if (flag==0&&a[i]==0)
111 break ;
112 cout<<a[i];
113 flag=1;
114 }
115 if (flag==0)
116 cout<<"0";
117 cout<<endl;
118 }
119 else {
120 int temp=1;
121 int flag=0;
122 for (int i=n-1;i>=0;i--){
123 if (a[i]==sign[2]&&temp){
124 temp=0;
125 continue ;
126 }
127 if (flag==0&&a[i]==0)
128 break ;
129 flag=1;
130 cout<<a[i];
131 }
132 if (flag==0)
133 cout<<"0";
134 cout<<endl;
135 }
136 }
137 }
138 return 0;
139 }

View Code

转载于:https://www.cnblogs.com/gfc-g/p/3844825.html

CodeForces 214B Hometask相关推荐

  1. 【CodeForces - 214B】Hometask (模拟,有坑)

    题干: Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants ...

  2. 【CodeForces - 155C】Hometask (字符串,思维,贪心,熟悉句式)(总结)

    题干: Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the ...

  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 375D Tree and Queries

    传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...

  5. 「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)

    题意与分析(CodeForces 540D) 是一道概率dp题. 不过我没把它当dp做... 我就是凭着概率的直觉写的,还好这题不算难. 这题的重点在于考虑概率:他们喜相逢的概率是多少?考虑超几何分布 ...

  6. 【codeforces 812C】Sagheer and Nubian Market

    [题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...

  7. CodeForces 获得数据

    针对程序的输出可以看见 CodeForces :当输入.输出超过一定字符,会隐藏内容 所以:分若干个程序进行输入数据的获取 1. 1 for (i=1;i<=q;i++) 2 { 3 scanf ...

  8. codeforces水题100道 第二十七题 Codeforces Round #172 (Div. 2) A. Word Capitalization (strings)...

    题目链接:http://www.codeforces.com/problemset/problem/281/A 题意:将一个英文字母的首字母变成大写,然后输出. C++代码: #include < ...

  9. CodeForces 595A

    题目链接: http://codeforces.com/problemset/problem/595/A 题意: 一栋楼,有n层,每层有m户,每户有2个窗户,问这栋楼还有多少户没有睡觉(只要一个窗户灯 ...

最新文章

  1. Java学习_day009面向对象(oop):对象和类(下)
  2. centos 程序 mysql数据库文件位置,CentOS 更改MySQL数据库目录位置
  3. MVC基础知识-View
  4. tomcat部署项目启动采坑之UnknownHostException
  5. [转发]在Visual Studio 2010/2012/2013/2015上使用C#开发Android/IOS安装包和操作步骤
  6. 计算机网络笔记(王道考研) 第一章:计算机网络体系结构
  7. static analysis tool
  8. 基于单片机的指纹识别电子密码锁设计
  9. 重庆邮电大学c语言题库
  10. 《畅玩NAS》家庭 NAS 服务器搭建方案
  11. 【附源码】计算机毕业设计java原创网络文学管理系统设计与实现
  12. 直播间小游戏,带货直播源码如何实现?
  13. 漫画 | “道德沦丧”的程序员 !
  14. 【Python】多个文件夹合并到一个文件夹中
  15. 视频处理系列︱利用达摩院ModelScope进行视频人物分割+背景切换(一)
  16. 武汉软件工程职业学院普通话测试站,湖北普通话测试中心地址及联系方式一览表(65个)...
  17. Python简笔画发射爱心
  18. 概率论与数理统计(第二版) 吴传生 编 高等教育出版社 大学课后习题答案
  19. IOS逆向--performSelector动态调用
  20. mysql 一个update语句 对主表内容和子表批量修改

热门文章

  1. mpi tcp连接报错_PHP Swoole长连接常见问题总结
  2. 一行代码实现数组降维去重排序
  3. python shell的交互模式和文本编辑模式
  4. 用python实现接口测试(八、实现序列化与反序列化)
  5. GROUPING amp; GROUPING_ID amp; GROUP_ID amp; GROUPING SETS
  6. Bundling and Minification
  7. VMware Workstation 端口映射 How to
  8. Linux下的硬件驱动——USB设备(下)
  9. python requests form data_Python requests模块 multipart/form-data类型文件上传
  10. 电脑文件太多找不到?试试这个免费搜索软件