当前位置:网站首页>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);
}
}
边栏推荐
猜你喜欢

Selenium+Pytest自动化测试框架实战

ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法

MySQL uninstallation and installation methods

Simple use of promise in uniapp

Esp8266-rtos IOT development

vb. Net changes with the window, scales the size of the control and maintains its relative position
![[oc]- < getting started with UI> -- common controls uibutton](/img/4d/f5a62671068b26ef43f1101981c7bb.png)
[oc]- < getting started with UI> -- common controls uibutton
![[OC foundation framework] - string and date and time >](/img/75/e20064fd0066810135771a01f54360.png)
[OC foundation framework] - string and date and time >

LeetCode:498. Diagonal traversal

A convolution substitution of attention mechanism
随机推荐
Alibaba cloud server mining virus solution (practiced)
Pytest参数化你不知道的一些使用技巧 /你不知道的pytest
LeetCode:剑指 Offer 42. 连续子数组的最大和
Advance Computer Network Review(1)——FatTree
Intel Distiller工具包-量化实现3
什么是MySQL?MySql的学习之路是怎样的
LeetCode:剑指 Offer 04. 二维数组中的查找
LeetCode:394. 字符串解码
LeetCode:26. 删除有序数组中的重复项
How to effectively conduct automated testing?
Deep anatomy of C language -- C language keywords
Li Kou daily question 1 (2)
Niuke winter vacation training 6 maze 2
Unsupported operation exception
requests的深入刨析及封装调用
Intel Distiller工具包-量化实现1
BN折叠及其量化
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
LeetCode:124. Maximum path sum in binary tree
使用latex导出IEEE文献格式