1 条题解

  • 0
    @ 2025-4-14 18:41:38

    C :

    // test.cpp : 定义控制台应用程序的入口点。
    //
    
    #include <stdio.h>
    #include <math.h>
    long long S[20];
    
    int bittest(unsigned int num, int bit)
    {
    	if ((num >> bit & 0x01) == 1)
    		return 1;
    	else
    		return 0;
    }
    
    long long sum(long long s[20], unsigned int i, int n){
    	long long t = 0;
    	for (int j = 0; j < n; j++){
    		if (bittest(i, j) == 1){
    			t += s[j];
    		}
    	}
    	return t;
    }
    
    long long subtract(long long t1,long long t2){
    	if (t1>t2){
    		return t1 - t2;
    	}
    	else{
    		return t2 - t1;
    	}
    }
    
    int main()
    {
    	int n;
    	while (scanf("%d", &n) == 1 && n != 0){
    		long long t = 0;
    		for (int i = 0; i < n; i++){
    			scanf("%ld", &S[i]);
    			t += S[i];
    		}
    		for (unsigned int i = 0; i < (unsigned int)pow(2.0, n - 1); i++){
    			long long r = subtract(sum(S, i, n), sum(S, ~i, n));
    			if (t>r){
    				t = r;
    			}
    
    		}
    		printf("%lld\n", t);
    	}
    	return 0;
    }
    
    

    C++ :

    // test.cpp : 定义控制台应用程序的入口点。
    //
    
    #include <stdio.h>
    #include <math.h>
    long long S[20];
    
    int bittest(unsigned int num, int bit)
    {
    	if ((num >> bit & 0x01) == 1)
    		return 1;
    	else
    		return 0;
    }
    
    long long sum(long long s[20], unsigned int i, int n){
    	long long t = 0;
    	for (int j = 0; j < n; j++){
    		if (bittest(i, j) == 1){
    			t += s[j];
    		}
    	}
    	return t;
    }
    
    long long subtract(long long t1,long long t2){
    	if (t1>t2){
    		return t1 - t2;
    	}
    	else{
    		return t2 - t1;
    	}
    }
    
    int main()
    {
    	int n;
    	while (scanf("%d", &n) == 1 && n != 0){
    		long long t = 0;
    		for (int i = 0; i < n; i++){
    			scanf("%ld", &S[i]);
    			t += S[i];
    		}
    		for (unsigned int i = 0; i < (unsigned int)pow(2.0, n - 1); i++){
    			long long r = subtract(sum(S, i, n), sum(S, ~i, n));
    			if (t>r){
    				t = r;
    			}
    
    		}
    		printf("%lld\n", t);
    	}
    	return 0;
    }
    
    

    Java :

    import java.util.Scanner;
    
    public class Main {
        public static void main(String args[]){
            long to[]=new long[10];
            //System.out.println("ytfhgaaaaaaaaaaaahggg");
            int tou=0;
            Scanner scan2=new Scanner(System.in);
            while(true)
            {
                long n[]=new long[20];
                int u;
                boolean turn[]=new boolean[20];
    
               // System.out.println("ytfhghgggh|<"+scan2);
                u=scan2.nextInt();
                //System.out.println("ytfhghggg|<"+u);
                if(u==0) break;
                for(int i=0;i<u;i++)
                {//Scanner scan1=new Scanner(System.in);
                    n[i]=scan2.nextLong();
    
                }
                boolean temp[]=new boolean[20];
                long s=sum(n,turn,u);
                long d=s;
                for(;;)
                    if(t(turn ,u))
                    {
                        long s1=sum(n,turn,u);
                        long s2=s-s1;
                        long t=com(s1,s2,d);
                        if(t>=0)
                        {
                            d=t;
                            for(int k=0;k<u;k++)temp[k]=turn[k];
                        }
                    }
                    else
                        break;
                to[tou]=d;
                tou++;
                //System.out.println("ytfhghggg|"+tou);
            }
            //System.out.println("ytfhghggg");
            for(int i=0;i<tou;i++)
                System.out.println(to[i]);
        }
        public static  boolean t(boolean t[],int u)
        {
            for(int i=0;i<u;)
                if(t[i]==false)
                {
                    t[i]=true;
                    if(i>0)
                        for(int j=0;j<i;j++)
                            t[j]=false;
                    return true;
                }
                else i++;
            return false;
        }
    
        public static long sum(long n[],boolean t2[],int u)
        {
            long sum=0;
            for(int i=0;i<u;i++)
                if(t2[i]==false)sum+=n[i];
            return sum;
        }
    
        public static long com(long a,long b,long before)
        {
            long c;
            if(a>b)c=a-b;
            else c=b-a;
            if(c>=before)return -1;
            else return c;
        }
    }
    
    

    C# :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication4
    {
        class Program
        {
            static void Main(string[] args)
            {
                int N;
                long  Num = 10000000000000;
    	        long  sum1,sum2;
                long [] a=new long [100];
    	        int [] flag=new  int [20];
    	        long  Result;
                N = int.Parse(Console.ReadLine());
    	        while(N!=0)
            {
    		      long Min=Num;
    		      int Empty=0;
    		      for(int i=0;i<=N-1;i++)
    		      {
                  a[i] = long.Parse(Console.ReadLine());
    			  flag[i]=0;
    		
    		    }
    	
    	        while(Empty==0)
    	        {
    		     sum1=0;
    		     sum2=0;
    		     for(int i=0;i<=N-1;i++)
    		     {
    			 if(flag[i]==0) sum1=sum1+a[i];
    			 else sum2=sum2+a[i];
    	     	}
                 if (sum1 > sum2) Result = sum1 - sum2;
                 else Result = sum2 - sum1;
                 if (Min > Result) Min = Result;
                 for (int i = 0; i <= N - 1; i++)
    	
    	                 {
    		                if(flag[i]!=0&&i==N-1) Empty=1;
    		                 if(flag[i]==0) break;
    	                  }
    	        for(int i=0;i<=N-1;i++)
    	            {
    		           if(flag[i]==0) {flag[i]=1; break;}
    		           else flag[i]=0;
    	
    	
    	            }
    	          }
                   Console.WriteLine(Min);
                   N = int.Parse(Console.ReadLine());
    	
    	       }
    
            }
        }
    }
    
    • 1

    信息

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