+ 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; }
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.
+ 6
~ swim ~ is truely a gems đ for SoloLearn. such a good answerer.
+ 4
Hichem GOUIA thanks alot friend.
And happy learning!đ
+ 1
Thanks for your explanation
Can you take the example of 31 and how it gives 8 as a result?
Thanks
+ 1
Great explanation