以打印规定的输出

to print the output prescribed

本文关键字:输出 打印      更新时间:2023-10-16
#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 i tried a little bit , but it's incomplete and may be incorrect too, you may help
i want to print the sequence as 1,2,-2,3,-3,3,4,-4,4,-4,5,-5,5,-5,5..... till n , where n is size of array in c++?*/
厨师

的孩子,初级厨师喜欢玩不同的系列。厨师被儿子的好奇心所打动,在他生日那天送给他一个特别系列的S。S=1,2,-2,3,-3,3,4,-4,4,-4,.............现在厨师急于检查儿子的智商,给他q查询解决。每个查询由两个位置 a 和 b 组成,需要 Junior_chef 来计算从 a 到 b 的所有整数的总和。输入

输入的第一行包含一个整数 q,表示查询数。接下来的 q 行由两个整数组成,a 和 b。输出

在一行中打印答案。约束

1<=q<=10 , 1<=a,b<=10^12示例输入

11 4示例输出

四说明:由于序列是 1,2,-2,3,因此总和将是 4。

你的意思是以下内容吗?

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

如果按顺序输入 1 2 3 4 5 6 7 0,则输出将是

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