c++中的Floyd模式
Floyd pattern in c++
嗨,我试着在c++中打印以下模式
1
2 3
4 5 6
7 8 9 10
7 8 9 10
4 5 6
2 3
1
使用下面的循环打印它的一半。
int i,j,k=1;
cout<<"Enter row";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<k<<"t";
k++;
}
}
我得到了像
这样的输出1
2 3
4 5 6
7 8 9 10
如何打印余额输出。但是我怎样才能打印出这个图案的镜像呢?
首先,上面的代码是错误的,它应该是:
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<k<< " ";
k++;
}
cout << 'n';
}
现在,要绘制图案,可以使用一个循环,它将使用以下逻辑:
for(;i > 0; i--)
{
k -= i-1;
for(j=1;j<i;j++)
{
cout <<k<< " ";
k++;
}
cout << 'n';
k -= i-1;
}
int main()
{
int n;
int i, j, k = 1;
cout << "Enter row";
cin >> n;
int elemcount = 0;
for (i = 1; i <= n; i++)
{
elemcount = 0;
for (j = 1; j <= i; j++)
{
cout << k << "t";
k++;
}
cout <<endl;
}
k = k - n; //Reset Counter to the value of the first digit in current row.
i--;
j--;
for (; i > 0; i--)
{
j = i;
elemcount = 0; //Counter to keep track of elements printed.
for (; j> 0; j--)
{
cout << k << "t";
k++;
elemcount++;
}
k = k - elemcount - (i-1); //Resetting K, substracting the number of elements printed and number of elements to be printed in next row.
cout << endl;
}
return 0;
}
相关文章:
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 为什么在保护模式下继承升级不起作用
- 如何在全屏模式下(在OpenGL中)使背景透明
- 为什么使用__LINE_的代码在发布模式下在MSVC下编译,而不是在调试模式下
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 此模式的C++RegEx
- avrogencpp能为模式中的每种类型生成单独的头文件吗
- 使用可变模板的Broadcaster/Listener模式
- c++方法参数只能在linux的发布模式下自行更改
- 资源管理设计模式
- 使用 mod_gsoap 部署服务时,如何在 Gsoap 中更改 soap 上下文的模式?
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 小字符串优化(调试与发布模式)
- 可视化C++:发布模式的运行时库作为'Multi-threaded Debug DLL'
- 如何设计具有不同类型的通知和观察器的观察者模式?
- 在C++的一系列数字中查找重复模式
- 是否允许使用带有"w+"模式的 freopen 进行标准设置?
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- С++ wxWidgets:代码架构,设计原则和模式
- c++中的Floyd模式