来自澳大利亚的兵


私信TA

用户名:zhangjun678

访问量:2965

签 名:

等  级
排  名 244
经  验 5875
参赛次数 0
文章发表 28
年  龄 0
在职情况 学生
学  校 djtu
专  业 计算机科学与技术

  自我简介:

喜欢数学,编程小白

解题思路:

注意事项:

参考代码:

import java.util.Scanner;

public class Main{

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int N=scanner.nextInt();

        int some=0;

        int i=0;

        int a=N;

        while(a!=1){

            a=a/2;

            i++;

        }

        int []m=new int[i+1];

        double k=N;

        for(int j=i;j>=0;j--)

        {


             k=k-Math.pow(2,j);

            if(k<0){

               m[j]=-1;

               k=k+Math.pow(2,j);

            }

            else m[j]=j;

        }

       for(int p=0;p<=i;p++ ){

           if(m[p]!=-1){

                some=p;

                break;

           }


       }

       System.out.print(N+"=");

    for(int r=i;r>=0;r--){

        if(m[r]!=-1&&r!=some){

            System.out.print((int)(Math.pow(2,m[r]))+"+");

        }

        if(r==some){

            System.out.print((int)(Math.pow(2,m[r])));

            break;

        }

    }

    }

}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区