要求:每次作业或实验放到一个博客(随笔)提交,不能将题目分开提交
实验 6-1 题目:用辗转相除法求两个数的最大公约数 最小公倍数
1 /////////////////////////////////////////////////////// 2 // 程序说明:用辗转相除法求两个数的最大公约数、最小公倍数 3 /////////////////////////////////////////////////////// 4 #include <stdio.h> 5 int main() { 6 int m,n; 7 printf("请输入两个整数: "); 8 scanf_s("%d,%d",&m,&n); 9 int mb = m; 10 int nb = n; 11 12 while(1) { 13 // 辗转相除法,用大数除以小数,然后用余数代替大数。整除时的除数就是最大公约数 14 if(n>m) { 15 // 保证用大数除以小数,即保证m>n 16 int tem = m; m = n; n = tem; 17 } 18 // 整除时的除数就是最大公约数 19 int rem= m%n; 20 if (0 == rem) { 21 printf("最大公约数:%d 最小公倍数:%d ",n,(mb*nb)/n); 22 break; 23 } 24 else 25 m = rem; 26 } 27 return 0; 28 }
运行效果
-----------------------------------题目分割线---------------------------------------
实验 6-2 题目:计算两点间的距离
题目描述: 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。
输入要求:输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数据之间用空格隔开。
输出要求:对于每组输入数据,输出一行,结果保留两位小数。
Sample Input
0 0 0 1
0 1 1 0
2 4 4 8
Sample Output
1.00
1.41
4.47
提示:本题用到库函数:
1 #include <math.h> 2 #include <stdio.h> 3 4 int main(void) 5 { 6 double x[2], y[2]; // 定义数组存放坐标 7 int i; 8 for(i=0;i<3;i++) 9 { 10 scanf("%lf%lf%lf%lf", x, y, x+1, y+1); 11 // 计算距离 12 printf("%.2f", sqrt((x[1]-x[0])*(x[1]-x[0]) + (y[1]-y[0])*(y[1]-y[0]))); 13 if (i<=2) printf(" "); 14 } 15 return 0; 16 }
运行效果
-----------------------------------题目分割线---------------------------------------
问题和心得
1. 网上有其他的解决方法更加简练,但明显看不懂!
2. 算法搞清楚了,还是比较容易,这个算法谁发明的,人才呀!