c++ - Insertion sort task -
i trying solve problem in should sort increasingly array of numbers, take first k numbers sorted array , eliminate numbers repeating , write them on output.
this code:
#include <iostream> using namespace std; int main() { int n, k; cin >> n >> k; int tab[n]; (int = 0; < n; i++) //taking n numbers input { cin >> tab[i]; } int j, element; (int = 1; < n; i++) //i using insertion sort { j = 0; while (tab[j] < tab[i]) j++; element = tab[i]; for(int k = - 1; k >= j; k--) tab[k + 1] = tab[k]; tab[j] = element; } (int = 0; < k; i++) //writing k smallest numbers without repetitions { if (tab[i] == tab[i + 1]) continue; cout << tab[i] <<"\n"; } cin >> n; return 0; }
generally works , gives expected output, when uploading problem check correctness (i found problem on polish site), says "wrong anwser". cannot see errors here, maybe see wrote bad.
i think misunderstood problem. 'eliminate numbers repeating' means have print number once , eliminate subsequent occurrence(s) of number. ex.
n = 5; k = 3; tab[n] = 5 1 1 1 1
here, sorted tab[] becomes '1 1 1 1 5', expected output '1', program gives nothing!
i hope helps :)
Comments
Post a Comment