代码与总结

3月17日课后作业:

1、给定两个整形变量的值,将两个值的内容进行交换。

#include <stdio.h>
#include <stdlib.h>
int main( )
{
	int a = 1, b = 2;
	int t = 0;
	t = b;
	b = a;
	a = t;
	printf("%d %d\n", a, b);
system("pause");
return 0;
}

总结1:在交换变量之前应该给变量进行定义,输出是应注意加双引号,主函数不能缺少。

2、不允许创建临时变量,交换两个数的内容(附加题)

#include <stdio.h>
#include <stdlib.h>
int main( )
{
	int a = 2, b = 3;
	a = a + b;
	b = a - b;
	a = a - b;
	printf("%d %d\n", a, b);
	system("pause");
	return 0;
}

总结2:首先应该思考这道题的算法,再将它转化为代码形式,倒不清楚关系是,可以将步骤在脑子里过一遍,或者先写出最初的方法,再进行修正。

3、求10个整数中的最大值

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
{
	int i;
	int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int max = a[0];
	for (i = 0; i <= 9; i++)
	{
	if (max < a[i])
		max = a[i];
	}
	printf("max=%d\n", max);
	system("pause");
    return 0;
}

总结3:数组定义是,应注意它的位数及下标,无论是变量还是数组,都只能在顶以后进行使用。循环时,应注意循环体的大括号不能丢。

4、将三个数按从大到小输出

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
{
	int temp;
	int x = 0;
	int y = 0;
	int z = 0;
	printf("请输入三个整数:");
	scanf("%d %d %d",&x,&y,&z);
	if (y > x)
	{
		temp = y;
		y = x;
		x = temp;
	}
	else if (z > y)
	{
		temp = z;
		z = y;
		y = temp;
	}
	else if (y > x)
	{
		temp = y;
		y = x;
		x = temp;
	}
	printf("从大到小%d %d %d", x, y, z);
	system("pause");
	return 0;
}

总结4:在进行变量赋值后,应注意,是变量的值进行了交换,而不是变量的位置进行交换,搞清楚变量之间的关系后,再执行下一个步骤。

5、求两个数的最大公约数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
{
	int m = 0;
	int n = 0;
	int r = 0;
	printf("输入两个正整数: ");
	scanf("%d %d", &m, &n);
	r = m % n;
	while (r != 0)
	{
		m = n;
		n = r;
		r = m % n;
	}
	printf("最大公约数为: %d\n", n);
	system("pause");
	return 0;
}

总结5:这道题的困难之处在于基本的数学概念梳理不清,导致没有思路去解决问题,只能借助外界概念,再加以修改而成。
总结:由于知识没有很好的掌握,导致很多细节都出了问题,以后应在注意小细节的基础上,多多尝试不同的代码,理解他们之间的联系。将代码很好的融合在一起。

猜你喜欢

转载自blog.csdn.net/qq_44781020/article/details/88676902