啊哈C语言 第四章 【代码】【习题答案】

啊哈C语言 第四章 【代码】【习题答案】,第1张

目录

第1节        永不停止的哭声

让计算机永不停止地在屏幕上打印wa:

不停地在屏幕上打印0和1:

让计算机“永无止境”地打印hello:

让计算机“永不止境”地在屏幕上显示中文汉字“你好”:

第2节        我说几遍就几遍

使用while循环输出100个wa:

打印1~100:

让计算机先输出100再输出99、98、97、……、1:

打印1~100中的偶数:

打印1~100中能被3整除的数:

让计算机从100打印到200:

让计算机从1打印到100再打印到1:

第3节        if对while说:我对你很重要

让计算机输出1~100中所有不能被3整除的数:

输出1~100中能被3整除但不能被5整除的所有数:

1~100中所有7的倍数和末尾含7的数:

第4节        求和!求和!!求和!!!

求1 + 2 + 3的值:

求1~100中所有数的和:

求1~100中所有7的倍数或者末尾含7的数的总和:

求1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10的值:

求1~100所有偶数的和:

输入一个整数n(1 <= n <= 9),求n的阶乘:

第5节        逻辑挑战4:60秒倒计时开始

让计算机输出3、2、1、0:

让计算机输出3、2、1、0(每过一秒打印一个数):

让计算机输出3、2、1、0(每次打印新内容前清除屏幕):

3秒的倒计时:

60秒的倒计时:

第6节        这个有点晕——循环嵌套来了

3行星号,每行5个星号的图形(循环嵌套):

5行星号,第一行1个星号,第二行2个星号……:

请尝试用while循环打印下面的图形(1):

请尝试用while循环打印下面的图形(2):

第7节        逻辑挑战5:奔跑的字母

字母H从左边向右边移动了3步:

第8节        究竟循环了多少次

打印6个OK:

打印8个OK:

请问下面这段代码会打印多少个“OK”?

第9节        逻辑挑战6:奔跑的小人

小人:

奔跑的小人:

设计一个“小人”并让它从右边向左边奔跑:

第10节        for隆重登场

用while让计算机从1循环到10:

用for让计算机从1循环到10:

用for循环来实现1~100中所有数的总和:

打印1~100中所有偶数:

用for循环输出1~100中所有7的倍数或者末尾含7的数:

求1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10的值:

尝试用for循环打印下面的图形:

尝试用for循环来打印一个九九乘法表:


第1节        永不停止的哭声


让计算机永不停止地在屏幕上打印wa:
#include 
#include 

int main()
{
	while (1 > 0)
	{
		printf("wa ");
	}

	system("pause");
	return 0;
}

调试结果:



不停地在屏幕上打印0和1:
#include 
#include 

int main()
{
	system("color 02");
	while (1 > 0)
	{
		printf(" 0 1");
	}

	system("pause");
	return 0;
}

调试结果:



让计算机“永无止境”地打印hello:
#include 
#include 

int main()
{
	while (1 > 0)
	{
		printf("hello ");
	}

	system("pause");
	return 0;
}

调试结果:



让计算机“永不止境”地在屏幕上显示中文汉字“你好”:
#include 
#include 

int main()
{
	while (1 > 0)
	{
		printf("你好 ");
	}

	system("pause");
	return 0;
}

调试结果:


第2节        我说几遍就几遍


使用while循环输出100个wa:
#include 
#include 

int main()
{
	int a;
	a = 1;
	while (a <= 100)
	{
		printf("wa ");
		a = a + 1;
	}
	printf("\n");

	system("pause");
	return 0;
}

调试结果:



打印1~100:
#include 
#include 

int main()
{
	int a;
	a = 1;
	while (a <= 100)
	{
		printf("%d ", a);
		a = a + 1;
	}
	printf("\n");

	system("pause");
	return 0;
}

调试结果:



让计算机先输出100再输出99、98、97、……、1:
#include 
#include 

int main()
{
	int a;
	a = 100; //初始值从100开始
	while (a >= 1) //请注意这里的循环条件变为 a >= 1
	{
		printf("%d ", a);
		a = a - 1; //每循环一次将a的值递减1
	}
	printf("\n");

	system("pause");
	return 0;
}

调试结果:



打印1~100中的偶数:
#include 
#include 

int main()
{
	int a;
	a = 2;
	while (a <= 100)
	{
		printf("%d ", a);
		a = a + 2;
	}
	printf("\n");

	system("pause");
	return 0;
}

调试结果:



打印1~100中能被3整除的数:
#include 
#include 

int main()
{
	int a;
	a = 3;
	while (a <= 100)
	{
		printf("%d ", a);
		a = a + 3;
	}
	printf("\n");

	system("pause");
	return 0;
}

调试结果:



让计算机从100打印到200:
#include 
#include 

int main()
{
	int a;
	a = 100;
	while (a <= 200)
	{
		printf("%d ", a);
		a = a + 1;
	}
	printf("\n");

	system("pause");
	return 0;
}

调试结果:



让计算机从1打印到100再打印到1:
#include 
#include 

int main()
{
	int a;
	a = 1;
	while (a < 100)
	{
		printf("%d ", a);
		a = a + 1;
	}
	while (a >= 1)
	{
		printf("%d ", a);
		a = a - 1;
	}
	printf("\n");

	system("pause");
	return 0;
}

调试结果:


第3节        if对while说:我对你很重要


让计算机输出1~100中所有不能被3整除的数:
#include 
#include 

int main()
{
	int a;
	a = 1;
	while (a <= 100)
	{
		if (a % 3 != 0)
			printf("%d ", a);
		a = a + 1;
	}
	printf("\n");

	system("pause");
	return 0;
}

调试结果:



输出1~100中能被3整除但不能被5整除的所有数:
#include 
#include 

int main()
{
	int a;
	a = 1;
	while (a <= 100)
	{
		if (a % 3 == 0 && a % 5 != 0)
			printf("%d ", a);
		a = a + 1;
	}
	printf("\n");

	system("pause");
	return 0;
}

调试结果:



1~100中所有7的倍数和末尾含7的数:
#include 
#include 

int main()
{
	int a;
	a = 1;
	while (a <= 100)
	{
		if (a % 7 == 0 || a % 10 == 7)
			printf("%d ", a);
		a = a + 1;
	}
	printf("\n");

	system("pause");
	return 0;
}

调试结果:


第4节        求和!求和!!求和!!!


求1 + 2 + 3的值:
#include 
#include 

int main()
{
	int a;
	a = 1 + 2 + 3;
	printf("%d\n", a);

	system("pause");
	return 0;
}

调试结果:



求1~100中所有数的和:
int main()
{
	int a, i;
	a = 0;
	i = 1;
	while (i <= 100)
	{
		a = a + i;
		i = i + 1;
	}
	printf("%d\n", a);

	system("pause");
	return 0;
}

调试结果:



求1~100中所有7的倍数或者末尾含7的数的总和:
#include 
#include 

int main()
{
	int a, i;
	a = 0;
	i = 1;
	while (i <= 100)
	{
		if (i % 7 == 0 || i % 10 == 7)
		{
			a = a + i;
		}
		i = i + 1;
	}
	printf("%d\n", a);

	system("pause");
	return 0;
}

调试结果:



求1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10的值:
#include 
#include 

int main()
{
	int a, i;
	a = 1;
	i = 1;
	
	while (i < 10)
	{
		i = i + 1;
		a = a * i;
	}
	printf("%d\n", a);

	system("pause");
	return 0;
}

调试结果:



求1~100所有偶数的和:
#include 
#include 

int main()
{
	int a, i;
	a = 0;
	i = 1;
	while (i <= 100)
	{
		if (i % 2 == 0)
		{
			a = a + i;
		}
		i = i + 1;
	}
	printf("%d\n", a);


	system("pause");
	return 0;
}

调试结果:



输入一个整数n(1 <= n <= 9),求n的阶乘:
#include 
#include 
int main()
{
	int ret = 1;
	int num = 0;
	scanf("%d", &num);
	if (1 <= num && num <= 9)
	{
		while (num)
		{
			ret = ret * num;
			num = num - 1;
		}
		printf("%d\n", ret);
	}

	system("pause");
	return 0;
}

调试结果:


第5节        逻辑挑战4:60秒倒计时开始


让计算机输出3、2、1、0:
#include 
#include 

int main()
{
	printf("3\n");
	printf("2\n");
	printf("1\n");
	printf("0\n");

	system("pause");
	return 0;
}

调试结果:



让计算机输出3、2、1、0(每过一秒打印一个数):
#include 
#include 
#include 

int main()
{
	printf("3\n");
	Sleep(1000);
	printf("2\n");
	Sleep(1000);
	printf("1\n");
	Sleep(1000);
	printf("0\n");

	system("pause");
	return 0;
}

调试结果:



让计算机输出3、2、1、0(每次打印新内容前清除屏幕):
#include 
#include 
#include 

int main()
{
	system("cls");
	printf("3\n");
	Sleep(1000);

	system("cls");
	printf("2\n");
	Sleep(1000);

	system("cls");
	printf("1\n");
	Sleep(1000);

	system("cls");
	printf("0\n");
	Sleep(1000);

	system("pause");
	return 0;
}

调试结果:



3秒的倒计时:
#include 
#include 
#include 

int main()
{
	int a;
	a = 3;
	while (a >= 0)
	{
		system("cls");
		printf("%d\n", a);
		Sleep(1000);
		a = a - 1;
	}

	system("pause");
	return 0;
}

调试结果:



60秒的倒计时:
#include 
#include 
#include 

int main()
{
	int a;
	a = 60;
	system("color 0a");
	while (a >= 0)
	{
		system("cls");
		printf("%d\n", a);
		Sleep(1000);
		a = a - 1;
	}

	system("pause");
	return 0;
}

调试结果:



两分钟的倒计时:

#include 
#include 
#include

int main()
{
	int i, j;             //i控制分;j控制秒;
	system("color ea");  //黄底绿字;里面控制颜色的参数是16进制,从1-f;
	for (i = 1; i >= 0; i--)    //**外循环**,i从1自减到0;**控制分**从1到0;
	{
		if (i >= 1)        //这个判断语句用来输出第一个出现的时间:“2:00”;
		{               //只有在i=1的时候才会成立,但要注意括号内的条件表达式不能写成“i=1”,这代表条件恒成立;
			printf("2:00");
			Sleep(1000);  //等待时间;1000ms==1s;
		}
		for (j = 59; j >= 0; j--) //**内循环**,j从59自减到0;**控制秒**;
		{
			system("cls"); //清屏;
			if (j <= 9)       //这个判断语句用来输出形如“1:09”的时间样式;如果没有的话只会输出“1:9”,这显然与题意有出入;
			{
				printf("%d:0%d\n", i, j);
				Sleep(1000);
				continue;
			}
			printf("%d:%d\n", i, j);
			Sleep(1000);
		}
	}
	return 0;
}

调试结果:


第6节        这个有点晕——循环嵌套来了


3行星号,每行5个星号的图形(循环嵌套):
#include 
#include 
#include

int main()
{
	int a, b;
	a = 1;
	while (a <= 3)
	{
		b = 1;
		while (b <= 5)
		{
			printf("*");
			b = b + 1;
		}
		printf("\n");
		a = a + 1;
	}

	system("pause");
	return 0;
}

调试结果:



5行星号,第一行1个星号,第二行2个星号……:
#include 
#include 
#include


int main()
{
	int a, b;
	a = 1;
	while (a <= 5)
	{
		b = 1;
		while (b <= a)
		{
			printf("*");
			b = b + 1;
		}
		printf("\n");
		a = a + 1;
	}

	system("pause");
	return 0;
}

调试结果:



请尝试用while循环打印下面的图形(1):

输入一个整数n(1 <= n <= 30),当输入的n值为3时,打印结果为:

1
2    2
3    3    3

输入一个整数n(1 <= n <= 30),当输入的n值为6时,打印结果为:

1
2    2
3    3    3
4    4    4    4
5    5    5    5    5
6    6    6    6    6    6

代码:

#include 
#include 
#include


int main()
{
	int a, b;
	a = 1;
	while (a <= 5)
	{
		b = 1;
		while (b <= a)
		{
			printf("*");
			b = b + 1;
		}
		printf("\n");
		a = a + 1;
	}

	system("pause");
	return 0;
}

调试结果:

 



请尝试用while循环打印下面的图形(2):

输入一个整数n(1 <= n <= 30),当输入的n值为3时,打印结果是:

1
2    3
4    5    6

输入一个整数n(1 <= n <= 30),当输入的n值为5时,打印结果是:

1
2    3
4    5    6
7    8    9    10
11   12   13   14   15

代码:

#include 
#include 
#include


int main()
{
	int a, b, c, n;
	a = 1;
	c = 1;
	scanf("%d", &n);
	while (a <= n)
	{
		if (1 <= n <= 30)
		{
			b = 1;
			while (b <= a)
			{
				printf("%d ", c);
				b = b + 1;
				c = c + 1;
			}
		}
		printf("\n");
		a = a + 1;
	}

	system("pause");
	return 0;
}

调试结果:


第7节        逻辑挑战5:奔跑的字母


字母H从左边向右边移动了3步:
#include 
#include 
#include

int main()
{
	int a, b;
	a = 0;
	while (a <= 2)
	{
		system("cls");
		b = 1;
		while (b <= a)
		{
			printf(" ");
			b = b + 1;
		}

		printf("H\n");
		Sleep(1000);
		a = a + 1;
	}

	system("pause");
	return 0;
}

调试结果:


第8节        究竟循环了多少次


打印6个OK:
#include 
#include 

int main()
{
	int a, b;
	a = 1;
	while (a <= 2)
	{
		b = 1;
		while (b <= 3)
		{
			printf("ok ");
			b = b + 1;
		}
		a = a + 1;
	}
	printf("\n");
	
	system("pause");
	return 0;
}

调试结果:



打印8个OK:
#include 
#include 

int main()
{
	int a, b;
	a = 1;
	while (a <= 4)
	{
		b = 1;
		while (b <= 2)
		{
			printf("ok ");
			b = b + 1;
		}
		a = a + 1;
	}
	printf("\n");
	
	system("pause");
	return 0;
}

调试结果:



请问下面这段代码会打印多少个“OK”?
#include 
#include 

int main()
{
	int i, j;
	i = 1;
	while (i <= 10)
	{
		j = 1;
		while (j <= i)
		{
			printf("OK ");
			j = j + 1;
		}
		i = i + 1;
	}
	printf("\n");

	system("pause");
	return 0;
}

调试结果:

答案:55个“ok”。 


第9节        逻辑挑战6:奔跑的小人


小人:
#include 
#include 

int main()
{
	printf(" O\n");
	printf("\n");
	printf("I I\n");

	system("pause");
	return 0;
}

调试结果:



奔跑的小人:
#include 
#include 
#include

int main()
{
	int a, b;
	a = 0;
	while (a <= 2)
	{
		system("cls");

		b = 1;
		while (b <= a)
		{
			printf(" ");
			b = b + 1;
		}
		printf(" O\n");

		b = 1;
		while (b <= a)
		{
			printf(" ");
			b = b + 1;
		}
		printf("\n");

		b = 1;
		while (b <= a)
		{
			printf(" ");
			b = b + 1;
		}
		printf("I I\n");

		Sleep(1000);
		a = a + 1;
	}

	system("pause");
	return 0;
}

调试结果:



设计一个“小人”并让它从右边向左边奔跑:
#include 
#include 
#include

int main()
{
	int a = 0, b;
	while (a <= 10)
	{
		system("cls");
		b = 10;
		while (b > a)
		{
			printf(" ");
			b = b - 1;
		}
		printf(" 0\n");
		b = 10;
		while (b > a)
		{
			printf(" ");
			b = b - 1;
		}
		printf("\n");
		b = 10;
		while (b > a)
		{
			printf(" ");
			b = b - 1;
		}
		printf("I I\n");
		Sleep(100);
		a = a + 1;
	}

	system("pause");
	return 0;
}

调试结果:


第10节        for隆重登场


用while让计算机从1循环到10:
#include 
#include 

int main()
{
	int a;
	a = 1;
	while (a <= 10)
	{
		printf("%d ", a);
		a = a + 1;
	}
	printf("\n");

	system("pause");
	return 0;
}

调试结果:



用for让计算机从1循环到10:
#include 
#include 

int main()
{
	int a;

	for (a = 1; a <= 10; a = a + 1)
	{
		printf("%d ", a);
	}
	printf("\n");

	system("pause");
	return 0;
}

调试结果:



用for循环来实现1~100中所有数的总和:
#include 
#include 

int main()
{
	int a, sum;
	sum = 0;
	for (a = 1; a <= 100; a++)
	{
		sum = sum + a;
	}
	printf("%d ", sum);
	printf("\n");

	system("pause");
	return 0;
}

调试结果:



打印1~100中所有偶数:
#include 
#include 

int main()
{
	int a;

	for (a = 2; a <= 100; a = a + 2)
	{
		printf("%d ", a);
	}
	printf("\n");

	system("pause");
	return 0;
}

调试结果:



用for循环输出1~100中所有7的倍数或者末尾含7的数:
int main()
{
	int a;

	for (a = 1; a <= 100; a++)
	{
		if (a % 7 == 0 || a % 10 == 7)
			printf("%d ", a);
	}
	printf("\n");

	system("pause");
	return 0;
}

调试结果:



求1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10的值:
#include 
#include 

int main()
{
	int a, b, sum;
	a = 10;
	sum = 1;
	for (b = 1; b <= a; b++)
	{
		sum = sum * b;
	}
	printf("%d\n", sum);

	system("pause");
	return 0;
}

调试结果:



尝试用for循环打印下面的图形:
#include 
#include 

int main()
{
	int a, b, c;
	for (a = 1; a <= 4; a++)
	{
		for (b = 4; b >= a; b--)
		{
			printf(" ");
		}
		for (c = 1; c <= 2 * a - 1; c++)
		{
			printf("*");
		}
		printf("\n");
	}
	for (a = 1; a <= 5; a++)
	{
		for (b = 1; b <= a - 1; b++)
		{
			printf(" ");
		}
		for (c = 1; c <= 9 - 2 * (a - 1); c++)
		{
			printf("*");
		}
		printf("\n");
	}

	system("pause");
	return 0;
}

调试结果:



尝试用for循环来打印一个九九乘法表:
#include 
#include 

int main()
{
	int a, b;
	for (a = 1; a <= 9; a++)
	{
		for (b = 1; b <= a; b++)
		{
			if (a > 2 && a < 5)
			{
				if (b == 3)
				{
					printf(" ");
				}
			}
			printf("%d * %d = %d  ", b, a, b * a);
		}
		printf("\n");
	}

	system("pause");
	return 0;
}

调试结果:


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/2889668.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-14
下一篇 2022-09-14

发表评论

登录后才能评论

评论列表(0条)

保存