注册 登录
编程论坛 C++教室

C++初学的一道经典例题,请教更简便方法。

wylog 发布于 2008-08-07 11:28, 2262 次点击
编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,以此类推,每一个包含的字符数等于用户指定的行数。在型号不够的情况下,在星号前面加上句点。该程序的运行情况如下:
例如:输入的行数为5

....*
...**
..***
.****
*****


下面是小弟编写的程序,请教达人有没有更简便的算法:

int main()
{
    int n;
    cout<<"enter the row you want to cout:"<<endl;
    cin>>n;

for (int b=1;b<=n;++b)
{

    for (int i=n-b;i>0;i--)
    {
        cout<<".";
        
    };
    for (int x=b;x>0;x--)
    {cout<<"*";
    };
    cout<<endl;
};
return 0;
}
11 回复
#2
zerocn2008-08-07 17:18
#include <iostream>
#include <stdlib.h>

using namespace std;


int main()
{
  int n;
    cout<<"enter the row you want to cout:"<<endl;
    cin>>n;
  int n2=n;
  for(;n>0;n--)
  {
    for(int i=0;i<n2;i++)
    {if(i<n-1)
     cout<<'.';
     else cout<<'*';
    }
    cout<<endl;
  }
  system("pause");
}
#3
xzx10020022008-08-07 19:57
可以减掉一层循环。这是个矩阵,对角线的元素的行数和列数之和都为维数。下对角的元素的行数和列数之和大于维数。

for(int i= 1;i<=n*n;i++)

    a=(i/n+1);//行数
    b=i%n;//列数
    if(a+b>=n)
    {
      cout<<'*';
    }else{
      cout<<'.';
    }
    if(i%n==0)
      cout<<endl;


没上机调试过,仅供参考。
#4
TYFY2008-08-10 14:58
想法很好呀。。。
#5
leeco2008-08-11 01:10
如果有一个足够长的前面是'.'后面是'*'
例如:"....................********************"
那么对于任意的n,输出的每一行都是这个串的一部分(长度为n)
#6
gloomyboy2008-08-11 12:44
[bo][un]xzx1002002[/un] 在 2008-8-7 19:57 的发言:[/bo]

可以减掉一层循环。这是个矩阵,对角线的元素的行数和列数之和都为维数。下对角的元素的行数和列数之和大于维数。

for(int i= 1;i=n)
    {
      cout

这样是不可以的,到每行最后一个元素的时候,行数和列数就不对了,可以完善之
#7
yang_net2008-08-11 17:03
// sample.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<time.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    
    int lineNumber;
    cout<<"line number _>";
    cin>>lineNumber;
    time_t oldTime,newTime;
    time(&oldTime);
    char* outChars=new char[lineNumber+1];
    memset(outChars,'.',lineNumber-1);
    outChars[lineNumber-1]='*';
    outChars[lineNumber]='\0';
    while(lineNumber>=1)
    {
        outChars[lineNumber-1]='*';
        cout<<outChars<<endl;
        lineNumber--;
    }
    time(&newTime);
    cout<<oldTime<<endl;
    cout<<newTime<<endl;
    cout<<"Time pay:"<<newTime-oldTime<<endl;
    return 0;
}

ps:对于本题,因为相邻两行之间只用改变一个字符,所以直接用数组保存,改变特定位置上的一个字符就可以了。这样便将问题的时间复杂度降到了O(n)的范围。。^_^

[[it] 本帖最后由 yang_net 于 2008-8-11 17:10 编辑 [/it]]
#8
gloomyboy2008-08-11 17:47
"对于本题,因为相邻两行之间只用改变一个字符,所以直接用数组保存,改变特定位置上的一个字符就可以了。这样便将问题的时间复杂度降到了O(n)的范围"
这句话,我没有能明白,难道你的意思是用输组直接输出,望点明梦中人!
#9
gloomyboy2008-08-11 17:53
[bo][un]yang_net[/un] 在 2008-8-11 17:03 的发言:[/bo]

// sample.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include
#include
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    
    int lineNumber;
    cout>lineNumber;
    t ...

此程序我在调试的时候发现头文件总是有错误,#include "stdafx.h"这个地方有错误,不知道什么情况!
#10
yang_net2008-08-12 11:04
回复 9# gloomyboy 的帖子
因为我是用VS2005写的,“#include "stdafx.h"”是VS自动加入的,你把它删掉就OK了。。
#11
yang_net2008-08-12 11:05
回复 8# gloomyboy 的帖子
对,就是用数组直接输出。
#12
thames2008-08-12 12:17
回复 8# gloomyboy 的帖子
#include<iostream.h>
main()
{
      int ctr,n;
      cout<<"请输入行数:\n";
      cin>>n;
      char line[n];
//赋初值
for(ctr=0;ctr<n;ctr++)
{ if(ctr<n-1)
     {line[ctr]='.';}
     else{line[ctr]='*';}
     }
//改变数组
 for(ctr=0;ctr<n;ctr++)
 {cout<<line<<"\n";
 line[n-ctr-1]='*';
}
cin.get();
cin.get();
return 0;
}
1