1、二进制1的个数 2、最大公约数 3、1/1-1/2+1/3-1/4… + 1/99 - 1/100 的值 4、水仙花数 5、猜数字游戏

1、求一个整数,在内存当中存储时,二进制1的个数

import java.util.Scanner;
public class Binary {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the number:");
        int n=input.nextInt();
        int count = 0 ;
        while(n > 0){
            if(n % 2 == 1)
                count++;
            n = n / 2 ;
        }
        System.out.println("二进制中1的个数为"+count);
    }
}

2、求两个正整数的最大公约数

import java.util.Scanner;
public class Divisor {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter two numbers:");
        int a = input.nextInt();
        int b = input.nextInt();
        int n,i;
        n = (a < b) ? a : b ;
        for(i = n ; i > 0 ; i--){
            if(a % i == 0 && b % i == 0)
                break;
        }
        System.out.println("The greatest common divisor of "+a+" and "+b+" is " +i);
    }
}

3、计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值

public class Calculate {
    public static void main(String args[]){
        double sum1 = 0,sum2 = 0;
        for(int i = 1 ; i < 100 ; i = i+2){
            sum1 +=  1.0/i;
        }
        for(int i = 2 ; i <= 100 ; i = i+2){
            sum2 +=  1.0/i;
        }
        System.out.println(sum1-sum2);
    }
}

4、求出0~999之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数,其各位数字的立方和恰好等于该数本身,如;153=1+5+3?,则153是一个“水仙花数“。)

public class Calculate {
    public static void main(String args[]){
        int i,j ,k,n;
        for( i = 0 ; i <= 9 ; i++){
            for( j = 0 ; j <= 9 ; j++){
                for( k = 0; k <= 9; k++){
                    n = i*100+j*10+k;
                    if( n == i*i*i+j*j*j+k*k*k && n >100)
                        System.out.println( n );
                }
            }
        }
    }
}

5、完成猜数字游戏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值