👀作者简介:大家好,我是大杉。
🚩🚩 个人主页:爱编程的大杉
支持我:点赞+关注~不迷路🧡🧡🧡
✔系列专栏:javase基础⚡⚡⚡
(❁´◡`❁)励志格言:生命可以随心所欲,但不能随波逐流。(宫崎骏 《猫的报恩》)🤞🤞
一.二进制移位篇:
1.题目一:
//打 印一个数二进制的奇数位数和偶数位数字
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
//奇数位至少要移动一位
for(int i=31;i>=1;i-=2)
{
System.out.print(((n >> i) & 1)+" ");
//和数字一的二进制进行取与运算
}
System.out.println();
//从左往右看,右移动30次正好到正数第二位,
for(int i=30;i>=0;i-=2)
{
System.out.print(((n >> i) & 1)+" ");
}
}
2.题目二:
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int count=0;
for(int i=0;i<32;i++)
{
if(((n>>i)&1)==1)
{
count++;
}
}
System.out.println("这个数的二进制位有"+count+"个1");
}
使用Scanner是不要忘记调包import java.util.Scanner;
二:解密篇:
//三次机会猜密码
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String k="mediumint";
int count=3;
while(count!=0)
{
System.out.println("please input the password!");
String password=sc.nextLine();
if(password.equals(k))
{
System.out.println("you's input is right");
break;
}
else
{
count--;
System.out.println("you also have"+count+"'s chance");
}
}
}
三.“个性数字”篇
1.题目一:
public static void main(String[] args) {
for(int i=1;i<=999999;i++) {
int count = 0;
int temp = i;
while (temp != 0) {
count++;
temp = temp / 10;
}
temp = i;
int sum = 0;
while (temp != 0) {
sum += Math.pow(temp % 10, count);
temp /= 10;
}
if (sum == i) {
System.out.println(i);
}
}
}
2.题目二
public static void main(String[] args) {
for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
{
System.out.print(i+"*"+j+"="+i*j+" ");
}
System.out.print("\n");
}
}