目录
1. 二进制1的个数
求一个整数,在内存当中存储时,二进制1的个数。
和比它小1的数&,即可一次排除二进制中的1
//求一个整数,在内存当中存储时,二进制1的个数。
public class test {
public static int func(int n) {
int num = 0;
while (n != 0) {
n &= (n-1);
num++;
}
return num;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(func(n));
}
}
// 7 -> 3
// -1 -> 32
2. 求1个数字是不是2的k次方(不用求k的值)
思路:只要二进制中只有一个1,就是2的k次方。
即:和比它小1的数取&为0,就是2的k次方。
public class test {
public static boolean func(int n) {
if ((n & (n-1)) == 0) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(func(n));
}
}
优化:可以直接return
return ((n & (n-1)) == 0);
3. 二进制序列
获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列
思路:通过移位运算符,移位后&1 再打印
//获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列
public class test {
public static void func(int n) {
System.out.println("打印奇数位:");
for (int i = 30; i >= 0; i-= 2) {
System.out.print(((n >> i)&1) + " ");
}
System.out.println();
System.out.println("打印偶数位:");
for (int i = 31; i >= 0; i -=2) {
System.out.print(((n >> i)&1) + " ");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
func(n);
}
}
/*
3
打印奇数位:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
打印偶数位:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
*/
4. 输出一个整数的每一位
输出一个整数的每一位,如:123的每一位是1 , 2 , 3
思路:递归
//输出一个整数的每一位,如:123的每一位是1 , 2 , 3
public class test {
public static void func(int n) {
if ((n/10) != 0) {
func(n/10);
}
System.out.println(n%10);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
func(n);
}
}
5. 输出乘法口诀表
//输出乘法口诀表
public class test {
public static void func(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
int ret = i * j;
System.out.print(i+" * "+j+" = "+ i*j+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
func(n);
}
}
6. 模拟登陆
编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 可以重新输 入,最多输入三次。三次均错,则提示退出程序
public class test {
public static void guessPassword() {
Scanner sc = new Scanner(System.in);
int count = 3;
while (count != 0) {
System.out.print("请输入密码:");
String password = sc.nextLine();
if (password.equals("bit")) {
System.out.println("登录成功!");
return;
} else {
count--;
System.out.println("你还有"+count+"次机会");
}
}
}
public static void main(String[] args) {
guessPassword();
}
}
7. 求N的阶乘(非递归)
//求 N 的阶乘(迭代)
public class test {
//
public static int func(int n) {
int ret = 1;
while (n != 0) {
ret *= n;
n--;
}
return ret;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(func(n));
}
}
8. 求N的阶乘(递归)
//求 N 的阶乘(递归)
public class test {
//求n的阶乘方法
public static int func(int n) {
if (n == 1) {
return 1;
}
return n * func(n-1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(func(n));
}
}
8. 求N的阶乘的和(非递归)
//求N的阶乘的和(非递归)
public class test {
//求和
public static int sum(int n) {
int sum =0;
while (n != 0) {
sum += func(n);
n--;
}
return sum;
}
//求阶乘
public static int func(int n) {
int ret = 1;
while (n != 0) {
ret *= n;
n--;
}
return ret;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(sum(n));
}
}
9. 求N的阶乘的和(递归)
会栈溢出!!
//求N的阶乘的和(递归)
public class test {
//求和
public static int sum(int n) {
int sum = 0;
if (n == 1) {
return 1;
}
return func(n) + sum(func(n-1));
}
//N的阶乘
public static int func(int n) {
if (n == 1) {
return 1;
}
return n * func(n-1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(sum(n));
}
}