|
查看: 1201|回复: 18
|
我的功课急需帮忙
[复制链接]
|
|
|
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 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 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, 比较方便明白- #include <iostream>
- using namespace std;
- void print_space( int no_of_times );
- void print_number( int number );
- int main()
- {
- int input, i;
- cout << "input :";
- cin >> input;
- for( i = 1; i <= input; i++ )
- {
- print_space( input - i );
- print_number(i);
- cout << endl;
- }
- return 0;
- }
- void print_space( int times )
- {
- while( times-- >= 0 )
- cout << " ";
- }
- void print_number( int number )
- {
- while( number >= 1 )
- cout << number--;
- }
复制代码 这个是以do-while 写的- #include <iostream>
- using namespace std;
- int main()
- {
- int input, i,
- space_condition, number_condition;
- cout << "input : ";
- cin >> input;
- i = 1;
- do
- {
- //print space
- for( space_condition = 0; space_condition < input - i; space_condition++)
- cout << " ";
- //print remaining number
- for( number_condition = i; number_condition >= 1; number_condition--)
- cout << number_condition;
- cout << endl; //print line break
- i++;
- } while( i <= input );
- return 0;
- }
复制代码
[ 本帖最后由 onlylonly 于 11-8-2008 10:04 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 11-8-2008 04:47 PM
|
显示全部楼层
谢谢你们
我把liangcunban的方式换到一下这个
不完美的- #include<stdio.h>
- void main(){
- int a,b,num,y,x,count;
- printf("enter a number:";
- scanf("%d",&num);
-
- a=0;
- count=1;
- do{
- for(y=1;y<=count;y++)
- printf("%d",++a);
-
- for(x=0;x<(num*2-count*2);x++)
- printf(" ";
-
- b=a;
- for(y=0;y<b;y++)
- printf("%d",a--);
- printf("\n";
- count++;
- }while(count != num+1);
- }
复制代码 红色的是我不大明白及confirm的
有人愿意解释给我知道吗?
谢谢
[ 本帖最后由 维也纳的咖啡厅 于 12-8-2008 02:00 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 11-8-2008 07:32 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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 11:44 AM
|
显示全部楼层
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #include <stdio.h>
- #include <conio.h>
- #pragma hdrstop
- /*
- IDE: Borland C++ Builder 6
- Copyright Geekman (C) 2008
- */
- //---------------------------------------------------------------------------
- #pragma argsused
- void main(void)
- {
- int num;
- do
- {
- clrscr();
- 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(int count=1; count<=num; count++)
- {
- for(int i=0; i<num; i++)
- {
- if(i < count)
- printf("%d", i+1);
- else
- printf(" ");
- }
- printf(" ");
- for(int i=num; i>0; i--)
- {
- if(i <= count)
- printf("%d", i);
- else
- printf(" ");
- }
- printf("\n");
- }
- printf("\nPress any key to continue.");
- getch();
- }
- }while(num > 0);
- }
- //---------------------------------------------------------------------------
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 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
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 13-8-2008 02:37 PM
|
显示全部楼层
回复 14# onlylonly 的帖子
赞成!一个问题可有多个解决的方法。 |
|
|
|
|
|
|
|
|
|
|
发表于 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
|
显示全部楼层
来,我也参一脚
- #include <iostream >
- main() {
- int row , col ;
- cout <<"please enter a number : ";
- cin >>row;
- int j=0;
- col = row*2+1;
- do{
- for (int i=0 ; i<col ; i++)
- if (i<=j)
- cout <<(i+1);
- else if(i==col/2)
- {
- cout <<"\t";
- for (int x = col/2 ; x >=0 ; x--)
- if(x <= j)
- cout <<x+1;
- else
- cout <<" ";
- }
- else
- cout <<" ";
- cout <<endl;
- j++;
- }while (j<row);
- }
复制代码
[ 本帖最后由 ss_sky87 于 31-8-2008 04:28 PM 编辑 ] |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|