最後一題寫 void sort(int x[], int y)
我寫的冒泡排序,課本上還有快速排序
void sort(int x[], int y)
{
for(int i = 0; i <= y - 1; ++i)
for(int j = i + 1; j <= y - 1; ++j)
if(x[i] > x[j])
{
int t = x[i];
x[i] = x[j];
x[j] = t;
}
}
倒數第二題計算e的近似值
這題稍有點那啥,如果你的計算階乘函數返回值是int,又沒有其他措施,
1/大於1的整數 永遠爲0,就不對了
#include <iostream>
#include <cstdio>
using namespace std;
double fac(double n)
{
double ans = 1;
for(int i = 2; i <= n; ++i)
ans *= i;
return ans;
}
int main()
{
double sum = 1;
double n;
scanf("%lf", &n);
for(int i = 1; i <= n; ++i)
sum += 1 / fac(double(i));
printf("%f", sum);
return 0;
}
某個寫程序結果的題
#include <iostream>
#include <cstdio>
using namespace std;
int n = 5;
int fun()
{
static int n = 0;
int s = 1;
s += n;
n++;
return s;
}
int main()
{
int sum = 0;
for(int i = 0; i < n; ++i)
sum += fun();
cout << sum << '\n';
return 0;
}
答案
我做的時候也很懵,全局變量不能覆蓋靜態局部變量(static局部變量),但自動變量(auto)應該會被覆蓋
某個選擇
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int a[][3] = {0, 0};
for(int j = 0; j < 3; ++j)
cout << a[0][j] << ' ';
return 0;
}
結果
我(現在)覺得只有一維(考試時並不是這麼覺得的),每個都被賦值0了
課本也有敘述