搜索
查看: 603|回复: 11

请教《啊哈c语言》第103页的第二题

[复制链接]
 楼主| 发表于 2019-3-8 15:03:37 | 显示全部楼层 |阅读模式
5啊哈币
《啊哈C语言》第103页有两道题,第一题能做出来,第二题做不出来,请教给位指点。原题如下:
请尝试用while循环打印下面的图形
输入一个整数n(1<=n<=30),当输入的你值为3时,打印结果是:
1
2 3
4 5 6
当输入的n值为5时,打印的结果是:
1
2 3
4 5 6
7 8 910
11 12 13 14 15

谢谢!

最佳答案

查看完整内容

#include #include int main() { int a,b,n; a=1;//a赋值为1 scanf("%d",&n);//在计算器上输出一个整型数字。 while(a
发表于 2019-3-14 07:43:16 | 显示全部楼层
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
        int a,b,c,i;
        a=1;i=1;
        scanf("%d",&b);
        while(a<=b)
                {
                        c=1;
                        while(c<=a)
                                {
                                        printf("%d",i);
                                        i=i+1;
                                        c=c+1;
                                }
                        printf("\n");
                        a=a+1;       
                }
       
return 0;
}
发表于 2019-3-8 15:03:38 | 显示全部楼层
#include <stdio.h>
#include <stdlib.h>
int main()
{
        int a,b,n;
    a=1;//a赋值为1
    scanf("%d",&n);//在计算器上输出一个整型数字。
    while(a<=n && n<=30)//因为a赋值为1,因此while a条件为1<=n<=30.
    {
                b=0;//b赋值为0
        while(b<a)//当a赋值为1,b赋值为0,满足条件进入while b 循环。
        {
                        printf("%d ",a+b);//在计算机上输出一个a+b,a赋值为1,b赋值为0,因此输出为1.
            b=b+1;//b赋值为0,b+1计算结果为1.进入while b循环,b<a不满足跳出循环进入while a(外循环)。
        }
                printf("\n");//另起一行。
        a=a+1;//a赋值为1,因此计算结果等于2.在进入while a循环中。。。。
    }
        system("pause");
        return 0;
}

点评

输入。。scanf是输入、、、  发表于 2019-3-12 06:57
发表于 2019-3-9 13:03:07 | 显示全部楼层
以后这样类型的问题都不需要在这里问了,挑战里面都是这里面的答案
发表于 2019-3-9 13:04:42 | 显示全部楼层
添柴里面有个挑战,点进去里面都是答案
 楼主| 发表于 2019-3-11 10:22:08 | 显示全部楼层
本帖最后由 之乎者也 于 2019-3-11 10:23 编辑

谢谢楼上两位。。。
 楼主| 发表于 2019-3-11 10:33:00 | 显示全部楼层
添柴的挑战里,只能看到题目,看不到答案呐??
 楼主| 发表于 2019-3-11 11:37:53 | 显示全部楼层
一超级圣王一 发表于 2019-3-9 13:04
添柴里面有个挑战,点进去里面都是答案

好像链接打不开呀?
 楼主| 发表于 2019-3-12 22:38:58 | 显示全部楼层
Charles. 发表于 2019-3-11 18:15
#include
#include
int main()

非常感谢楼上的这位大神老师

 楼主| 发表于 2019-3-12 22:45:46 | 显示全部楼层
Charles. 发表于 2019-3-8 15:03
#include
#include
int main()

非常非常感谢您的代码,但程序运行后还稍微有点小偏差,请再指教一下。谢谢!
当输入5的时候显示如下:
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
 楼主| 发表于 2019-3-14 12:19:04 | 显示全部楼层
小葡萄1 发表于 2019-3-14 07:43
#include

/* run this program using the console pauser or add your own getch, system("pause") or  ...

非常感谢大神!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表