The topics are described as follows:
To see this topic, the most easily thought of is the violent search method. However, that is not a good way, it is also ACM can not pass
Way to live.
Thought analysis: For a given array, we must first order it small to large. If the minimum number is not
1 (greater than 1), the result of the output must be 1. If it is 1, through the current number and the preceding all the number and plus 1 in
If the current number is less than or equal to the sum of 1 of the preceding number, then the current number is also a few factors that can be found
Otherwise, directly returns the and plus 1 of the preceding number. There are two conditions for jumping out of a loop, break and loop termination conditions, such as
The result is a termination condition, that is, the 1 to all elements in the array and, can find its factor,
This returns the plus 1 for all numbers.
"Source Code"
#include <stdio.h> #include <malloc.h>void sort (int arr[], int num) {int i = 0;int j = 0;int tmp = 0;for (i = 1; i < num; i++) {int m = arr[i];for (j = i-1; (J >= 0) && (Arr[j] > m); j--) {arr[j + 1] = arr[j];} Arr[j + 1] = m;}} int fun (int arr[], int num) {int i = 0;int r = 0, ans = -1;/*if (arr[0]! = 1) return 1;*/for (int i = 0;i< num; i++) { int tl = Arr[i], tr = r + arr[i];if (TL <= r + 1) {r = TR;} Else{ans = R + 1;break;}} if (ans = =-1) ans = r + 1;return ans;} int main () {int *arr = Null;int num = 0;int i = 0;scanf ("%d", &num), if (num <= 0) {printf ("input error"); return 0;} ARR = (int *) malloc (num * sizeof (int)); if (NULL = = arr) {printf ("Out of memory\n"); return 0;} for (i = 0; i < num; i++) {scanf ("%d", &arr[i]);} Sort (arr,num); int ret = fun (arr, num);p rintf ("%d", ret); return 0;}
Through the code, we find that if the smallest element of the array is not 1, it can still be judged by the For loop, not
You need the front-most if judgment in the fun function.
Make the best of yourself ~ ~ Come on
The small algorithm---find the smallest number that cannot be selected by N number of sums