使用单链表打印杨辉三角
来源:互联网 发布:反黄软件 编辑:程序博客网 时间:2024/05/10 00:39
你还在申请一个大的数组来存放杨辉三角吗,你out啦;知道队列么,杨辉三角其实是可以用队列来实现的,一个也好,两个也罢,当然你也可以用N个,那就得看你是怎么想的了。
一、使用一个队列打印杨辉三角
二、使用两个队列打印杨辉三角
一、使用一个队列打印杨辉三角
<pre name="code" class="cpp">#include<iostream>#include<queue>using namespace std;void print(int n){ queue<int> yang; int i = 1, j, s = 0; int k =0, t, u; yang.push(i),yang.push(i); cout<<yang.front(); for( i = 1; i < n; i++) { cout<<endl; yang.push(k); for(j = 1; j <= i + 2; j++) { t = yang.front(); yang.pop(); u = s + t; yang.push(u); s = t; if(j != i + 2) cout<<s<<' '; } }}int main(){ for(int i = 1; i < 8; i++) {cout<<"Case:"<<i<<endl;print(i);cout<<endl;} return 0;}
二、使用两个队列打印杨辉三角
#include<iostream>#include<queue>using namespace std;void print(int n){ queue<int> yang1; queue<int> yang2; int change = 1; yang1.push(1); cout<<yang1.front(); for(int i = 1; i < n; i++) { cout<<endl; int temp = 0; if(change) { yang1.push(1); while(!yang1.empty()) { cout<<yang1.front()<<' '; yang2.push(temp+yang1.front()); temp = yang1.front(); yang1.pop(); } } else { yang2.push(1); while(!yang2.empty()) { cout<<yang2.front()<<' '; yang1.push(temp+yang2.front()); temp = yang2.front(); yang2.pop(); } } change = !change; }}int main(){ for(int i = 1; i < 8; i++) {cout<<"Case:"<<i<<endl;print(i);cout<<endl;} return 0;}
三、N个队列?
#include<iostream>using namespace std;int main(){ cout<<"233333333..."<<endl; return 0;}
0 0
- 使用单链表打印杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 打印-杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 打印杨辉三角
- 杨辉三角打印
- 打印杨辉三角
- 杨辉三角打印
- 打印杨辉三角
- Get请求和POST请求的区别
- Eclipse常用快捷键
- 堆的动态操作(插入、删除、调整)
- Bye POJ
- [leetcode]40 Remove Nth Node From End of List
- 使用单链表打印杨辉三角
- java——对象的引用级别
- 第一章
- 如何使用Visual Studio 2008打包发布开发好的应用程序
- android开发 手机分辨率
- TCP/IP核心协议之传输层协议
- A modular InnoSetup script to download and install all available .NET Framework versions and much mo
- listener.ora 配置说明
- 序列化(Serializable)