here the round up of 2.5 is 3 so the average of 1 to n will be 2 only. Here we can start from n to 1 and take average of current and average of previous operation So here we used stack to make it easy We pop last two elements and push the average into same stack contains only one element
| 6 | //So here we used stack to make it easy |
| 7 | //We pop last two elements and push the average into same stack contains only one element |
| 8 | int main() |
| 9 | { |
| 10 | ios_base::sync_with_stdio(0); |
| 11 | cin.tie(0); cout.tie(0); |
| 12 | int t; |
| 13 | cin>>t; |
| 14 | while(t--){ |
| 15 | int n; |
| 16 | cin>>n; |
| 17 | stack<int>arr; |
| 18 | for(int i=0;i<n;i++){ |
| 19 | arr.push(i+1); |
| 20 | } |
| 21 | cout<<2<<endl; |
| 22 | while(arr.size()>1){ |
| 23 | int a = arr.top(); |
| 24 | arr.pop(); |
| 25 | int b = arr.top(); |
| 26 | arr.pop(); |
| 27 | cout<<a<<" "<<b<<endl; |
| 28 | //round is eg. 2.5 is 3 |
| 29 | arr.push(ceil((float)(a+b)/2)); |
| 30 | } |
| 31 | } |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 |
nothing calls this directly
no outgoing calls
no test coverage detected