Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在上面的行中添加数字?_C++_Arrays_Pointers - Fatal编程技术网

C++ 如何在上面的行中添加数字?

C++ 如何在上面的行中添加数字?,c++,arrays,pointers,C++,Arrays,Pointers,我正在用c++解决这个模式: 一, 27 368 45910 我有两个逻辑来解决这个模式,但我被卡住了: 1使用循环的逻辑: using namespace std; #include <iostream> int main() { bool flag=true; for(int i=1;i<10;i++) { if(i<=4) //when 4 column then if print the 5 //it is exec

我正在用c++解决这个模式:

  • 一,
  • 27
  • 368
  • 45910
我有两个逻辑来解决这个模式,但我被卡住了:

1使用循环的逻辑

using namespace std;
#include <iostream>

int main()
{
    bool flag=true;
    for(int i=1;i<10;i++)
    {
         if(i<=4) //when 4 column then if print the 5 //it is execute perfactly
         {   
           flag=false;
           cout<<endl;
           cout<<i;

           for(int column=4;column<=i;column++)
           {
               flag=true;
               column=column+1;       
               cout<<column;
           }
         }

         //here now i want to add a 6 in 3 column but not proper output
//       if(i<=3)
//       {   
//         flag=false;
//         cout<<endl;
//         cout<<i;

//         for(int column=3;column<=i;column++)
//         {
//             flag=true;
//             column=column+1;       
//             cout<<column;
//         }
//       }
    }
}       

使用名称空间std;
#包括
int main()
{
int*列_数组[4];
int-mapping_数组[4]={1,2,3,4};//4列
int i;//行
int j;//列
对于(i=0;i<4;i++)
{
column_array[i]=malloc(sizeof(int)*column_array[i]);//这里我面临错误
}
}    
错误是: 错误:“long unsigned int”和“int*”类型的操作数对二进制“operator*”无效

我希望我能用正确的方式在上面的程序上应用逻辑


解决此模式的方法是什么?

尝试用数学方法解决它,找出哪个数字将位于位置
(i,j)
,其中
i
-行号,
j
-列号

然后金字塔看起来像

(0, 0)
(1, 0) (1, 1)
(2, 0) (2, 1) (2, 2)

提示:假设您需要输出
r
行,那么第一列将有
r
元素,第二列将有
r-1
元素,依此类推。

如果我是您,我将从如下所示的数组开始:

1 2 3 4
7 6 5
8 9
10
可以通过默认值或使用循环填充数组:

int-ar[4][4];
void生成()
{
int num=1;
布尔标志=真;
用于(int行=0;行<4;行++)
{
国际单项体育联合会(旗)
{
for(int colm=0;colm<4行;colm++)
{
ar[row][colm]=num;
num++;
}
flag=!flag;
} 
其他的
{
对于(int colm=4-(行+1);colm>=0;colm--)
{
ar[row][colm]=num;
num++;
}
flag=!flag;
}
}
}
现在,当您拥有阵列时,您可以按所需顺序打印它们:

void printInOrder(){
用于(int行=0;行<4;行++)
{

对于(int colm=0,mv=0;colm)你能解释得更详细些吗?plz
(3-0)=3,(3-1)=2,(3-2)=1,(3-3)=0
我不太清楚这些数字是从哪里来的?不是(3-0),而是(3,0)-坐标。这个坐标中的值将取决于你有多少行。根据你的逻辑第二行
(4-2)=2列
第三行(4-3)=1列
第四行(4-4)=0列
是错的还是对的?
(1,1)
的列是什么?现在我明白了
(1,1)=1,(2,1)=2和(2,2)=7,(3,1)=3和(3,2)=6和(3,3)=8,(4,1)=4和(4,2)=5和(4,3)=9和(4,4)=10
当我在尝试你的程序时遇到了这个问题,看到这个链接:你能帮我更多吗?我不能调试你的程序。你能告诉我什么是
mv=0
哪种类型的变量吗?我看不到nodeclare anywhere
mv
是一种
int
类型的变量,在循环定义中声明,
for(int…)
感谢您花费宝贵的时间和精力来解决此逻辑问题
(0, 0)
(1, 0) (1, 1)
(2, 0) (2, 1) (2, 2)
1 2 3 4
7 6 5
8 9
10
1 2 3 4
7 6 5
8 9
10

1
2 7
3 6 8
4 5 9 10