佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1201|回复: 18

我的功课急需帮忙

[复制链接]
发表于 11-8-2008 12:43 AM | 显示全部楼层 |阅读模式
enter a number:  5
output shape:
1                   1
12               21
123           321
1234       4321
12345   54321



要用do-while loop做
#c++
但我只会做一半,不会另一半
只会红的部分

救救我吧
谢谢

[ 本帖最后由 维也纳的咖啡厅 于 11-8-2008 12:58 AM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 11-8-2008 01:44 AM | 显示全部楼层
我只是學過C語言
以下的做法你參考看看
基本上邏輯是一樣的

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int a,b,input,i,j,count;
    printf("enter a number:");
    scanf("%d",&input);
   
    a=0;

    count = 1 ;
    do
    {
          for(i=1;i<=count;i++)    printf("%d",++a);
         
          for(j=0;j<(input*2-count*2);j++) printf(" ");  
         
          b=a;
          for(i=0;i<b;i++)         printf("%d",a--);

          printf("\n");
          count++;
          a = 0;
         
    }while(count != input+1);
    system("pause");
}

[ 本帖最后由 liangcunban 于 11-8-2008 01:49 AM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 11-8-2008 02:02 AM | 显示全部楼层
谢谢你
我试试看
谢谢
回复

使用道具 举报

发表于 11-8-2008 09:55 AM | 显示全部楼层
其实遮体蛮简单的。你只需要抓住了流程就可以了。

input:5
        1
      21
    321
  4321
54321

从这里看得出, 第一个步骤是要现print出4次空格( 也就是 input - loop 的次数 ), 然后print出剩下的号码( loop次数 )
然后loop。

以下是以function写的code, 比较方便明白
  1. #include <iostream>

  2. using namespace std;

  3. void print_space( int no_of_times );
  4. void print_number( int number );

  5. int main()
  6. {
  7.     int input, i;

  8.     cout << "input :";
  9.     cin >> input;

  10.     for( i = 1; i <= input; i++ )
  11.     {
  12.        print_space( input - i );
  13.        print_number(i);
  14.        cout << endl;
  15.     }

  16.     return 0;
  17. }

  18. void print_space( int times )
  19. {

  20.     while( times-- >= 0 )
  21.         cout << " ";
  22. }

  23. void print_number( int number )
  24. {
  25.     while( number >= 1 )
  26.         cout << number--;
  27. }
复制代码
这个是以do-while 写的
  1. #include <iostream>

  2. using namespace std;

  3. int main()
  4. {
  5.     int input, i,
  6.         space_condition, number_condition;

  7.     cout << "input : ";
  8.     cin >> input;

  9.     i = 1;
  10.     do
  11.     {
  12.         //print space
  13.         for( space_condition = 0; space_condition < input - i; space_condition++)
  14.             cout << " ";

  15.         //print remaining number
  16.         for( number_condition = i; number_condition >= 1; number_condition--)
  17.             cout << number_condition;

  18.         cout << endl; //print line break

  19.         i++;
  20.     } while( i <= input );

  21.     return 0;
  22. }
复制代码

[ 本帖最后由 onlylonly 于 11-8-2008 10:04 AM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 11-8-2008 04:47 PM | 显示全部楼层
谢谢你们
我把liangcunban的方式换到一下这个
不完美的
  1. #include<stdio.h>
  2. void main(){
  3.     int a,b,num,y,x,count;
  4.     printf("enter a number:";
  5.     scanf("%d",&num);
  6.    
  7.     a=0;
  8. count=1;
  9.     do{
  10.           for(y=1;y<=count;y++)
  11.      printf("%d",++a);
  12.          
  13.           for(x=0;x<(num*2-count*2);x++)
  14.      printf(" ";  
  15.          
  16.           b=a;
  17.           for(y=0;y<b;y++)
  18.      printf("%d",a--);
  19.     printf("\n";
  20.           count++;
  21. }while(count != num+1);
  22. }
复制代码
红色的是我不大明白及confirm的
有人愿意解释给我知道吗?
谢谢

[ 本帖最后由 维也纳的咖啡厅 于 12-8-2008 02:00 AM 编辑 ]
回复

使用道具 举报

发表于 11-8-2008 07:32 PM | 显示全部楼层

回复 5# 维也纳的咖啡厅 的帖子

看不到颜色。。。。。
回复

使用道具 举报

Follow Us
 楼主| 发表于 12-8-2008 02:03 AM | 显示全部楼层
原帖由 onlylonly 于 11-8-2008 07:32 PM 发表
看不到颜色。。。。。
edit了还是没有
6,7行-
a=0;
count=1;

11行的
x<(num*2-count*2);
13行的
b=a;
14行
y<b


差不多是这样
有我会再更新
你人真好
谢谢
回复

使用道具 举报

发表于 12-8-2008 11:43 AM | 显示全部楼层
原帖由 维也纳的咖啡厅 于 12-8-2008 02:03 AM 发表
edit了还是没有
6,7行-
a=0;
count=1;
11行的
x


#include<stdio.h>

#define n 5     /* set how many lines and max number you want to print here */

main()
{
        int count, i, j;

        count = 1;

        do {
                for(i=1; i<=count; i++)
                        printf("%d",i);         /* print numbers */
                for(j=count*2; j<=n*2; j++)
                        printf(" ";            /* print space = 2*(n-count) */
                for(i=1; i<=count; i++)
                        printf("%d",i);         /* print numbers */

                printf("\n";
        }while(count++ != n);   /* quit when it reach the max number */
}

基本上this one is same with liangcunban,你shi一下这个see can understand or not
p/s: i cannot use online chinese keyin, sry webmin
回复

使用道具 举报


ADVERTISEMENT

发表于 12-8-2008 11:44 AM | 显示全部楼层
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #pragma hdrstop

  6. /*
  7. IDE: Borland C++ Builder 6
  8. Copyright Geekman (C) 2008
  9. */
  10. //---------------------------------------------------------------------------

  11. #pragma argsused
  12. void main(void)
  13. {
  14.     int num;
  15.     do
  16.     {
  17.         clrscr();
  18.         num = 0;
  19.         printf("Please enter a number (1-9, 0 or other key to quit): ");
  20.         scanf("%d", &num);
  21.         printf("\n");
  22.         if(num > 0 && num <= 9)
  23.         {
  24.             for(int count=1; count<=num; count++)
  25.             {
  26.                 for(int i=0; i<num; i++)
  27.                 {
  28.                     if(i < count)
  29.                         printf("%d", i+1);
  30.                     else
  31.                         printf(" ");
  32.                 }
  33.                 printf(" ");

  34.                 for(int i=num; i>0; i--)
  35.                 {
  36.                     if(i <= count)
  37.                         printf("%d", i);
  38.                     else
  39.                         printf(" ");
  40.                 }
  41.                 printf("\n");
  42.             }
  43.             printf("\nPress any key to continue.");
  44.             getch();
  45.         }
  46.     }while(num > 0);
  47. }
  48. //---------------------------------------------------------------------------
复制代码
回复

使用道具 举报

 楼主| 发表于 12-8-2008 06:16 PM | 显示全部楼层
原帖由 retnuoc 于 12-8-2008 11:43 AM 发表


#include

#define n 5     /* set how many lines and max number you want to print here */

main()
{
        int count, i, j;

        count = 1;

        do {
                for(i=1; i
你的比较容易明白,没有这样复杂
谢谢
只是那个不是define5
那个是让user key in a number
上面的只是key in 5 的example
我应该会了
谢谢你的解释
回复

使用道具 举报

发表于 12-8-2008 09:39 PM | 显示全部楼层

回复 10# 维也纳的咖啡厅 的帖子

明白了就好。

我建议你明白了整个program 的 flow 过后, 在尝试理解这里各位大大写的code。

每一个大大都有自己的algorithm, 如果你可以理解,接纳然后尝试分析的话, 那么对你掌握algorithm 与 program flow 上有莫大的帮助。 ( 看source code 学习的最快, 同时可以分辨出何种 programming style 比较简洁, 那种写法属于复杂云云, 对编程方面真的非常好)
回复

使用道具 举报

发表于 12-8-2008 10:36 PM | 显示全部楼层
原帖由 retnuoc 于 12-8-2008 11:43 AM 发表


#include

#define n 5     /* set how many lines and max number you want to print here */

main()
{
        int count, i, j;

        count = 1;

        do {
                for(i=1; i



這個出來的結果應該不是你想要的吧
看code學會學很快,
但是卻失去了自己的風格,
多嘗試自己寫
不會再來問會比較好
回复

使用道具 举报

 楼主| 发表于 13-8-2008 12:13 AM | 显示全部楼层
原帖由 liangcunban 于 12-8-2008 10:36 PM 发表



這個出來的結果應該不是你想要的吧
看code學會學很快,
但是卻失去了自己的風格,
多嘗試自己寫
不會再來問會比較好

再改过应该就能了

我是只会一半不会一半..不会用dowhile loop
回复

使用道具 举报

发表于 13-8-2008 08:22 AM | 显示全部楼层

回复 12# liangcunban 的帖子

开始的阶段看code的确是比较好的。因为可以看到各种不同的写法,风格。 学习了一段时间后就可以开始融合之前所学的的, 开始自己的风格写法。

我倒是认为, 有自己的风格未必是最好的, 寻找最有效率的,最简明的风格才重要的。
毕竟coding是未必是 1 man show 的, 在team的时候, 不好的coding style 就会

在说, 看code也未必一定是看写法风格的。 可以参考其他人的algorithm。 有时相同的东西, 其他人的algorithm会更有效率, 更节省memory的。

这也是为什么资深的programmer都会教别人进入open source community。

新手是养成好的coding style 很重要。 否则过后就难改了。

[ 本帖最后由 onlylonly 于 13-8-2008 08:35 AM 编辑 ]
回复

使用道具 举报

发表于 13-8-2008 11:43 AM | 显示全部楼层
原帖由 维也纳的咖啡厅 于 12-8-2008 06:16 PM 发表
你的比较容易明白,没有这样复杂
谢谢
只是那个不是define5
那个是让user key in a number
上面的只是key in 5 的example
我应该会了
谢谢你的解释


其实是因为懒惰打多字,所以才这样的。。
也不能帮你做完嘛,不然你怎样学?
回复

使用道具 举报

发表于 13-8-2008 02:37 PM | 显示全部楼层

回复 14# onlylonly 的帖子

赞成!一个问题可有多个解决的方法。
回复

使用道具 举报


ADVERTISEMENT

发表于 13-8-2008 02:39 PM | 显示全部楼层
//我改自geekman的写法

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num, count, i, nombor;
    do
    {
        num = 0;
        printf("Please enter a number (1-9, 0 or other key to quit): ");
        scanf("%d", &num);
        printf("\n");
        if(num > 0 && num <= 9)
        {
            for(count=1; count<=num; count++)
            {
                for(i=1; i<=(num*2+1); i++)
                {
                    nombor = (num+1) - abs(i -num-1);
                    printf (nombor<= count ? "%d": " ", nombor);
                }
                printf("\n");
            }
            printf("\nPress any key to continue.");
        }
    }while(num > 0);
    return 0;
}
回复

使用道具 举报

 楼主| 发表于 13-8-2008 11:44 PM | 显示全部楼层
谢谢你们
你们人真好
回复

使用道具 举报

发表于 31-8-2008 04:12 PM | 显示全部楼层
来,我也参一脚

  1. #include <iostream >

  2. main() {
  3. int row , col ;
  4. cout <<"please enter a number : ";
  5. cin >>row;
  6. int j=0;
  7. col = row*2+1;
  8. do{
  9.    for (int i=0 ;  i<col ; i++)
  10.    if (i<=j)
  11.         cout <<(i+1);
  12.    else if(i==col/2)
  13.    {
  14.    cout <<"\t";
  15.    for (int x = col/2 ; x >=0 ; x--)
  16.    if(x <= j)
  17.    cout <<x+1;
  18.    else
  19.    cout <<" ";
  20.    }
  21.    else
  22.    cout <<" ";
  23.    cout <<endl;
  24.    j++;
  25. }while (j<row);

  26. }
复制代码

[ 本帖最后由 ss_sky87 于 31-8-2008 04:28 PM 编辑 ]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 24-12-2025 02:33 AM , Processed in 0.135744 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表