c++ - to print the output prescribed -


#include<iostream> #include<math.h> #include<stdlib.h> using namespace std; main() {     int q,a,b,i,number;     cin>>q;     while(q--)     {         cin>>a>>b;         int a[b];         int number=1;         for(i=0;i<b;i++)         {             if(i==0)             {                 a[i]=number;                 number++;             }              a[i]=number;             if(number>0)             number=number*-1;         }     } } /*the above code tried little bit , it's incomplete , may incorrect too, may want print sequence 1,2,-2,3,-3,3,4,-4,4,-4,5,-5,5,-5,5..... till n , n size of array in c++?*/ 

chef's kid, junior chef loves playing different series. chef, impressed son's curiosity, gifts him special series s on birthday s=1,2,-2,3,-3,3,4,-4,4,-4,............. chef, eager check intelligence of son, gives him q queries solve. each query consists of 2 positions , b, , junior_chef required calculate sum of integers b. input

the first line of input contains single integer q, denotes number of queries. next q lines consist of 2 integers, , b. output

print answer in single line. constraints

1<=q<=10 , 1<=a,b<=10^12 sample input

1 1 4 sample output

4 explanation : series 1,2,-2,3, therefore sum 4.

do mean following?

#include <iostream>  int main()  {      while ( true )     {         std::cout << "enter non-negative number (0-exit): ";          int n = 0;         std::cin >> n;          if ( n <= 0 ) break;          ( int = 1; <= n; i++ )         {             ( int j = 0; j < i; j++ ) std::cout << ( j % 2 ? -i : ) << ' ';         }          std::endl( std::cout );     }      return 0; } 

if enter sequentially 1 2 3 4 5 6 7 0 output

enter non-negative number (0-exit): 1 1  enter non-negative number (0-exit): 2 1 2 -2  enter non-negative number (0-exit): 3 1 2 -2 3 -3 3  enter non-negative number (0-exit): 4 1 2 -2 3 -3 3 4 -4 4 -4  enter non-negative number (0-exit): 5 1 2 -2 3 -3 3 4 -4 4 -4 5 -5 5 -5 5  enter non-negative number (0-exit): 6 1 2 -2 3 -3 3 4 -4 4 -4 5 -5 5 -5 5 6 -6 6 -6 6 -6  enter non-negative number (0-exit): 7 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  enter non-negative number (0-exit): 0 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -