第一场非电机试卷A.doc
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
![preview](https://dl-preview.csdnimg.cn/85323137/0001-531e89b77bcf48a9ef52ef2a858a2597_thumbnail-wide.jpeg)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
这篇文档是针对非电类学生的上机考试试题,包含了两道题目:改错题和编程题,主要涉及C++编程语言的基本操作和算法设计。 **改错题详解:** 题目要求找出在n1和n2(100<n1<n2<1000且n2-n1<=200)之间的所有数,这些数的最高两位组成的整数与该数的各位数字平方和相等。例如,当n1=100,n2=200时,符合条件的数有103和113。提供的代码中存在一处错误,需要修改。原始代码如下: ```cpp if(s=i/10) x[j++]=i; ``` 此处应该将条件改为`s==i/10`,因为我们需要比较的是平方和是否等于最高两位数,而不是赋值。正确的代码应该是: ```cpp if(s==i/10) x[j++]=i; ``` 此外,还需要确保在main函数中正确调用数组,应使用`fun(a, n1, n2)`而非`fun(a[], n1, n2)`。完整修复后的代码如下: ```cpp #include<iostream.h> void fun(int x[], int n1, int n2) { int i, j = 0; for (i = n1; i <= n2; i++) { int s = 0, m = i; while (m) { s += (m % 10) * (m % 10); m /= 10; } if (s == i / 10) x[j++] = i; } } void main() { int i, n1, n2; int a[100]; cout << "请输入起始范围:"; cin >> n1 >> n2; if (n1 < 100 || n1 >= 1000 || n2 - n1 >= 200) return; fun(a, n1, n2); // 修改这里,去掉[] for (i = 0; a[i]; i++) cout << a[i] << '\t'; cout << endl; } ``` **编程题详解:** 这道题目要求编写一个函数`double fun(double x, int n)`来计算斐波那契数列的前n项之和,并且给定了测试数据n=10,x=0.5,期望输出s=0.648015。斐波那契数列的定义是:F0=1, F1=1, Fi=Fi-1+Fi-2(i>1)。函数`fun`应该返回这个级数的和。以下是实现此功能的代码: ```cpp double fib(int n) { if (n <= 0) return 1; else if (n == 1) return 1; else return fib(n - 1) + fib(n - 2); } double fun(double x, int n) { double sum = 0; for (int i = 0; i < n; i++) { sum += fib(i) * pow(x, i); } return sum; } int main() { int n = 10; double x = 0.5; cout << "s=" << fun(x, n) << endl; return 0; } ``` 在这个例子中,我们首先定义了一个辅助函数`fib`用于计算斐波那契数列的第n项,然后在`fun`函数中,通过循环计算并累加级数的每一项。最后在`main`函数中调用`fun`并输出结果。 以上两题都涉及到C++的基础语法,包括输入输出、循环、条件判断、数组操作以及函数调用,同时也考察了算法设计和理解能力。考生需要对这些知识点有扎实的掌握才能正确解答。
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/release/download_crawler_static/85323137/bg1.jpg)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 2534
- 资源: 8万+
![benefits](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-1.c8e153b4.png)
![privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-2.ec46750a.png)
![article](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-3.fc5e5fb6.png)
![course-privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-4.320a6894.png)
![rights](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-icon.fe0226a8.png)
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)