『题解』Coderforces352A Jeff and Digits
Portal
Portal1: Codeforces
Portal2: Luogu
Description
Jeff's got n cards, each card contains either digit 0, or digit 5. Jeff can choose several cards and put them in a line so that he gets some number. What is the largest possible number divisible by 90 Jeff can make from the cards he's got?
Jeff must make the number without leading zero. At that, we assume that number 0 doesn't contain any leading zeroes. Jeff doesn't have to use all the cards.
Input
The first line contains integer \(n (1 \le n \le 103)\). The next line contains \(n\) integers \(a_1, a_2, \cdots , a_n (a_i = 0 or a_i = 5)\). Number ai represents the digit that is written on the \(i\)-th card.
Output
In a single line print the answer to the problem - the maximum number, divisible by 90. If you can't make any divisible by 90 number from the cards, print -1.
Sample Input1
4
5 0 5 0
Sample Output1
0
Sample Input2
11
5 5 5 5 5 5 5 5 0 5 5
Sample Output2
5555555550
Sample Explain
In the first test you can make only one number that is a multiple of 90 - 0.
In the second test you can make number \(5555555550\), it is a multiple of 90.
Description in Chinese
Jeff有一些数字卡片,上面为\(0\)或\(5\)。Jeff想用这些卡片组成一个数字,使这个数字可以被\(90\)整除并且尽可能的大。
Solution
先分解质因数:\(90 = 9 \times 10\)
为什么不分成\(90 = 2 \times 3^2 \times 5\)呢,因为\(9\)有自己的整除特性:各个数位之和被\(9\)整除。
\(10\)的整除特性就不用说了。
然后分类讨论:
当\(0\)的个数为\(0\),也就是没有\(0\),我们就直接输出\(-1\),因为无论怎么取都不能被\(10\)整除。
当\(5\)的个数\(\le 9\),而且存在\(0\)时,我们只能构造出\(9 | 0\)(这里的\(|\)表示整除),比如样例\(1\)。
当\(5\)的个数\(\ge 9\),而且存在\(0\)时,就可以构造能被\(90\)的数,比如样例\(2\)。
我们可以把\(5\)的个数\(\div 9\)取整后\(\times 9\)就得到所要输出的\(5\)的个数了,这个数一定是最大的能被9整除的数,当然最后要把所有的\(0\)输出。
Code
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n, x, sum1, sum2;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
if (x == 5) sum1++; else sum2++;//统计0和5的数量
}
if (sum2 == 0) {//情况1
printf("-1\n");
return 0;
}
if (sum1 < 9) {//情况2
printf("0\n");
return 0;
}
sum1 = sum1 / 9 * 9;//将5的个数÷9整除后×9,得到最终的5的个数
for (int i = 1; i <= sum1; i++)
printf("5");
for (int i = 1; i <= sum2; i++)
printf("0");
printf("\n");
return 0;
}
『题解』Coderforces352A Jeff and Digits的更多相关文章
- 『题解』洛谷P1063 能量项链
原文地址 Problem Portal Portal1:Luogu Portal2:LibreOJ Portal3:Vijos Description 在\(Mars\)星球上,每个\(Mars\)人 ...
- 『题解』Codeforces121A Lucky Sum
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Petya loves lucky numbers. Everybody k ...
- 『题解』Codeforces1142A The Beatles
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Recently a Golden Circle of Beetlovers ...
- 『题解』Codeforces1142B Lynyrd Skynyrd
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Recently Lynyrd and Skynyrd went to a ...
- 『题解』洛谷P1993 小K的农场
更好的阅读体验 Portal Portal1: Luogu Description 小\(K\)在\(\mathrm MC\)里面建立很多很多的农场,总共\(n\)个,以至于他自己都忘记了每个农场中种 ...
- 『题解』洛谷P2296 寻找道路
更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 在有向图\(\mathrm G\)中,每条边的长度均为\(1\),现给定起点和终点 ...
- 『题解』洛谷P1351 联合权值
更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 无向连通图\(\mathrm G\)有\(n\)个点,\(n - 1\)条边.点从 ...
- 『题解』Codeforces656E Out of Controls
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description You are given a complete undirected gr ...
- 『题解』洛谷P2170 选学霸
更好的阅读体验 Portal Portal1: Luogu Description 老师想从\(N\)名学生中选\(M\)人当学霸,但有\(K\)对人实力相当,如果实力相当的人中,一部分被选上,另一部 ...
随机推荐
- 关于未来实现API管理系统的几个关键词
下面将通过几个关键词的形式说明API管理的重要性和未来的实现方式. 1.生命周期管理 在整个API生命周期中更深入地集成所有工具将进一步提高生命周期循环的速度,而且更重要的是提供满足消费者需求的API ...
- KafkaStream低级别API
开发者可以通过Processor接口来实现自己的自定义处理逻辑.接口提供了Process和Punctuate方法. 其中:Process方法用于处理接受到的消息 Punctuate方法指定时间间隔周期 ...
- 使用Jmeter并发websocket协议项目
1.安装Jmeter 网址:http://jmeter.apache.org/下载 2.启动Jmeter \apache-jmeter-5.1.1\bin\jmeter.bat 3.安装‘Plugin ...
- 微信小程序学习总结
微信小程序开发环境安装以及相关设置配置 微信小程序前端页面书写 微信小程序前端样式WXSS书写 微信小程序中事件 微信小程序自定义组件 微信小程序发起请求 微信小程序登入流程 微信小程序路由跳转 微信 ...
- unittest中的方法调用时报错ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTest
调用unittest中的方法时报错: ValueError: no such test method in <class 'mytestcase.MyTestCase'>: runTest ...
- Kubernetes网络插件Flannel的三种工作模式
跨主机通信的一个解决方案是Flannel,由CoreOS推出,支持3种实现:UDP.VXLAN.host-gw 一.UDP模式(性能差) 核心就是通过TUN设备flannel0实现(TUN设备是工作在 ...
- Web前端助手-功能丰富的Chrome插件
整合优秀的前端实用工具.免费,可配置的强大工具集 示例 安装 github仓库: https://github.com/zxlie/FeHelper 官网地址:https://www.baidufe. ...
- PMBOK(第六版) PMP笔记——《十三》第十三章(项目干系人管理)
PMBOK(第六版) PMP笔记——<十三>第十三章(项目干系人管理) 第十三章 项目干系人管理: 了解干系人的需要和期望.解决实际发生的问题.管理利益冲突.促进干系人合理参与 项目决策和 ...
- 常用函数-String
/************************************************************************ 函数功能:将字符串中str的old_value子字符 ...
- MFC中如何分割CString类型的数据
[才疏学浅,难免有纰漏,若有不正确的地方,欢迎指教] MFC中有一个库函数 Tokenize(); 函数原型:CStringT Tokenize( PCXSTR pszTokens , int& ...