入门2

//交换两个数组的内容

#include <stdio.h>
int main(){
	int a[5] = { 3, 4, 5, 6, 7 };
	int b[5] = { 234, 456, 565, 444, 777 };
	int t;
	int i = 5;
	for (i = 0; i < 5; i++){
		t = a[i]; a[i] = b[i]; b[i] = t;
		printf("%d\t", a[i]);
		if (i == 4){
			printf("\n");
		}
	}for (i = 0; i < 5; i++){
		printf("%d\t", b[i]);
	}
	system("pause");
	return 0;
}

//计算1/1-1/2+1/3…-1/100

#include <stdio.h>
int main(){
	int i;
	double sum = 0;
	for (i = 1; i <= 100; i++){
		if (i % 2 == 0){
			sum = sum - 1.0 / i;
		}
		else{
			sum = sum + 1.0 / i;
		}
	}
	printf("%f\n", sum);
	system("pause");
	return 0;
}

//9出现的次数

#include <stdio.h>
int main(){
	int num, hundred, ten, unit, count = 0;
	for (num = 1; num <= 100;++num){
		hunderd = num / 100;
		ten = (num - hundred * 100) / 10;
		unit = num - hundred * 100 - ten * 10;
		if (hundred == 9){
			count++;
		}if (ten == 9){
			count++;
		}if (unit == 9){
			count++;
		}
	}
	printf("1到100中9出现的个数:%d\n", count);
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43557235/article/details/83660258