当前位置:网站首页>Brush classic topics
Brush classic topics
2022-04-23 08:53:00 【Red apples are delicious】
Catalog
1. Find binary in input integer 1 The number of
1. Find binary in input integer 1 The number of
① Topics and examples :
② Method resolution :
This problem is very similar to the one we brushed yesterday , Its purpose is to calculate the binary lower 1 The number of , Then we can use bitwise and 1 To calculate , Move the number to be calculated to the right one bit at a time , The result is 1, be count++;
However, it is also worth noting that the title emphasizes paying attention to multiple groups of input and output , So we will use scanner Medium scanner.hasNext() This method , Let's take a look at some of its instructions
This is a reference jdk1.8 Result . in other words , At the end of a set of data output , It doesn't stop the program process immediately , It will block the program first , Enter the next set of data .
③ The code is as follows :
import java.util.*; public class demo4 { public static void main(String[]args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); int count=0; while(n!=0){ if((n&1)==1){ count++; } n>>=1; } System.out.println(count); } } }
2. Perfect number calculation
① Topics and examples :
② Method resolution :
The meaning of this topic will not be explained too much , The problem is very simple , It's mainly about the divisor and ( Remove itself ) To judge . What we should pay attention to in this question is that when judging the divisor, we can choose from the range cut , Because the two are corresponding . And because it is not selected , So we can divide our relative divisor 1 Add it directly to the end to judge .
③ The code is as follows :
import java.util.*; public class Main { public static void main(String[]args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int count=0; for(int i=2;i<=n;i++){ if(isPerfectNum(i)){ count++; } }System.out.println(count); }// Judge the divisor public static boolean isPerfectNum(int i){ int sum=0; for(int j=2;j<Math.sqrt(i);j++){ if((i%j==0)){ sum+=j+(i/j); } } if(i==sum+1){ return true; }else{ return false; } } }
版权声明
本文为[Red apples are delicious]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230817006897.html
边栏推荐
- 是否同一棵二叉搜索树 (25 分)
- Concave hull acquisition method based on convex hull of point cloud
- 基于点云凸包的凹包获取方法
- 2022-04-22 openebs cloud native storage
- Go语言自学系列 | golang方法
- 洋桃电子STM32物联网入门30步笔记二、CubeIDE下载、安装、汉化、设置
- Latex paper typesetting operation
- Non duplicate data values of two MySQL query tables
- L2-024 部落 (25 分)(并查集)
- Use of Arthas in JVM tools
猜你喜欢
L2-3 romantic silhouette (25 points)
Learn SQL injection in sqli liabs (Level 11 ~ level 20)
L2-3 浪漫侧影 (25 分)
Automatic differentiation and higher order derivative in deep learning framework
LaTeX论文排版操作
洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解
Solidity 问题汇总
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
2022-04-22 openebs cloud native storage
K210 learning notes (II) serial communication between k210 and stm32
随机推荐
xctf刷题小记
基于点云凸包的凹包获取方法
关于数组复制问题
搜索树判断 (25 分)
共享办公室,提升入驻体验
idea打包 jar文件
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
求简单类型的矩阵和
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
【58】最后一个单词的长度【LeetCode】
Taxable income
Study notes of deep learning (8)
Summary of solid problems
Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting
Consensus Token:web3. 0 super entrance of ecological flow
Find the sum of simple types of matrices
完全二叉搜索树 (30 分)
Chris LATTNER, father of llvm: the golden age of compilers
php基于哈希算法出现的强弱比较漏洞
Get the absolute path of the class according to the bytecode