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

  • 相关阅读:
    bzoj [POI2015]Myjnie
    bzoj2217 [Poi2011]Lollipop
    Codeforces A Mist of Florescence
    bzoj4380 [POI2015]Myjnie
    bzoj4292 [PA2015]Równanie
    bzoj 3517翻硬币
    模块补充
    python解释器
    __file__、__name__、__dict__方法整理
    软件开发规范
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13077544.html
Copyright © 2011-2022 走看看