当前位置:网站首页>AcWing 2456. 记事本
AcWing 2456. 记事本
2022-07-06 08:59:00 【Easenyang】
题目描述
最初在一个记事本上只有一个字符 A。
你每次可以对这个记事本进行两种操作:
Copy All (复制全部) :
你可以复制这个记事本中的所有字符(部分的复制是不允许的)。
Paste (粘贴) :
你可以粘贴你上一次复制的字符。 给定一个数字 n。
你需要使用最少的操作次数,在记事本中打印出恰好 n 个 A。
输出能够打印出 n 个 A 的最少操作次数。
输入格式
一个整数 n。
输出格式
一个整数,表示最少操作次数。
数据范围
1 ≤ n ≤ 1 0 6 1≤n≤10^6 1≤n≤106
题目链接:记事本
思路:
我们先考虑输入的数为质数 n 的情况:质数肯定是必须要先复制 1 次,然后粘贴 n - 1次。(例如 7 只能是复制 1 次,粘贴 6 次得到,大家可以在草稿本上推一次)
那么对于合数的情况,我们就可以去分解质因数,然后不断粘贴得到。
举个例子:
24 = 22 * 32。我们先去复制粘贴得到2,复制 1 次,粘贴 2 - 1次,得到 22 就又要复制 1 次,粘贴 2-1次,然后把22当成整体,复制 1 次,粘贴 3-1次,再复制 1 次,粘贴 2-1 次。共2x2+3x2 = 10次
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n = new Scanner(System.in).nextInt();
int res = 0;;
for (int i = 2; i <= n; i++) {
int s = 0; //看当前质因数用到了几次
while (n % i == 0) {
s++;
n /= i;
}
if (s > 0) {
res += i * s;
}
}
System.out.println(res);
}
}
边栏推荐
- Improved deep embedded clustering with local structure preservation (Idec)
- Post training quantification of bminf
- LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
- LeetCode:26. Remove duplicates from an ordered array
- Advance Computer Network Review(1)——FatTree
- [OC-Foundation框架]---【集合数组】
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Deep anatomy of C language -- C language keywords
- LeetCode:39. Combined sum
- After reading the programmer's story, I can't help covering my chest...
猜你喜欢
[OC-Foundation框架]---【集合数组】
Selenium+pytest automated test framework practice
MYSQL卸载方法与安装方法
Variable length parameter
Digital people anchor 618 sign language with goods, convenient for 27.8 million people with hearing impairment
IJCAI2022论文合集(持续更新中)
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Cesium draw points, lines, and faces
I-BERT
[oc]- < getting started with UI> -- learning common controls
随机推荐
CUDA implementation of self defined convolution attention operator
TDengine 社区问题双周精选 | 第三期
CSP first week of question brushing
力扣每日一题(二)
Nacos 的安装与服务的注册
What are the common processes of software stress testing? Professional software test reports issued by companies to share
LeetCode:124. 二叉树中的最大路径和
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
Navicat premium create MySQL create stored procedure
LeetCode:26. 删除有序数组中的重复项
[text generation] recommended in the collection of papers - Stanford researchers introduce time control methods to make long text generation more smooth
Advanced Computer Network Review(4)——Congestion Control of MPTCP
Esp8266-rtos IOT development
Variable length parameter
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
I-BERT
LeetCode:836. Rectangle overlap
Mongodb installation and basic operation
【嵌入式】Cortex M4F DSP库
Leetcode: Jianzhi offer 03 Duplicate numbers in array