1 条题解

  • 0
    @ 2021-6-15 13:05:33

    C :

    #include <stdio.h>
    int main()
    {
    	int N;
    	int a[100],i,j,k;
    	int thisSum=0,maxSum=0;
    	while(scanf("%d",&N)!=EOF&&N>=1&&N<=100)
    	{
           for(i=0;i<N;i++)
           {
       	     scanf("%d",&a[i]);
           }
           maxSum=a[0];
           for ( i = 0; i<N; i++ )  
           {
               for (j = 1; j<N; j++ )   
               {  
                   thisSum = 0;  
                     for ( k = i; k <= j; k++ )  
                     {
                       thisSum+= a[k];  
                     }
                      if ( thisSum > maxSum )  
                      {
                        maxSum=thisSum;  
                    }
               }  
           }
              printf("%d\n",maxSum);
       }
       return 0;
    }
    

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
    	int n;
    	while(~scanf("%d",&n)){
    		int a[105];
    		for (int i=0;i<n;i++){
    			scanf("%d",&a[i]);
    		}
    		
    		int d[105];
    		memset(d,0,sizeof(d));
    		
    		if (a[0]>=0) d[0]=a[0];
    		
    		int maxx=d[0];
    		for (int i=1;i<n;i++){
    			if (d[i-1]+a[i]>0) d[i]=d[i-1]+a[i];
    			maxx=max(maxx,d[i]);
    		}
    		
    		printf("%d\n",maxx);
    		
    	}
    	return 0;
    }
    

    Java :

    import java.util.Scanner;
    public class Main {
    	public static void main(String[] args) {
    		int N;
    		int i,j,k;
    		int[] a = new int[100];
    		int thisSum=0,maxSum=0;
    		Scanner scan = new Scanner(System.in);
    		while (scan.hasNextInt()) {
    			 N = scan.nextInt();
    			 for(i=0;i<N;i++)
    		     {
    			   a[i] = scan.nextInt();
    		     }
    			 maxSum=a[0];
    			 for ( i = 0; i<N; i++ ){
    				 for (j = 1; j<N; j++ ){
    					 thisSum = 0; 
    					 for ( k = i; k <= j; k++ ) {
    	                   thisSum+= a[k];  
    	                 }
    	                 if ( thisSum > maxSum ){
    	                    maxSum=thisSum;  
    	                 } 
    				 } 
    			 }
    			 System.out.println(maxSum);
    	    }
       }
    }
    
    • 1

    信息

    ID
    828
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    1
    已通过
    0
    上传者