【C】入门级编程,学习第三天

  1. 优化上一篇的最大公约数
  2. 将数组A中的内容和数组B中的内容进行交换。(数组一样大)
  3. 计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值。
  4. 编写程序数一下 1到 100 的所有整数中出现多少次数字9。

1

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>


int main()
{
	int a = 0, b = 0,i=0,j=0;
	printf("请输入两个整数:");
	scanf("%d %d", &a, &b);
	if (b > a)
	{
		i = a;
		a = b;
		b = i;
	}
	if (a%b != 0)
	{
		i = a - b;
		a = b;
		b = i;
	}
	printf("最大公约数为%d\n",b);
	system("pause");
}

运行结果

2

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>


void swap(int* x, int* y)
{
	int temp = 0;
	temp = *x;
	*x = *y;
	*y = temp;
}
int main()
{
	int arr1[10] = { 0 }, arr2[10] = { 0 };
	printf("请输入第一个数组中的值");
	for (int i = 0; i < 10; i++)
	{
		scanf("%d", &arr1[i]);
	}
	printf("请输入第二个数组中的值");
	for (int j = 0; j < 10; j++)
	{
		scanf("%d", &arr2[j]);
	}
	for (int k = 0; k < 10; k++)
	{
		swap(&arr1[k],  &arr2[k]);
	}
	printf("交换之后的数组1为:");
	for (int i = 0; i < 10; i++)
	{
		printf("%d\t", arr1[i]);
	}
	printf("\n");
	printf("交换之后的数组2为:");
	for (int j = 0; j < 10; j++)
	{
		printf("%d\t", arr2[j]);
	}
	printf("\n");
	system("pause");
}

运行结果

3

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>

int main()
{
	double num=0;
	for (int i = 1; i < 101; ++i )
	{
		if (i % 2 == 0)
		{
			num -= (1.0 / i);
		}
		else
		{
			num += (1.0 / i);
		}
	}
	printf("%f", num);
	system("pause");
}


运行结果

4

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>

int main()
{
	int i = 0, j = 0;
	for ( i = 1; i < 101; ++i)
	{
		if (i % 10 == 9)
		{
			j += 1;
		}
	}
	j += 1;//加上个位数上的9
	printf("1到100共出现%d次9\n", j);
	system("pause");
}



运行结果

猜你喜欢

转载自blog.csdn.net/qq_38606740/article/details/88618833
今日推荐