chapter2——习题

1. 算法:解决问题的步骤和方法。

2. 结构化的算法:运用顺序,选择,循环三种结构所表示的算法。这三种结构能解决一切问题。并得到了图灵的理论证明。

3. 顺序结构:顺序化解决问题

选择结构:在遇到选择的难度时可以提供解决方案

循环结构:对于一些重复性的操作可以通过循环来解决,将大问题简化步骤,理清思路。

二、代码

1、

/*
	2013年5月17日13:37:54
	目的:	设计一个交换程序
*/
# include <stdio.h>

int main(void)
{
	int a, b, t;	//	t是用来交换的临时变量	

	printf("请输入两个数,中间用空格分开: ");
	scanf("%d %d", &a, &b);

	t = a;             /*数据交换的操作*/					  
	a = b;
	b = t;

	printf("a = %d, b = %d", a, b);

	return 0;
}
/*
	在VC++6.0中的输出结果是:
	-----------------------------
	请输入两个数,中间用空格分开: 123 321
	a = 321, b = 123Press any key to continue
	-----------------------------
*/
2、
/*
	2013年5月17日23:47:05
	目的:	依次将10个数输入,并输出其中的最大值
*/

# include <stdio.h>
int main(void)
{
	int i, a[10], j, t;

	printf("请输入10个数字,中间用空格分开:");
	for(i = 0; i < 10; ++i)	//用数组来对给定的数据进行输人
	{
		scanf("%d", &a[i]);
	}

	for(j = 1; j < 10; ++j)	//对10个数进行判断,按照大的放入a[0]进行判断
	{
		if (a[j] > a[0])
		{
			t = a[j];
			a[j] = a[0];
			a[0] = t;
		}
	}

	printf("输出的最大值是: %d\n", a[0]);

	return 0;
}
/*
	在VC++6.0中的输出结果是:
	----------------------------------
	请输入10个数字,中间用空格分开:2 3 4 12 43 6  8 7 6 54
	输出的最大值是: 54
	Press any key to continue
	----------------------------------
*/

3、

/*
	2013年5月18日0:59:12
	目的:	求1+2+3+.....+100
*/
# include <stdio.h>
int main(void)
{
	int i, sum=0;

	for(i = 1; i < 101; ++i)	//用循环来累加
	{
		sum = sum + i;
	}
	printf("输出的结果是%d\n", sum);

	return 0;
}
/*
	在VC++6.0中的输出结果是:
	-------------------------------
	输出的结果是5050
	Press any key to continue
	-------------------------------
*/

4、

/*
	2013年5月20日13:09:03
	目的:	判断一个数n能否同时被3或者5 整除
*/
# include <stdio.h>
int main(void)
{
	int n;

	printf("请输入一个数: ");
	scanf("%d", &n);

	if(n % 3 == 0)
	{
		if(n % 5 == 0)
		{
			printf("则这个数能同时被3和5整除\n");
		}
	}

	return 0;
}
/*
	在VC++6.0中的输出结果是:
	------------------------------------
	请输入一个数: 15
	则这个数能同时被3和5整除
	Press any key to continue

	------------------------------------
*/


5、


 

原文地址:https://www.cnblogs.com/zhurun/p/4590571.html