当前位置:网站首页>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
边栏推荐
- Go language self-study series | initialization of golang structure
- Flink reads MySQL and PgSQL at the same time, and the program will get stuck without logs
- 洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
- RCC introduction of Hal Library
- 调包求得每个样本的k个邻居
- 是否完全二叉搜索树 (30 分)
- Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
- 【58】最后一个单词的长度【LeetCode】
- Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library
- Complete binary search tree (30 points)
猜你喜欢

Experimental report on analysis of overflow vulnerability of assembly language and reverse engineering stack

洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口

K210 learning notes (II) serial communication between k210 and stm32

Valgrind and kcache grind use run analysis

The K neighbors of each sample are obtained by packet switching

2021 Li Hongyi's adaptive learning rate of machine learning

cadence的工艺角仿真、蒙特卡洛仿真、PSRR

四张图弄懂matplotlib的一些基本用法

Learn SQL injection in sqli liabs (Level 11 ~ level 20)

企业微信应用授权/静默登录
随机推荐
Reference passing 1
Solidity 问题汇总
Complete binary search tree (30 points)
After a circle, I sorted out this set of interview questions..
Wechat: get the owner of a single tag
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
Redis Desktop Manager for Mac(Redis可视化工具)
2022-04-22 openebs cloud native storage
Go语言自学系列 | golang结构体作为函数参数
Taxable income
Flink reads MySQL and PgSQL at the same time, and the program will get stuck without logs
MATLAB入门资料
企业微信应用授权/静默登录
Go语言自学系列 | golang结构体的初始化
idea打包 jar文件
Judgment on heap (25 points) two insertion methods
Introduction to GUI programming swing
Find the sum of simple types of matrices
政务中台研究目的建设目标,建设意义,技术创新点,技术效果
Technological innovation in government affairs in the construction of Digital Government
https://www.nowcoder.com/practice/1b46eb4cf3fa49b9965ac3c2c1caf5ad?tpId=37&&tqId=21285&rp=1&ru=/activity/oj&qru=/ta/huawei/question-ranking

