『题解』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\)对人实力相当,如果实力相当的人中,一部分被选上,另一部 ...
随机推荐
- B-微积分-sign(符号)函数
目录 sign(符号)函数 一.sign函数概述 二.python实现sign函数 更新.更全的<机器学习>的更新网站,更有python.go.数据结构与算法.爬虫.人工智能教学等着你:h ...
- C++学习笔记-预备知识
1.1 C++简介 C++融合3种不同的编程方式:C语言代表的过程性语言.C++在C语言基础上添加的类代表的面向对象语言.C++模板支持的广泛编程. 1.2 C++简史 1.2.1 C语言 Ritch ...
- Mac上Charles抓包iOS的https请求
介绍一款抓包工具,一般我在windows下使用Fiddler抓包,Fiddler使用教程这里就不讲了,重点介绍使用mac时的抓包工具----Charles. 进入官网 :Charles官网地址官网下载 ...
- JAVA阻塞(IO)和非阻塞(NIO)
查看这篇文章,了解更多关于Java的阻塞和非阻塞替代创建套接字的信息. 套接字使用TCP / IP传输协议,是两台主机之间的最后一块网络通信. 您通常不必处理它们,因为它们之上构建了协议,如HTTP或 ...
- 设计模式常见面试知识点总结(Java版)
设计模式 这篇总结主要是基于我设计模式系列的文章而形成的的.主要是把重要的知识点用自己的话说了一遍,可能会有一些错误,还望见谅和指点.谢谢 更多详细内容可以到我的cdsn博客上查看: https:// ...
- 题解:2018级算法第二次上机 Zexal的流水线问题
题目描述: 样例: 实现解释: 最基础的流水线调度问题,甚至没有开始和结束的值 实现方法即得出状态转移方程后完善即可,设a[][i]存储着第一二条线上各家的时间花费,t[][i]存储着i处进行线路切换 ...
- 【Java必修课】一图说尽排序,一文细说Sorting(Array、List、Stream的排序)
简说排序 排序是极其常见的使用场景,因为在生活中就有很多这样的实例.国家GDP排名.奥运奖牌排名.明星粉丝排名等,各大排行榜,给人的既是动力,也是压力. 而讲到排序,就会有各种排序算法和相关实现,本文 ...
- Python从入门到精通视频(全60集) ☝☝☝
Python从入门到精通视频(全60集) Python入门到精通 学习 教程 首先,课程的顺序需要调整:一和三主要是介绍学习和布置开发环境的,一介绍的是非VS开发,三介绍的是VS开发.VS2017现在 ...
- JZ2440 u-boot-2016.11、linux-4.17和busybox-1.28.4移植笔记
2018年5月份开始在JZ2440上陆续移植了u-boot-2016.11.u-boot-spl-2016.11.linux-4.17和busybox-1.28.4,其中linux-4.17和busy ...
- C++ set 用法略解
先看一段代码. #include<iostream> #include<set> #include<cstdio> #include<cstdlib> ...