给定两个数,求这两个数的最大公约数
例如:
输入:20 40
输出:20
答题
( 注:编程题请贴代码+运行截图!)
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<Windows.h>
using namespace std;
//利用递归求最大公约数
int recursion(int a, int b)
{
return b == 0 ? a : recursion(b, a % b);
}
void maxCommonDivisor()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
if (a < b)
{
int temp = a;
a = b;
b = temp;
}
while (b)
{
a = b;
b = a % b;
}
cout << a << endl;
}
int main()
{
/*给定两个数,求这两个数的最大公约数
例如:
输入:20 40
输出:20
*/
maxCommonDivisor();
printf("%d\n", recursion(4, 2));
system("pause");
return 0;
}
*/
计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值,打印出结果
```c
#include<stdio.h>
#include<Windows.h>
double calculate()
{
double sum = 0;
double fenzi = 1.0;
int fenmu = 1;
int sign = 1;
while (fenmu <= 100)
{
double temp = fenzi * sign / fenmu;
sum += temp;
fenmu++;
sign = -sign;
}
return sum;
}
int main()
{
//计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值,打印出结果
printf("The result is %f\n", calculate());
system("pause");
return 0;
}
求十个整数的最大值
#include<stdio.h>
#include<Windows.h>
void getMax()
{
int arr[10] = { 93, 6, 1, 7, 73, 75, 88, 25, 78, 353 };
int len = sizeof(arr) / sizeof(arr[0]);
int maxValue = INT_MIN;
for (int i = 0; i < len; i++)
{
if (arr[i] > maxValue)
{
maxValue = arr[i];
}
}
printf("The max value is %d\n", maxValue);
}
int main()
{
//求10 个整数中最大值
getMax();
system("pause");
return 0;
}
```c
//猜数字游戏
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<time.h>
#include<Windows.h>
void menu()
{
printf("**********************************************\n");
printf("********* 1.play **********\n");
printf("********* 0.exit **********\n");
printf("**********************************************\n");
}
void guess()
{
int num = 0;
printf("游戏开始!\n");
int answer = rand() % 100 + 1;
while (1)
{
printf("请输入你猜的数字:(答案在0-100之间) \n");
scanf("%d", &num);
if (num > answer)
{
printf("big\n");
}
else if (num < answer)
{
printf("small\n");
}
else
{
printf("right\n");
break;
}
}
}
int main()
{
srand((unsigned int)time(NULL));
//猜数字游戏
menu();
while (1)
{
printf("请输入你的选择: \n");
int select = 0;
scanf("%d", &select);
switch (select)
{
case 0:
exit(0);
break;
case 1:
guess();
break;
default:
printf("输入错误!\n");
}
}
system("pause");
}