求一简单程序....
好久没有碰C啦..今天一个朋友问一个简单的问题都记不起哒
请各位帮帮忙...小弟感激不尽...
程序要求实现
1
1 2 1
1 2 3 2 1
.............
问题点数:20、回复次数:14Top
1 楼gxnet12(及郁闷)回复于 2005-05-29 19:53:26 得分 0
1
1 2 1
1 2 3 2 1
...................
分不够再给...
在线等...Top
2 楼gxnet12(及郁闷)回复于 2005-05-29 19:54:17 得分 0
晕...最前面那一个1怎么不可以居中啊.
郁闷
--------------------------------------------
1
1 2 1
1 2 3 2 1
.......................Top
3 楼gxnet12(及郁闷)回复于 2005-05-29 20:22:10 得分 0
怎么没有人来回答我啊...
555Top
4 楼foochow(无聊,灌水......)回复于 2005-05-29 20:38:46 得分 2
用队列可以实现~~~-_-Top
5 楼gxnet12(及郁闷)回复于 2005-05-29 20:56:07 得分 0
楼上兄弟可以给出原程序吗.
谢谢...Top
6 楼mydefoliate()回复于 2005-05-29 21:21:26 得分 4
看看是不是这效果:
#include <stdio.h>
void main( )
{
int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=0;j<n-i;j++) printf(" ");
for(j=1;j<=i;j++) printf("%d ",j);
for(j=i-1;j>=1;j--) printf("%d ",j);
printf("\n");
}
}
Top
7 楼foochow(无聊,灌水......)回复于 2005-05-29 21:38:21 得分 4
#include<iostream>
#include<queue>
using namespace std;
int main()
{
queue<int>Q;
Q.push(0);
Q.push(1);
Q.push(0);
int s=0;
int n;
cout<<"enter n:";
cin>>n;
int m=0;
for(int i=1;i<=n;++i)
{
Q.push(0);
for(int j=0;j<=2*i;++j)
{
int t=Q.front();
Q.push(1+t);
Q.pop();
if(t)cout<<t<<" ";
}
Q.push(0);
cout<<endl;
}
return 0;
system("PAUSE");
}
Top
8 楼skyphantom()回复于 2005-05-29 21:38:55 得分 2
#pragma warning(disable : 4786)
#include<iostream>
#include <string>
using namespace std;
void main()
{
char strTem[80];
string strLast, strLast_1, strLast_2;
for( int i = 1; i < 10; i++)
{
itoa( i, strTem, 10);
strLast_1 = strLast_1 + " "+ strTem;
if(i > 1)
{
for( int j = i-1; j >= 1; j--)
{
itoa( j, strTem, 10);
strLast_2= strLast_2 + " "+ strTem;
}
}
strLast = strLast_1 + " " + strLast_2;
strLast_2 = "";
cout << strLast << endl;
}
}Top
9 楼skyphantom()回复于 2005-05-29 21:43:46 得分 2
#pragma warning(disable : 4786)
#include<iostream>
#include <string>
using namespace std;
void main()
{
char strTem[80];
string strLast, strLast_1, strLast_2;
cout << "Input n" << endl;
int n = 0;
cin >> n;
for( int i = 1; i <= n; i++)
{
itoa( i, strTem, 10);
strLast_1 = strLast_1 + " "+ strTem;
if(i > 1)
{
for( int j = i-1; j >= 1; j--)
{
itoa( j, strTem, 10);
strLast_2= strLast_2 + " "+ strTem;
}
}
strLast = strLast_1 + " " + strLast_2;
strLast_2 = "";
cout << strLast << endl;
}
}Top
10 楼gxnet12(及郁闷)回复于 2005-05-30 09:46:34 得分 0
TO: mydefoliate()
你的只可以显示到1 2 1 后面的就出不来啦..而且像你这样的话如果我的数是无限大的话..那不是要写好久的代码....
TO: foochow(恰似你的温柔) skyphantom
你们两位的好像也不能实现我要的效果...
-----------------------------------------------------------------效果如下:
1
1 2 1
后面无法显示.
不管输入的值是多少!Top
11 楼QunKangLi(心里面疼得有点发酸 一定是有雾来了 打湿了我的眼眶)回复于 2005-05-30 11:41:55 得分 4
int abs_int( int x )
{
return x < 0 ? -x : x ;
}
void print( int n )
{
int r, c, t, h, cc ;
static char CH[] = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ;
for( cc = n+n-1, h = cc/2, r = 0 ; r < n ; ++r, putchar('\n') )
for( c = 0 ; t = abs_int(c-h), c < cc ; ++c )
putchar( r < t ? ' ' : CH[r-t] ) ;
}
int main( )
{
print( 40 ) ;
return 0 ;
}
Top
12 楼QunKangLi(心里面疼得有点发酸 一定是有雾来了 打湿了我的眼眶)回复于 2005-05-30 11:48:59 得分 2
数字版:
int abs_int( int x )
{
return x < 0 ? -x : x ;
}
void print( int n )
{
int r, c, t, h, cc ;
for( cc = n+n-1, h = cc/2, r = 0 ; r < n ; ++r, putchar('\n') )
for( c = 0 ; t = abs_int(c-h), c < cc ; ++c )
if( r < t ) printf( " " ) ;
else printf( "%3d", r-t+1 ) ;//格式符%xd中x为上一句中输出的空格数
}
int main( )
{
print( 10 ) ;
return 0 ;
}Top
13 楼gxnet12(及郁闷)回复于 2005-05-30 12:51:12 得分 0
楼上的没人加头文件吧
我加后好像是可以啦
但是不知道怎么停下来..
闪一下就过啦
也看不出到底是不是我要的效果..
Top
14 楼gxnet12(及郁闷)回复于 2005-05-30 13:09:24 得分 0
谢谢楼上的各位...根椐QunKangLi(To iterate is human,to recurse divine) 数字版
我差不多调出来啦...
给我原程序
大家看看:
#include<iostream>
#include <stdio.h>
int abs_int( int x )
{
return x < 0 ? -x : x ;
}
void print( int n )
{
int r, c, t, h, cc ;
for( cc = n+n-1, h = cc/2, r = 0 ; r < n ; ++r, putchar('\n') )
for( c = 0 ; t = abs_int(c-h), c < cc ; ++c )
if( r < t ) printf( " " ) ;
else printf( "%3d", r-t+1 ) ;//格式符%xd中x为上一句中输出的空格数
}
int main( )
{
print( 10 ) ;
getchar();
return 0 ;
}
Top




