当前位置:网站首页>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
边栏推荐
- Search tree judgment (25 points)
- 汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
- 洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別
- Stm32f103zet6 [development of standard library functions] - Introduction to library functions
- Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
- xctf刷题小记
- Go language self-study series | initialization of golang structure
- Non duplicate data values of two MySQL query tables
- 2021李宏毅机器学习之Adaptive Learning Rate
- Complete binary search tree (30 points)
猜你喜欢

L2-3 浪漫侧影 (25 分)

Valgrind et kcachegrind utilisent l'analyse d'exécution

深度学习框架中的自动微分及高阶导数

ONEFLOW learning notes: from functor to opexprinter

请提前布局 Star Trek突破链游全新玩法,市场热度持续高涨

Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动

Use of Arthas in JVM tools

论文阅读《Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry》

Notes on 30 steps of introduction to the Internet of things of yangtao electronics STM32 III. cubemx graphical programming and setting the IO port on the development board

MATLAB入门资料
随机推荐
Valgrind and kcache grind use run analysis
The K neighbors of each sample are obtained by packet switching
Flink reads MySQL and PgSQL at the same time, and the program will get stuck without logs
Test your machine learning pipeline
Use of Arthas in JVM tools
Latex paper typesetting operation
扣缴义务人
Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
基于点云凸包的凹包获取方法
Go language self-study series | golang method
Research purpose, construction goal, construction significance, technological innovation, technological effect
Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
洋桃电子STM32物联网入门30步笔记四、工程编译和下载
2022-04-22 OpenEBS云原生存储
Error: cannot find or load main class
【58】最后一个单词的长度【LeetCode】
mycat配置
关于cin,scanf和getline,getchar,cin.getline的混合使用
K210 learning notes (II) serial communication between k210 and stm32
Basic usage of synchronized locks
https://www.nowcoder.com/practice/1b46eb4cf3fa49b9965ac3c2c1caf5ad?tpId=37&&tqId=21285&rp=1&ru=/activity/oj&qru=/ta/huawei/question-ranking

