1.交换俩个数组内容(内容一样)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr1[] = { 1, 2, 3, 4, 5 };
int arr2[] = { 0, 2, 4, 6, 8 };
int tmp=0;
int i = 0;
int sz = sizeof(arr1) / sizeof(arr1[0]);
for (i = 0; i < sz; i++)
{
tmp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = tmp;//数组每个元素逐一交换
}
for (i = 0; i < sz; i++)//遍历数组
{
printf("%d ", arr1[i]);//打印第一个数组
}
printf("\n");
for (i = 0; i < sz; i++)
{
printf("%d ", arr2[i]);
}
printf("\n");
system("pause");
return 0;
}
2.计算1/1-1/2+1/3-1/4+1/5.....+1/99-1/100的值
方法一:#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0;
double a, sum = 0.0;
for (i = 1; i <= 50; i++)
{
a = ((1.0) / (2 * i - 1)) - ((1.0) / (2 * i));
sum = sum + a;
}
printf("%lf\n", sum);
system("pause");
return 0;
}
方法二:#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0;
double sum = 0.0;
int flag = 1;
for (i = 1; i <= 100; i++)
{
sum = sum + flag*1.0/i;
flag = -flag;
}
printf("%lf\n", sum);
system("pause");
return 0;
}
3.数1-100所有整数中出现多少次数字9
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n = 1, count = 0;
while (n <= 100)
{
if (n % 10 == 9)
count++;
if (n / 10 == 9)
count++;
n++;
}
printf("9出现的次数:%d\n", count);
system("pause");
return 0;
}