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

一道简单输出****的问题

跳跳鱼 发布于 2012-07-24 16:25, 661 次点击
题目要求,编写一程序,输出以下图案,
* * * * * * * *
 * * * * * * * *
* * * * * * * *
 * * * * * * * *
* * * * * * * *
 * * * * * * * *
要求只能用以下输出语句,cout<<"* "    cout<<" "      cout<<endl
我编了一个,可是不怎么太对,能帮我改改吗?或者您编一个让我学习一下也行。。。
程序代码:
#include <iostream>
using namespace std;
int main()
{
    int num,i=1,m=1,n=1;      //i,m,n均为计数器
    cout<<"轻如如我们要输出多少行,请输入偶数"<<endl;
    cin>>num;
    while(i<=num/2)
    {
        while(m<=8)
    {
        cout<<"* ";              //输出第一横行
        m++;
    }
    cout<<endl;
    cout<<" ";
    while(n<=8)
    {
     cout<<"* ";                 //输出第二横行
     n++;
    }
    i++;                        //用i判断输出几次两行的图案
    }
    return 0;
}
10 回复
#2
跳跳鱼2012-07-24 16:28
上边的图案显示错了,偶数行都是空一个空格的,刚才打了,电脑显示不出来
* * * * * * * *
空* * * * * * * *
* * * * * * * *
空* * * * * * * *
* * * * * * * *
空* * * * * * * *
#3
chenchaoyiji2012-07-24 19:20
你的程序运行完一遍之后,m和n就都变成8了,后面的循环就不会产生在执行while后的语句;每次执行完后都要定义m和n为1.
#include <iostream>
using namespace std;
int main()
{
    int num,i=1,m=1;      //i,m为计数器
    cout<<"我们要输出多少行,请输入偶数"<<endl;
    cin>>num;
   for(i=1;i<=num;i++)
   {    m=1;
       while(m<=8)
    {
        cout<<"* ";              //输出第一横行
        m++;
    }
    cout<<endl;
      
                         //用i判断输出几次两行的图案
    }
    return 0;
}
#4
有容就大2012-07-24 22:10
程序代码:
#include <iostream>
using namespace std;

int main(void)
{
    int lines, rows, i=1, j = 1;      // lines 是总行数, rows 是总列数, i j 是循环变量
                                             
    cout << "请输入你想要的行数和列数。" << endl;
    cout << "总行数是:";
    cin >> lines;
    cout << "总列数是:";
    cin >> rows;
    while(i <= lines)
    {
        if (i % 2 == 1)    // 输出奇数行 最上面一行为第一行
        {
            while(j <= rows)
            {
                cout<<"* ";         
                j++;
            }
            cout<<endl;
        }

        if (i % 2 == 0)    // 输出偶数行
        {
            cout << "  ";
            while(j + 1 <= rows)
            {
                cout<<"* ";         
                j++;
            }
            cout<<endl;
        }

        i++;       // 保障循环结束
        j = 1;     // 使列数标记恢复到第一列            
    }
    system("pause");

    return 0;
}

看看这个效果怎么样?
只有本站会员才能查看附件,请 登录


[ 本帖最后由 有容就大 于 2012-7-24 22:12 编辑 ]
#5
pangding2012-07-25 00:19
回复 4楼 有容就大
我觉得这个效果很好,楼主可以满意结帖了。
#6
woshiluli32012-07-25 08:52
我也是个新学者,不过没看懂你那个,搞不懂为什么要分奇偶行,下面是我写的,我试了下,没问题的
#include<iostream>
using namespace std;
int main()
{
    int m;
    int n;
    cout<<"输入行和列:";
    cin>>m>>n;
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
            cout<<"* ";
        cout<<endl;
    }
    return 0;
}
#7
跳跳鱼2012-07-25 12:37
回复 5楼 pangding
满意!绝对满意!谢谢大家了!O(∩_∩)O哈哈~
#8
跳跳鱼2012-07-25 12:39
回复 6楼 woshiluli3
你仔细看看我在二楼画的那个图就知道了,嘿嘿!刚开始那个图显示错了
#9
lz10919149992012-07-25 21:20
程序代码:
#include <iostream>
using namespace std;

int main()
{
    int columns, rows;
    cin >> rows >> columns;
    for (int i = 0; i < rows; ++i) {
        if (i % 2)
            cout << "  ";
        for (int j = 0; j < columns; ++j)
            cout << "* ";
        cout << endl;
    }
}
#10
Aidoneus2012-08-04 21:05
#include <iostream>
int main()
{
    for (int x=0;x<48;x++)
    {
        if (x%8==0)
        {
            std::cout<<"\n";
        }
        if (x==8||x==24||x==40)
        {
            std::cout<<"  ";
        }
        std::cout<<"* ";
    }
    if (x==48)
        {
            std::cout<<std::endl;
        }
        return 0;
}


无聊看着写了下。应该是这样吧
只有本站会员才能查看附件,请 登录
  
#11
lyp8809242012-08-05 14:08
我 觉得既然是用c++写程,用面向对象的思想写更好一些,而不是一个main函数到底
1