zoukankan      html  css  js  c++  java
  • 基本算法实现

    1.最大公约数

    /*
     * GCD(A,B)=GCD(A,B mod A)循环求模实现最大公约数
     */
    public class Gcd {
    
    	public static void main(String[] args) {
    		int a,b,r;
    		Scanner sc=new Scanner(System.in);
    		a=sc.nextInt();
    		b=sc.nextInt();
    		r=a%b;
    		while(r!=0){
    			a=b;
    			b=r;
    			r=a%b;//一种类似方法的重用,靠循环实现
    		}
    		System.out.println(b);
    	}
    
    }

    2.最小公倍数

    /*
     * LCM(a,b)*GCD(a,b)=a*b--->LCM(a,b)=a*b/GCD(a,b)最小公倍数的求法
     */
    public class Lcm {
    	public static void main(String[] args) {
    		Scanner sc=new Scanner(System.in);
    		int a=sc.nextInt();
    		int b=sc.nextInt();
    		int r=a%b;
    		int mid=a*b;	//	保存之前的a*b,因为后面的a,b均已改变
    		int m;
    		while(r!=0){
    			a=b;
    			b=r;
    			r=a%b;
    		}
    		m=mid/b;
    		System.out.println(m);
    	}
    }

    3.n个数求最大公约数

    /*
     * 实现了不间断输入整数求最大公约数
     */
    
    public class NGcd2 {
    
    	public static void main(String[] args) {
    		Scanner sc=new Scanner(System.in);
    		int a=sc.nextInt();
    		int b;
    		int c=0;
    		while((b=sc.nextInt())!=0){//输入为0时表示结束,输入非零整数即展开计算最大公约数
    			int r=a%b;
    			while(r!=0){
    				a=b;
    				b=r;
    				r=a%b;
    			}
    			c=b;		//把每次求得的b传出去,因为下一轮的输入将覆盖前面计算的最大公约数
    		}
    		System.out.println(c);
    	}
    
    }
    

    4.求数根

    /*
     * 求一个数的数根,即为一个n位数的各个位上数字之和,结果若为大于9的数,则继续求其数根,直到结果不超过一位数为止.
     */
    public class NumRoot {
    	public static void main(String[] args) {
    		int num,sum=0;	
    		Scanner sc=new Scanner(System.in);
    		num=sc.nextInt();
    		while(num!=0){
    			if(num<=9)
    				break;
    			sum=0;
    			while(num>0){//想重复利用此代码段对求得数字继续求数根,但又没必要写一个方法,只需将此代码段包含在一个循环里
    				sum+=num%10;
    				num/=10;
    			}
    			num=sum;	
    		}
    		System.out.println(num);
    	}
    
    }

    5.百元百鸡

    /**
     * “百元买百鸡”是我国古代的著名数学题。题目这样描述:3元可以买1只公鸡,2元可以买一只母鸡,1元可以买3只小鸡。用100文钱买100只鸡,
     *  那么各有公鸡、母鸡、小鸡多少只?与之相似,有"鸡兔同笼"问题。
     */
    public class BuyChicken {
    
    	public static void main(String[] args) {
    		for(int x=1;x<=24;x++){
    			int y=(200-8*x)/5;
    			if((200-8*x)%5==0){
    				int z=100-x-y;
    			   System.out.print(x+" ");
    				System.out.print(y+" ");
    				System.out.print(z);
    				System.out.println();		
    			}	
    		}
    	}
    }

    6.水仙花数

    /*
     *求(100-999内的)水仙花数。所谓水仙花数,是指一个三位数abc,如果满足a^3+b^3+c^3=abc,则abc是水仙花数。
     */
    public class WaterFlower {
    	public static void main(String[] args) {
    		int a;
    		for(int i=100;i<1000;i++){
    			int sum=0,num=i;
    			while(num>0){
    				a=num%10;//分解三位数,每次取个位上的数字
    				sum+=a*a*a;
    				num/=10;//个位上数字取走后就可移除,用十位数代替
    			}
    			if(sum==i)
    				System.out.println(i);	
    		}
    	}
    }
    


    ================================== 赵客缦胡缨,吴钩霜雪明。 银鞍照白马,飒沓如流星。 ==================================
  • 相关阅读:
    【Shell】Shell介绍及常用shell脚本
    【Redis】Redis底层数据结构原理--简单动态字符串 链表 字典 跳跃表 整数集合 压缩列表等
    检查Mysql主从状态
    三种方式获取随机字符串或数字
    Intellij 编译时报 未结束的字符串字面值
    IDEA 远程调试
    kafka操作命令
    maven idea设置查找依赖优先从指定的本地仓库获取
    详解布隆过滤器的原理、使用场景和注意事项
    IDEA查找接口实现类及快速实现接口
  • 原文地址:https://www.cnblogs.com/lucare/p/9312691.html
Copyright © 2011-2022 走看看