#include<stdio.h> #include<iostream> using namespace std; int main() { setlocale(LC_ALL, ""); int a[] = { 1, 2, 3, 4, 5, -1, -2, -3, -4, -5, 7, 7, 7, 9, 10, -6, -7, -8, -9, -10 }, i, j,k; int(*b)[5]; b = (int(*)[5])a; for (i = 0; i < 4; i++) { for (j = 0; j < 5; j++) cout << b[i][j] << " "; cout << "\n"; } system("pause"); } - 2Look at the map . - avp pm
- @Xeller, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina
|
3 answers
All answers are bad, except for the advice given by avp. I want to clarify only that if the task is taken literally, i.e. it is necessary to determine only the fact of the presence of repeating elements, then you can still distort yourself like this:
#include <stdio.h> #include <stdlib.h> int main(void) { int array[] = { 1, 2, 3, 4, 5, -1, -2, -3, -4, -5, 7, 7, 7, 9, 10, -6, -7, -8, -9, -10 }; int repetitions = 0; int compare(const void* pa, const void* pb) { int a = *(int*)pa; int b = *(int*)pb; if (a == b) { repetitions = 1; return 0; } return a < b ? -1 : 1; } qsort(array, sizeof(array) / sizeof(int), sizeof(int), compare); puts(repetitions ? "Yes!" : "No."); return 0; } - I'm afraid your example is not working ideone.com/05Lyzr - inceon
- oneSlyly invented. If you really pervert, then you can look at this modification: main () {... sigjmp_buf jmp; void L = && L1; ... if (sigsetjmp (jmp, 0)) goto * L; int compare (const void pa, const void * pb) {int a = (int ) pa; int b = (int ) pb; if (a == b) {repetitions = 1; siglongjmp (jmp); } return a <b? -eleven; } qsort (...); L1 :; puts (...); ... Too bad, a non-local goto (by void * L) does not allow to correctly exit functions, otherwise it could be ... - avp
- @inceon, nested functions (and label addresses) is an extension to gcc. This is C, not C ++. It all works. - avp
- In order to stop sorting on time, I would just write its code. For example, dwarf sorting is so laconic that it will turn out to be almost shorter than calling standard qsort with this your longjump. - VadimTukaev
- However, there is a difference in the speed of work (IMHO for hundreds of millions of numbers the difference will be measured in hours). - avp
|
If we mean the same elements in the array, then
for(i=0;i<20;i++){ for(j=i+1;j<20;j++){ if(a[i]==a[j]){ cout << "Identical elements: " << i << " and " << j << endl; } } } Or
#include <algorithm> ... for(i=0;i<20;i++){ if(count(a,a+20,a[i])>1){ cout << "There identical " << a[i] << endl; break; } } |
#include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; int main(){ int a[] = { 1, 2, 3, 4, 5, -1, -2, -3, -4, -5, 7, 7, 7, 9, 10, -6, -7, -8, -9, -10 }; vector<int> v(a, a + 20); // Π·Π°ΠΊΠΈΠ½ΡΠ»ΠΈ ΠΌΠ°ΡΡΠΈΠ² Π² Π²Π΅ΠΊΡΠΎΡ set<int> s; // ΡΡΠ΄Π° ΠΌΡ Π±ΡΠ΄Π΅ΠΌ Π»ΠΎΠΆΠΈΡΡ ΠΏΠΎΠ²ΡΠΎΡΡΡΡΠΈΠ΅ ΡΠΈΡΠ»Π° Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ ΠΈ Ρ
ΡΠ°Π½ΠΈΡΡ ΠΈΡ
Π² Π΅Π΄ΠΈΠ½ΡΡΠ²Π΅Π½Π½ΠΎΠΌ ΡΠΈΡΠ»Π΅ (ΠΈΡΠΊΠΎΠΌΡΠΉ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ) for (vector<int>::iterator it = v.begin(); it != v.end(); it++){ // Π±Π΅ΡΠ΅ΠΌ ΡΠΈΡΠ»ΠΎ ΠΈΠ· ΠΌΠ°ΡΡΠΈΠ²Π° if(count(v.begin(), v.end(), *it) > 1){ // Π΅ΡΠ»ΠΈ Π΅ΡΡΡ Π΄ΡΠ±Π»ΠΈΡΡΡΡΠ΅Π΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ cout << "finded: " << *it << endl; // ΠΎΡΠ»Π°Π΄ΠΊΠ° s.insert(*it); // Π»ΠΎΠΆΠΈΠΌ Π΅Π³ΠΎ Π² Π½Π΅ΠΏΠΎΠ²ΡΠΎΡΡΡΡΠΈΠΉΡΡ ΠΊΠΎΠ½ΡΠ΅ΠΉΠ½Π΅Ρ ΡΠΈΠΏΠ° set continue; } } for (set<int>::iterator it = s.begin(); it != s.end(); it++){ // Π²ΡΠ²ΠΎΠ΄ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ° cout << "Look for duplicate value: -> " << *it << " <- found: " << count(v.begin(), v.end(), *it) << endl; } return 0; } 
|