目录
1. 在Java中一个数字开根号使用的是: Math.sqrt( )
3.在Java中当要求一个数的几次幂使用的是 Math.pow 方法
6.在Java中字符串的比较相等使用的是 equals 方法
8.short a=128; byte b=(byte) a; 此时b的值。
1. 在Java中一个数字开根号使用的是: Math.sqrt( )
public static void main(String[] args) {
//判定一个数字是否是素数
//素数是只能被1和自己整除的数字,1不是素数
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i=0;
//一个数总可以写成2个数的乘积的形式,a*b
// 所以说a和b都一定是 <= 根号n 的
for(i=2;i<=Math.sqrt(n);i++){
if(n%i==0){
System.out.println(n+"不是素数");
break;
}
}
if(i>Math.sqrt(n)){
System.out.println(n+"是素数");
}
}
2. 水仙花数:
(“ 水仙花数 ” 是指一个三位数,其各位数字的立方和确好等于该数本身,如: 153 = 1^3 + 5^3 + 3^3 ,则 153 是一个 “ 水仙花数 ” 。 )
public static void main(String[] args) {
// 求一个数是不是水仙花数
// (“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数
// 本身,如: 153=1^3+5^3+3^3 ,则153是一个“水仙花数”。)
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=n;
int count=0;
while(m!=0){
count++;
m/=10;
}
int a=n%10;
int b=n/10;
int sum=0;
if(count==3){ // 水仙花数是三位数,所以只有三位
for (int j = 0; j < count; j++) {
int ret=1;
for (int i = 0; i < count; i++) {
ret*=a;
}
sum+=ret;
a=b%10;
b=b/10;
}
if(sum==n){
System.out.println(n+"是水仙花数");
}else{
System.out.println(n+"不是水仙花数");
}
}else{
System.out.println(n+"不是水仙花数");
}
sc.close();
}
3.在Java中当要求一个数的几次幂使用的是 Math.pow 方法
要 求 tmp 的 count 次方,使用的是 Math.pow 方法
Math.pow(tmp , count );
4.写一个函数返回参数二进制中1的个数
例如:15 ----》 0000 1111 -----》 4个1
解题思路:
1.与1&32次 (对于int类型的数据,就是4个字节,就是32bit,所以需要 & 32)
2.&到n为0的时候就停止
3. n&(n-1)直到n为0
原理:
就让a&1,这样的时候,就是1的二进制是0001,所以a&1就是可以判断出a 的二进制的最低位是否是1,如果是1,&完以后的值会是等于1,如果不是1,那么&以后的值就是 0 ,
然后把a无符号的右移,这样就是在a的左边补0 ,把a二进制进行右移,所以说,当a无符号右移(>>>)到值为0了以后,就不需要比较了&了,这时 就可以得到我们想要的结果了。
3.让n&(n-1)直到n为0为止,这时&的次数就是原来n的二进制中1的个数,每&一次在二进制中就会少了一个1
第一个方法的代码:
public static void main(String[] args) { // 写一个函数返回参数二进制中 1 的个数 //比如: 15 -----> 0000 1111 ----> 有 4 个 1 int n=15; int count=0; for (int i = 0; i < 32; i++) { int ret=n&1; if(ret==1){ count++; } n=n>>>1; } System.out.println(count); }
第二个方法的代码:
public static void main(String[] args) { // 写一个函数返回参数二进制中 1 的个数 //比如: 15 -----> 0000 1111 ----> 有 4 个 1 int n=15; int count=0; while(n!=0){ int ret=n&1; if(ret==1){ count++; } n=n>>>1; } System.out.println(count); }
第三个方法的代码:
public static void main(String[] args) { // 写一个函数返回参数二进制中 1 的个数 //比如: 15 -----> 0000 1111 ----> 有 4 个 1 int n=15; int count=0; while(n!=0){ n=n&(n-1); count++; } System.out.println(count); }
5.打印X图形
public static void main(String[] args) {
//打印X图形
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(i==j || i+j==n-1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
sc.close();
}
6.在Java中字符串的比较相等使用的是 equals 方法
字符串的比较相等不能这样使用 == 直接比较相等了。
可以使用String类型的 equals 方法 来比较相等,
passWord.equals(" "),来比较相等。
7.合并2个有序数组
参考答案1:
在A数组原地操作的话,可以选择倒序从大到小进行操作,用两个指针 i 和 j 分别从大到小遍历A数组和B数组,将较大的那个放在A数组中应该对应的位置 k 即可。(把比较以后的结果放在A数组的k下标的位置。)
public static void merge(int[] nums1, int m, int[] nums2, int n)
{
//把nums1和nums2合并到nums1中
int end=m+n-1;
int end1=m-1;
int end2=n-1;
//从数组的后面开始遍历,然后取出大的数据元素放到nums1的后面去
while(end1>=0&&end2>=0){
if(nums1[end1]>nums2[end2]){
nums1[end]=nums1[end1];
end1--;
end--;
}else{
nums1[end]=nums2[end2];
end2--;
end--;
}
}
while(end2>=0){
nums1[end]=nums2[end2];
end2--;
end--;
}
}
参考答案2:
使用一个额外的数组空间 arr ,然后分别比较A数组和B数组的各个元素的大小,把比较后较小的值放到新开辟的数组 arr 中,当把A数组和B数组的元素都放到新开辟的数组 arr 中以后,----------- 再把新开辟的数组 arr 的各个元素再拷贝到A数组中。
public void merge(int A[], int m, int B[], int n) {
//合并2个有序数组
//把B合并到A中
int a=0;
int b=0;
int[] array=new int[m+n];
int i=0;
while(a<m&&b<n)
{
if(A[a]>B[b]){
array[i]=B[b];
i++;
b++;
}else{
array[i]=A[a];
i++;
a++;
}
}
if(a<m){
for (int j = a; j < m; j++) {
array[i]=A[a];
i++;
}
}
if(b<n){
for (int j = b ;j < n; j++) {
array[i]=B[b];
i++;
}
}
for (int j = 0; j < m+n; j++) {
A[j]=array[j];
}
}
递归调用可能会出现栈溢出错误:这个错误是必须程序员来解决的。
8.short a=128; byte b=(byte) a; 此时b的值。
此时输出结果:a=128 b=-128
解题思路:
short类型(二个字节)的128的二进制是:00000000 10000000
byte类型(一个字节)是1bit,就是8个字节:把a赋值给b,所以b就是:10000000
1成为了最高位,就是符号位了。所以此时b的值就是 : -128
9.闰年的分类:(闰年小知识)
10.IDEA 调试过程
1.打断点
2.按Debug按钮
在IDEA这个编译器中是不需要自己添加监视,编译器会自己把变量加载到这里
对于调试过程中各个按钮的作用:
代码的执行打印结果:
条件断点:先打断点,然后鼠标右键 (如下图所示)
在框框里面写条件
然后点击Done就完成了。
然后就可以Debug开始调试了
11. 如何让电脑生成随机数(左闭右开。)
列举一些例子,更方便理解:( 在让电脑生成随机数这里)
例1. 这个代码会生成 [ 0 , 100 ) 之间的随机数,左闭右开。
Random random=new Random();
int randNum=random.nextInt(100);
//会生成 [ 0 , 100 ) 之间的随机整数
System.out.println(randNum);
例2. 生成 [ 100 , 200 ) 之间的随机数,左闭右开:
Random random=new Random();
//会生成 [ 100 , 200 ) 之间的随机整数
int randNum=random.nextInt(100)+100;
System.out.println(randNum);
例3. 生成 [ 50 , 100 ) 之间的随机数:
Random random=new Random();
int randNum2=random.nextInt(50)+50;
//会生成 [50 , 100 ) 之间的随机整数
System.out.println(randNum2);
例4. 如果想要每次生成的都是一样的随机数,那么可以这样做:
Random random=new Random(123);
//这样做以后每次生成的都是一样的随机数字了
int randNum=random.nextInt(100)+100;
//会生成 [100 , 200 ) 之间的一个随机整数
System.out.println(randNum);