|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
a) What is the exact output of the code below if we execute the function call q2(20)? 如果执行函数调用q2(20),下面代码的确切输出是什么?
void q2(int n)
{
for (int i = 1; i < n; i *= 2)
cout << i;
}
b) The function in part (a) uses a for loop. Rewrite the function using a while loop without changing how the code works。(a)部分中的函数使用for循环。 使用while循环重写函数,而无需更改代码的工作方式。
c) Rewrite the function from part (a) using a do-while loop. (Hint: make sure the code produces the same output for all cases). 使用do-while循环重写部分(a)中的函数。 (提示:请确保代码在所有情况下都产生相同的输出)。
本帖最后由 xieglt 于 2020-11-18 09:11 编辑
a)q2(20)输出:124816
b)
- void q2(int n)
- {
- int i = 1;
- while(i<n)
- {
- cout << i;
- i*=2;
- }
- }
复制代码
c)
- void q2(int n)
- {
- int i = 1;
- do
- {
- cout << i;
- i*=2;
- }while(i<n);
- }
复制代码
|
|