+ 3

Can someone explain me this code please?

#include <vector> #include <iostream> using namespace std; int main(void) { vector<int> n = { 1,3,12,31,499393,1738391 }, v; for (size_t i = 1; i <= 2000; i++) { int n = i; while (n-- > 0) v.push_back(i); } for(size_t i = 0; i < n.size(); i++) cout << n[i] << " => " << v[n[i]-1] << endl; }

6th Jan 2020, 5:44 PM
Hichem GOUIA
Hichem GOUIA - avatar
11 Answers
+ 3
after execution of the first loop we got our vector v as v = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, ...} now to find the triangle no for 31 we just need to do: answer: v[31-1] = 8.
7th Jan 2020, 9:26 AM
minirkk
minirkk - avatar
+ 6
~ swim ~ is truely a gems 💎 for SoloLearn. such a good answerer.
7th Jan 2020, 9:29 AM
minirkk
minirkk - avatar
+ 4
Hichem GOUIA thanks alot friend. And happy learning!😄
7th Jan 2020, 9:30 AM
minirkk
minirkk - avatar
+ 3
~ swim ~ oh yes I get it 👍 big thanks friend RKK yes I take a random java or c++ code and I try to understand it, so this time it was your code 😜 Great work
7th Jan 2020, 9:27 AM
Hichem GOUIA
Hichem GOUIA - avatar
9th Jan 2020, 7:31 AM
ê§àŒ’â˜ŹBad☏Boyâ˜ŹàŒ’ê§‚
ê§àŒ’â˜ŹBad☏Boyâ˜ŹàŒ’ê§‚ - avatar
+ 1
Thanks for your explanation Can you take the example of 31 and how it gives 8 as a result? Thanks
7th Jan 2020, 8:48 AM
Hichem GOUIA
Hichem GOUIA - avatar
+ 1
Great explanation
8th Jan 2020, 8:26 AM
Jalloh Abubakarr
Jalloh Abubakarr - avatar