DAY 3
1. 將數組A中的內容和數組B中的內容進行交換。(數組一樣大)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5] = { 1, 2, 3, 4, 5 };
int b[5] = { 5, 6, 7, 8, 9 };
int i;
for (i = 0; i < 5; i++) //數組遍歷
{
printf("%d ", a[i]);
}
putchar('\n');
for (i = 0; i < 5; i++) //數組遍歷
{
printf("%d ", b[i]);
}
putchar('\n');
int tmp;
for (i = 0; i < 5; i++) //數組遍歷
{
tmp = a[i];
a[i] = b[i];
b[i] = tmp;
}
for (i = 0; i < 5; i++) //數組遍歷
{
printf("%d ", a[i]);
}
putchar('\n');
for (i = 0; i < 5; i++) //數組遍歷
{
printf("%d ", b[i]);
}
putchar('\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;
double sum = 0;
double tmp = 0;
int flag = 1;
for (i = 1; i <= 100; i++)
{
tmp = flag * 1.0 / i;
flag *= -1;
sum += tmp;
}
printf("%.4lf\n", sum);
system("pause");
return 0;
}
輸出結果:
3. 編寫程序數一下 1到 100 的所有整數中出現多少次數字9。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int count = 0;
for (i = 0; i <= 100; i++)
{
if (i % 10 == 9)
{
count++;
}
if (i / 10 == 9)
{
count++;
}
}
printf("%d", count);
system("pause");
return 0;
}
輸出結果: