题目内容

写出下面程序执行后的运行结果。#include< iostream>using namespace std;#define DEBUGvoid main(){ int a = 20, b =10, c; c = a / b;#ifdef DEBUG cout << a << "/" << b << "=";#endif cout << c;}

查看答案
更多问题

例3-2 数制转换输入一个8位二进制数,将其转换为十进制数输出。例如:从键盘输入110111012=1×23+1×22+0×21+1×20=1310所以,程序应输出13源代码:#include using namespace std;double power (double x, int n); //计算x的n次方int main() { int value = 0; cout << "Enter an 8 bit binary number "; for (int i = 7; i >= 0; i--) { char ch; cin >> ch; if (ch == '1') value += static_cast(power(2, i)); } cout << "Decimal value is " << value << endl; return 0;}double power (double x, int n) { double val = 1.0; while (n--) val *= x; return val;}试问:1、运行结果输入01101000,则power函数执行( )次?2、如果输入00111110,power函数又执行( )次?

例3-3(书上)#includeusingnamespace std;doublearctan(double x) { doublesqr = x * x; doublee = x; doubler = 0; inti = 1; while(e / i > 1e-15) { doublef = e / i; r= (i % 4 == 1) ? r + f : r - f; e= e * sqr; i+= 2; } returnr;}主程序intmain() { doublea = 16.0 * arctan(1/5.0); doubleb = 4.0 * arctan(1/239.0); //注意:因为整数相除结果取整,如果参数写1/5,1/239,结果就都是0 cout<< "PI = " << a - b << endl; return0;}试问:(1)执行程序中的arctan()函数时传入实参为1/ 5.0,那么arctan()函数体中while循环的循环体最后一次被执行后,i的值为( )? (2)如果传入实参为1/ 500.0,那么arctan()函数体中while循环的循环体最后一次被执行后,i的值又为( )?

例3-4(书上)#include using namespace std;//判断n是否为回文数bool symm(unsigned n) { unsigned i = n; unsigned m = 0; while (i > 0) { m = m * 10 + i % 10; i /= 10; } return m == n;}int main() { for (unsigned m = 11; m < 1000; m++) if (symm(m) && symm(m * m) && symm(m * m * m)) { cout << "m = " << m; cout << " m * m = " << m * m; cout << " m * m * m = " << m * m * m << endl; } return 0;}试问:(1)在symm()函数中,当输入参数为523时,while循环体内容执行( )次;(2)在symm()函数中,当输入参数为1523时,while循环体内容执行( )次;

例3-9(书上)#include using namespace std;//计算从n个人里选k个人的组合数int comm(int n, int k) { if (k > n)return 0; else if (n == k || k == 0)return 1; elsereturn comm(n - 1, k) + comm(n - 1, k - 1);}int main() { int n, k; cout << "Please enter two integers n and k: "; cin >> n >> k; cout << "C(n, k) = " << comm(n, k) << endl; return 0;}试问:(1)comm函数输入n=9,k=5时的返回值是( )?(2)输入n=7,k=3时的返回值是( )?

答案查题题库