本页面包含课程例题、课后拓展5关、代码填空4关的完整答案与家长辅导建议。
请输入本讲访问密码:
课程辅导解析(家长端) · 含全部例题、拓展、代码填空答案
#include <iostream>
using namespace std;
int main() {
double b;
cout << sizeof(char) << endl; // 输出 1
cout << sizeof(b) << endl; // 输出 8(double)
cout << sizeof(5) << endl; // 输出 4(整数字面量是int)
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a[10];
cout << sizeof(a[0]) << endl; // 4(单个int元素)
cout << sizeof(a) << endl; // 40(整个数组)
cout << sizeof(a) / sizeof(a[0]); // 10(元素个数=长度)
return 0;
}
#include <iostream>
#include <bitset>
using namespace std;
int main() {
cout << bitset<8>(8) << endl; // 00001000
cout << bitset<8>(-8) << endl; // 11111000
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a = 3;
int b = a << 5; // 3 × 2^5 = 96
int c = a >> 3; // 3 ÷ 2^3 = 0(取整)
cout << b << endl; // 96
cout << c << endl; // 0
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n; cin >> n;
int cnt = 0;
while (n > 0) {
if (n & 1) cnt++; // ★ 最低位是1就计数
n = n >> 1; // ★ 右移一位,看下一位
}
cout << cnt << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
cout << sizeof(int) << endl; // 4
cout << sizeof(double) << endl; // 8
cout << sizeof(char) << endl; // 1
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a[10];
cout << sizeof(a) << endl; // 40
cout << sizeof(a[0]) << endl; // 4
cout << sizeof(a) / sizeof(a[0]); // 10
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a = 3;
int b = a << 5; // ★ 左移5位
cout << b << endl; // 96
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a = 67;
int c = a >> 4; // ★ 右移4位
cout << c << endl; // 4
return 0;
}