C + + to find only one occurrence of two numbers in the array and save (ox-Guest sword refers to offer)

Source: Internet
Author: User
In an array of integers, except for two digits, the other numbers appear two times. Please write the program to find the two only occurrences of the number. Experience: At first I thought it was only one occurrence of a number, the problem is that there are 2 only one occurrence of the number//array from i=0, start, another j=i+1 start, when there is an equal I will save it in my newly created array b[n/2+1],//when J==n, Indicates that there is no value equal to a[i after position I, but there is no guarantee that there is no front,//So here I saved the data in the B array to work, and then iterate through the B array, if the B array still does not find the same//number, We get this one-time-only number. You can then save it and then ask for the next one.//the sword means the offer is perfectly passed. #include <iostream>using namespace std;void grial (int a[],int n {int B[n/2+1];int i = 0;int k = 0; for (; i<n;i++) {int J, for (j = i+1; j<n;j++) {if (A[i]==a[j]) {b[k++]=a[i];break;}} int m = 0; if (j>=n) {for (; m<k;m++) {if (a[i]==b[m]) break;}} if (m>=k && j>=n) {cout<<a[i]<<endl;//a[i] is stored in *num1,*num2 is the result of the offer in the Sword.}} int main () {int a[]={1,1,2,3,4,4}; Grial (a,6); return 0;}

C + + to find only one occurrence of two numbers in the array and save (ox-Guest sword refers to offer)

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.