1 条题解

  • 0
    @ 2022-6-17 16:33:47

    C :

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
     
     
    void work(int n)
    {
    	if(n==1)//初始判断条件,如果n为1或2则直接输出 
    	{
    		printf("2(0)");
    		return;
    	}
    	else if(n==2)
    	{
    		printf("2");
    		return; 
    	} 
    	else
    	{
    		int j=1,i=0;//j每次乘2,如果大于了n就分解结束,i为当前次数 
    		do
    		{
    			j*=2;
    			if(j>n)
    			{
    				j/=2;
    				if(i==1)//这步非常重要,确定是否需要继续 2() 
    					printf("2");
    				else
    				{
    					printf("2(");
    					work(i);
    					printf(")");
    				}	
    				if(n-j!=0)//如果n分解之后还有剩余的数,那么继续分解 
    				{
    					printf("+");
    					work(n-j);
    				}
    				return;
    			}
    			else
    				i++;
    			
    			
    		}while(1);
    	}	
    					
    	
    }
     
    int main()
    {
    	int n;
    	scanf("%d",&n);
    	work(n);
    }
    
    

    C++ :

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    void print(int n)
    {
        int p=0;
        while((1<<p)<=n) p++;
        p--;
        if(p==0) printf("2(0)");
        else if(p==1) printf("2");
        else
        {
            printf("2(");
            print(p);
            printf(")");
        }
        int remain=n-(1<<p);
        if(remain)
        {
            printf("+");
            print(remain);
        }
    }
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            print(n);
            printf("\n");
        }
        return 0;
    }
    
    

    Pascal :

    var
      n:integer;
    
    procedure bin(k:integer);
    var
      b:array[0..15] of integer;
      i,p:integer;
      first:boolean;
    begin
      p:=-1;
      if k=0 then
        write(0)
      else begin
        while k>0 do
        begin
          inc(p);
          b[p]:=k mod 2;
          k:=k div 2;
        end;
        first:=true;
        for i:=p downto 0 do
          if b[i]=1 then
          begin
            if first then first:=false else write('+');
            if i=1 then write('2')
            else begin
              write('2(');
              bin(i);
              write(')');
            end;
          end;
      end;
    end;
    
    begin
      readln(n);
      bin(n);
      writeln;
    end.
    
    

    Java :

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		String str = "";
    
    		Scanner scan = new Scanner(System.in);
    		int num = scan.nextInt();
    		scan.close();
    		String res = doWhile(num, str);
    		System.out.println(res);
    	}
    
    	private static String doWhile(int num, String str) {
    		int product = 1, n = 0;
    
    		if (num == 1) {
    			return "2(0)";
    		} else if (num == 2) {
    			return "2";
    		}
    
    		while (product * 2 <= num) {
    			product *= 2;
    			++n;
    		}
    		if (num - product != 0) {
    			int diff = num - product;
    			str = "2(" + doWhile(n, str) + ")+" + doWhile(diff, str);
    		} else {
    			str = "2(" + doWhile(n, str) + ")";
    		}
    		str = str.replace("2(2(0))", "2");
    		return str;
    	}
    }
    

    信息

    ID
    2986
    时间
    1000ms
    内存
    125MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者