zoukankan      html  css  js  c++  java
  • Java实现第九届蓝桥杯分数

    分数

    题目描述
    1/1 + 1/2 + 1/4 + 1/8 + 1/16 + …
    每项是前一项的一半,如果一共有20项,
    求这个和是多少,结果用分数表示出来。
    类似:
    3/2
    当然,这只是加了前2项而已。分子分母要求互质。

    注意:
    需要提交的是已经约分过的分数,中间任何位置不能含有空格。
    请不要填写任何多余的文字或符号。

    Sn=a1(1-q^n)/(1-q)
    
    public static void main(String[] args) {
    		int fenmu = (int)Math.pow(2, 19);
    		int fenzi = fenmu*2-1;
    		System.out.println(fenzi+"/"+fenmu);
    	}
    
    
    package 第八次模拟;
    
    public class Demo1分数 {
    	public static void main(String[] args) {
    		int sum=1;
    		int down=1;
    		for (int i = 2; i <=20; i++) {
    			sum=sum*2+1;
    			down*=2;
    		}
    		int gongyin=gcd(sum,down);
    		sum/=gongyin;
    		down/=gongyin;
    		System.out.println(sum+"/"+down);
    		
    		
    	}
    	private static int gcd(int a, int b) {
            if (b == 0) {
                return a;
            }
            return gcd(b, a % b);
        }
    
    }
    
    

    结果:1048575/524288

  • 相关阅读:
    MMU_段式映射
    MMU段式映射(VA -> PA)过程分析
    NOR FLASH驱动程序
    PCB上 如何显示 汉字
    poj1273 Drainage Ditches
    poj2349 Arctic Network
    poj3660 Cow Contest
    poj3259 Wormholes
    poj3159 Candies
    poj1011 Sticks
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13077544.html
Copyright © 2011-2022 走看看