当前位置:网站首页>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
边栏推荐
- Introduction to matlab
- Output first order traversal according to second order and middle order traversal (25 points)
- Consensus Token:web3. 0 super entrance of ecological flow
- Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动
- Latex paper typesetting operation
- Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing
- Consensus Token:web3.0生态流量的超级入口
- 资源打包关系依赖树
- 【58】最后一个单词的长度【LeetCode】
- Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
猜你喜欢

Multi view depth estimation by fusing single view depth probability with multi view geometry

L2-024 部落 (25 分)(并查集)

L2-3 romantic silhouette (25 points)

共享办公室,提升入驻体验

php基于哈希算法出现的强弱比较漏洞

Automatic differentiation and higher order derivative in deep learning framework

调包求得每个样本的k个邻居

Reference passing 1

Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise

2022-04-22 openebs cloud native storage
随机推荐
Restore binary tree (25 points)
tsdf +mvs
STM32使用HAL库,整体结构和函数原理介绍
求简单类型的矩阵和
Redis Desktop Manager for Mac(Redis可视化工具)
L2-023 graph coloring problem (25 points) (graph traversal)
ONEFLOW learning notes: from functor to opexprinter
调包求得每个样本的k个邻居
汇编语言与逆向工程 栈溢出漏洞逆向分析实验报告
Find the sum of simple types of matrices
cadence的工艺角仿真、蒙特卡洛仿真、PSRR
Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
The K neighbors of each sample are obtained by packet switching
L2-024 tribe (25 points) (and check the collection)
【精品】利用动态代理实现事务统一管理 二
Swagger document export custom V2 / API docs interception
Complete binary search tree (30 points)
Idea is configured to connect to the remote database mysql, or Navicat fails to connect to the remote database (solved)
uni-app和微信小程序中的getCurrentPages()
计算神经网络推理时间的正确方法
https://www.nowcoder.com/practice/1b46eb4cf3fa49b9965ac3c2c1caf5ad?tpId=37&&tqId=21285&rp=1&ru=/activity/oj&qru=/ta/huawei/question-ranking

