Array Polygon Questions

Source: Internet
Author: User

Array Polygon Questions

#include <iostream>using namespace std;//array summation int sum (int *a, int n) {return n = = 0? 0:sum (A, n-1) + a[n-1];} Recursive array summation int sum2 (int *a, int n) {int s = 0;for (int i = n-1; i > 0; i--) {s + = A[i];} return s;} Find the maximum minimum value of the array void max_min (int *a, int n, int &max_value, int &min_value) {max_value = A[0];min_value = a[0];for (int i = 0; I < n; i++) {if (A[i] > Max_value) max_value = A[i];if (A[i] < Min_value) Min_value = A[i];}} element int Halftime_val (int *a, int n) with more than half occurrences in the array {typedef struct VALUETYPE{INT num;int time;} Value;int Target_val = -1;value* data = new Value[n];for (int i = 0; i < n; i++)//statistics array elements occurrences {data[i].num = A[i];d ata[ I].time = 1;for (int j = 0; J < i; J + +) {if (Data[i].num = = Data[j].num) data[i].time++;}} for (int i = 0; i < n; i++) {if (Data[i].time > N/2) {target_val = Data[i].num;}} Delete Data;return Target_val;} Find the element in the array with more than half occurrences int find (int* a, int n) {int curvalue = A[0];int count = 1;for (int i = 1; i < n; ++i) {if (a[i] = = CU RValue) COUnt++;else{count--;if (Count < 0) {Curvalue = A[i];count = 1;}}} return curvalue;} Find the smallest two points in the array//given an integer array of n elements, finding out the two elements in the array x and y make the ABS (x-y) value min//First sort, then iterate int compare (const void* A, const void* b) { Return (* (int*) A-* (int*) b);} void min_distance (int *a, int n) {qsort (A, n, sizeof (int), compare); int min_distace = 0;int temp = 0, x1 = 0, x2 = 0;min_di Stace = a[1]-a[0];for (int i = 1; i < n-1; i++) {temp = ABS (A[i + 1]-a[i]); if (temp < min_distace) {Min_distace = Temp;x1 = a[i + 1];x2 = A[i];}} cout << min_distace << endl;cout << x1 << "<< x2 << Endl;} Find the common elements of two ordered arrays */* Given two ordered (non-descending) integer arrays of n elements A and B, find common elements such as a = 0, 1, 2, 3, 4b = 1, 3, 5, 7, 9 output 1, 3***************************** Analysis: Make full use of the order of the array of properties, with two pointers I and J respectively to A and B, compare A[i] and b[j], according to the comparison of the results of moving pointers, there are the following three cases 1. A[i] < B[j], then I increased by 1 and continued to compare 2. A[i] = = B[j], then I and J both add 1, continue to compare 3. A[i] < B[j], then J plus 1, continue to compare the above process until I or J reaches the end of the array. */void find_commom1 (int *a, int *b, int n) {int i = 0, j = 0;while (i < n && J < N) {if (A[i] < b[j]) i++;if (a[i] = = B[j]) {cout << a[i] << ""; i++;j++;} if (A[i] > B[j]) j + +;} cout << Endl;} void Find_commom (int* A, int *b, int n) {int I, j;for (i = 0; i < n; i++) {for (j = 0; J < N; j + +) {if (a[i] = = B[j]) CO UT << a[i] << "";}} cout << Endl;} int main () {int a[4] = {2, 4, 6, 8};int b[5] = {1, 5, 7, 9}; Find_commom (A, B, 4); Find_commom1 (A, B, 4);}

From: http://www.cnblogs.com/graphics/archive/2010/08/24/1761620.html#

Array Polygon Questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.