[C language] Getting hot issue

Title (1):

As we all know, living with Momo pot. In the E8 dinner activities, he often sold Meng sell innocent led everyone to eat hot pot. .
One day, I heard Momo Xiabu sipping a nearby school promotions at certain current charge of sponsorship, just 30 soft sister coins for each guest, which at N dish, but guests can only pick Some dishes continuous on.
So he was very excited and pulled the gray slag slag eat hot pot goes.
Momo is a very picky person, so he has every dish a pleasant degree (of course, because he's picky eaters, some of the pleasant things would be negative).
In order to be able to Momo enjoyed the dinner very pleasant, empathetic and gray slag slag decided to help him calculate, how they should best selection menu to make Momo happy to eat the dinner.
Input format:
The first line is an integer T, (T <= 10) represents the number of test cases
for each test case, the first line is an integer N, (1 <= N < = 10000) represented by the dishes the number of
the next N digits, si denotes the i-th digit of the i Momo pleasant dish. (-1000 <= Si <= 1000)
the PS: Since CF child blood out again rating, so the degree of pleasure starting Momo is 0
the PPS: Momo entirely possible to give pleasure to a negative value, poor Momo. .
Output format:
For each sample, a digital output, represents the maximum value of the happy Momo after eating.
HINT:
For 5
6 -1 -7 4 5
We chose 6, -1, 5, 4 four-course (note must be continuous, it is not Skip -1)

analysis:

To achieve the title of the "continuous period of input and maximum value." So our idea may be: added to the second number from the first number, the third number is added to the last n-number ......, and then added to the third number from the second number, the fourth number ...... number of n, and where to find the maximum.

The idea of ​​the results achieved:

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int a[10001]={0};
 5     int n,x,i,j,k;
 6     int count=0;
 7     scanf("%d",&n);
 8     int sum=0;
 9     
10     while(count<n) 
11     {
12         int max=-1001;
13         scanf("%d",&x);
14         for(i=1;i<=x;i++)
15         {
16             scanf("%d",&a[i]);
17         }
18         for(i=1;i<=x;i++)
19         {
20             for(j=i;j<=x;j++)
21             {
22                 for(k=i;k<=j;k++)
23                 {
24                     sum+=a[k];
25                 }
26                 if(sum>max)
27                 {
28                     max=sum;
29                 }
30                 sum=0;
31             }
32         }
33         printf("%d\n",max);
34         count++;
35     }
36 }

Of course, speed is not fast it seems to this question this method, and the method can not have an array, as follows:

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int x,n,t,s,m,max=-1001;
 5     scanf("%d",&t);
 6     while(t--)
 7     {
 8         scanf("%d",&n);
 9         s=m=0;
10         while(n--)
11         {
12             scanf("%d",&x);
13             if(max<x) max=x;
14             if(m+x>0) m=m+x;
15             else m=0;
16             if(s<m) s=m;
17         }
18         if(max<0) printf("%d\n",max);
19         else printf("%d\n",s);
20     }
21 }

The idea is roughly:

Two sets of intermediate record variables: s, m;

s: sum of the maximum recording has occurred;

m: maximum value sum of a plus input has occurred,

If the result is less than zero, then m = 0 (all operations before abandoned);

If greater than zero, it is determined that the magnitude relationship s (value obtained with an input original is larger or smaller than the original ratio).

 

The first method also has some universal value, for example, be used to calculate the equivalent number of columns problem.

 

Topic (2):

If the number of columns in a certain period (at least two elements) values of each element are the same, the number of columns is called the equivalent segments.
Equivalent segment in the number of columns the number of elements is called a segment length worth of the number of columns.
Input: integer of N elements of the columns of A (where N <= 50)
Output: Whole Story position A the maximum length equivalent series sections, if not equivalent series segments, the output No equal number list.
Description: Whole Story is the group index subscript position, i.e., 0 represents the first element.
If there are a plurality of equivalent number of columns of the same length, only the first output start position equivalent sequence.
When there are two equally long continuous string in a LIST, we should be the first answer a string of equal length.

 

Analysis: This is a basic idea,

The array a []: recording data;

Array startPosition []: a recording start position equivalent number of columns;

Array count []: recording a digital equivalent of the number of columns is equal to a few;

Eventually finds count [] the maximum number of equivalent set of columns.

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int a[50]={0};
 5     int startPosition[50]={0};
 6     int count[50]={0};
 7     int N;
 8     scanf("%d",&N);
 9     
10     int i=0;
11     int j=0;
12     for(i=0;i<N;i++)
13     {
14         scanf("%d",&a[i]);
15     }
16     
17     for(i=0;i<N;i++)
18     {
19         if(a[i]==a[i+1]&&count[j]>0)
20         {
21             count[j]++;
22         }
23         else if(a[i]==a[i+1]&&count[j]==0)
24         {
25             startPosition[j]=i;
26             count[j]++;
27         }
28         else
29         {
30             j++;
31         }
32     }
33     
34     int max=0;
35     int max_p=0;
36     for(j=0;j<N;j++)
37     {
38         if(count[j]>max)
39         {
40             max=count[j];
41             max_p=startPosition[j];
42         }
43     }
44     if(max>0)
45     {
46         printf("%d %d\n",max_p,max_p+count[max_p]);
47     }
48     else
49     {
50         printf("No equal number list.\n");
51     }
52     
53 }

We can also practice along the lines of hot issues:

 1 #include<stdio.h>
 2 int main()
 3 {    
 4     int N;
 5     scanf("%d",&N);
 6     int startPosition=0;
 7     int count=0;
 8     int i,j,k;
 9     int a[50]={0}; 
10     int sum=0;
11 
12     for(i=0;i<N;i++)
13         {
14             scanf("%d",&a[i]);
15         }
16         for(i=0;i<N;i++)
17         {
18             for(j=i;j<N;j++)
19             {
20                 for(k=i;k<=j;k++)
21                 {
22                     sum+=a[k];
23                 }
24                 if(sum==(j-i+1)*a[k])
25                 {
26                     if(j-i+1>count)
27                     {
28                         startPosition=i;
29                         count=j-i+1;
30                     }
31                 }
32                 sum=0;
33             }
34         }
35     if(count==0)
36     {
37         printf("No equal number list.\n");
38     }
39     else
40     {
41         printf("%d %d\n",startPosition,startPosition+count);
42     }
43 }

 

Guess you like

Origin www.cnblogs.com/fighterkaka22/p/11800074.html