C语言初学者-基础的三篇代码-2-
将数组A中的内容和B的交换
#include<stdio.h>
void print_arr(int arr[5], int sz)
{
int i = 0;
for (i = 0; i<sz; i++)
printf("%d\t", arr[i]);
printf("\n");
}
int main()
{
int arr1[] = { 1, 3, 5, 7, 9 };
int arr2[] = { 2, 4, 6, 8, 0 };
int tmp = 0;
int i = 0;
for (i = 0; i<5; i++)
{
tmp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = tmp;
}
print_arr(arr1, 5);
print_arr(arr2, 5);
system("pause");
return 0;
}
运行结果
计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int i = 0;
double sum = 0;
int flag = 1;
for (i = 1; i <= 100; i++)
{
sum += flag*1.0 / i;
flag = -flag;
}
printf("%lf\n", sum);
return 0;
}
运行结果
前 100 的所有整数中出现多少次数字9
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int i = 0;
int count = 0;
for (i = 1; i <= 100; i++)
{
if (9 == i % 10)
count++;
if (9 == i / 10)
count++;
}
printf("count = %d\n", count);
system("pause");
return 0;
}
运行结果