第6章 函数
千城忆梦
这个作者很懒,什么都没留下…
展开
-
第6章 函数 第25题
题目:设计一个基于分治法的函数,找出一组整型数中的最大值。代码:#include <iostream> using namespace std;int divide(int *a, int low, int high, int n);int main(){ cout << "设计一个基于分治法的函数,找出一组整型数中的最大值" << endl &...原创 2018-03-29 17:57:46 · 341 阅读 · 0 评论 -
第6章 函数 第9题
题目:设计一函数,求Fibonacci(n)。代码:1.递归://计算斐波拉契数列#include <iostream>#include<ctime>using namespace std;int a = 0;int function(int n);int main(){ clock_t start, finish; double totaltim...原创 2018-03-28 20:12:31 · 235 阅读 · 0 评论 -
第6章 函数 第12题
题目:编写一个函数,按如下格式打印你的名字。************************** ** 你的名字 ** *************************** 代码:#include <io...原创 2018-03-28 20:30:41 · 238 阅读 · 0 评论 -
第6章 函数 第13题
题目:写一个将英寸转换为厘米的函数(1英寸等于2.54厘米)。代码:#include <iostream>using namespace std;double function(double n);int main(){ double n; cout << "将英寸转换为厘米" << endl << endl; cout <...原创 2018-03-28 20:32:17 · 223 阅读 · 0 评论 -
第6章 函数 第28题
题目:已知华氏温度到摄氏温度的转换公式为C=5/9(F-32)试编写一个将华氏温度转换到摄氏温度的函数。代码:#include <iostream>using namespace std;double function(double f);int main(){ double f; cout << "将华氏温度转换为摄氏温度" << endl ...原创 2018-03-28 21:30:28 · 227 阅读 · 0 评论 -
第6章 函数 第27题
题目:编写一个函数,判断作为参数传入的一个整型数组的内容是否是回文。例如,数组元素值为{1,3,6,8,6,3,1},则是一个回文。代码:#include <iostream>using namespace std;int function(int *a, int length);int main(){ cout << "判断输入的一个整型数组的内容是否是回文...原创 2018-03-28 21:39:03 · 589 阅读 · 0 评论 -
第6章 函数 第26题
题目:写一个函数 bool isEven(int n);,当n的每一位数都是偶数时,返回true,否则返回false。如n的值是1234,函数返回false。如n的值为2484,返回true。用递归和非递归两张方法实现。代码:1.非递归://非递归#include <iostream>using namespace std;bool isEven(int n);int ma...原创 2018-03-28 22:09:47 · 1151 阅读 · 0 评论 -
第6章 函数 第14题
题目:编写一个函数,要求用户输入一个小写字母。如果用户输入的不是小写字母,则要求重新输入,直到输入了一个小写字母。返回此小写字母。代码:#include <iostream>using namespace std;char function();int main(){ cout << function() << endl << endl...原创 2018-03-29 11:18:16 · 495 阅读 · 0 评论 -
第6章 函数 第15题
题目:写三个函数,分别实现对一个双精度数向上取整、向下取整和四舍五入的操作。代码:#include <iostream>using namespace std;int function1(double n); //向上取整int function2(double n); //向下取整int function3(double n); //四舍五入int main...原创 2018-03-29 11:29:00 · 312 阅读 · 0 评论 -
第6章 函数 第11题
题目:设计一函数,在一个M个元素的整型数组中找出第N大的元素(N < M)。代码:#include <iostream>using namespace std;double function(double *a, int M, int N);int main(){ cout << "在一个M个元素的整型数组中找出第N大的元素(N < M)" <...原创 2018-03-29 11:37:29 · 257 阅读 · 0 评论 -
第6章 函数 第10题
题目:设计一个函数,输出小于10000的所有的Fibonacci数。代码:#include <iostream>using namespace std;void function(int n);int main(){ cout << "输出斐波拉契数列中小于n的项" << endl << endl; int n; cout <...原创 2018-03-29 11:45:41 · 249 阅读 · 0 评论 -
第6章 函数 第20题
题目:编写一函数 int count(),使得第一次调用时返回1,第二次调用时返回2。即返回当前的调用次数。代码:1.全局变量#include <iostream>using namespace std;int count();int a = 0; //全局变量int main(){number0: cout << "这是第" << co...原创 2018-03-29 15:33:39 · 619 阅读 · 0 评论 -
第6章 函数 第6题
题目:设计一个用于整型数的二分查找的递归函数。代码:#include <iostream>using namespace std;int search(int a[], int unfound, int left, int right);int main(){ int unfound, left, right, n; cout << "整型数的二分查找(递...原创 2018-03-28 17:13:14 · 250 阅读 · 0 评论 -
第6章 函数 第29题
题目:设计一个递归函数,计算Ackerman函数的值。Ackerman函数定义如下:代码:#include <iostream>#include <ctime>using namespace std;static int a = 0;double A(double m, double n);int main(){ //计时开始 clock_t star...原创 2018-03-28 16:29:14 · 260 阅读 · 0 评论 -
第6章 函数 第17题
题目:编写一个函数reverse,它有一个整型参数和一个整型的返回值。reverse函数返回参数值的逆序值。例如,参数值为12345时,函数返回54321。代码:#include <iostream>using namespace std;int reverse(int n);int main(){ int n; cout << "请输入一个非负整数:";...原创 2018-03-31 17:15:52 · 629 阅读 · 0 评论 -
第6章 函数 第16题
题目:编写一个递归函数reverse,它有一个整型参数。reverse函数按逆序打印出参数的值。例如,参数值为12345时,函数打印出54321。代码:#include <iostream>using namespace std;void reverse(int n);int main(){ int n; cout << "请输入一个非负整数:"; cin...原创 2018-03-31 17:35:24 · 439 阅读 · 0 评论 -
第6章 函数 第8题
题目:输入一个金额,用100、50、20、10、5、1进行换算。(不一定是这几张面额、而且这几张面额的数量也可以有限制。为了简化问题,这里选取了人民币的面额,以及默认每种面额都没有数量限制)代码:1.贪心算法//输入一个金额,用100、50、20、10、5、1进行换算#include <iostream>using namespace std;int main(){ cou...原创 2018-03-31 22:39:26 · 252 阅读 · 0 评论 -
第6章 函数 第18题
题目:编写一个函数模板,判断两个一维数组是否相同。模板参数是数组的类型。代码:#include <iostream>using namespace std;template <class T>bool function(T a[], T b[], int number);int main(){ cout << "判断两个一维数组是否相同" <...原创 2018-03-31 23:34:16 · 275 阅读 · 0 评论 -
第6章 函数 第1题
题目:设计一个函数,判别一个整数是否为素数。代码:#include <iostream>using namespace std;int function(int x);int main(){ int x; cout << "判别一个整数是否为素数" << endl << endl; cout << "请输入一个整数:&q原创 2018-03-27 19:08:53 · 296 阅读 · 0 评论 -
第6章 函数 第2题
题目:设计一个函数,使用以下无穷级数计算sinx的值。sinx=。舍去的绝对值应小于ε,ε的值由用户指定。如果用户不指定ε的值,则假设为10-6。代码://注意:本题中的变量定义最好用long long int和long double,因为极有可能溢出#include <iostream>#include <cmath>#include <iomanip>...原创 2018-03-27 20:07:06 · 1982 阅读 · 0 评论 -
第6章 函数 第3题
题目:写一个函数stringCopy将一个字符串复制到另一个字符串。代码:#include <iostream>#include <string>using namespace std;void stringCopy(string s1);int main(){ string s1; cout << "请输入一个字符串:"; getline...原创 2018-03-27 20:31:17 · 226 阅读 · 0 评论 -
第6章 函数 第4题
题目:设计一个支持整型、实型和字符型的气泡排序的函数模板。代码://冒泡排序(函数模版)//注意:T只能定义有多种可能类型的值,如i,j只能用int#include <iostream>using namespace std;template <class T>T sort(T a);int main(){ const int length = 100...原创 2018-03-27 21:56:23 · 622 阅读 · 0 评论 -
第6章 函数 第5题
题目:设计一函数求两个正整数的最大公约数和最小公倍数。代码:#include <iostream> using namespace std;int divisor(int a, int b, int dis);int main(){ unsigned int a, b, dis = 1, result; cout << "请输入两个正整数:"; cin...原创 2018-03-27 22:04:37 · 219 阅读 · 0 评论 -
第6章 函数 第16题
题目:编写一个递归函数reverse,它有一个整型参数。reverse函数按逆序打印出参数的值。例如,参数值为12345时,函数打印出54321。代码:#include <iostream>#include <string>using namespace std;void reverse(int n);int main(){ int n; cout <...原创 2018-03-28 15:46:13 · 303 阅读 · 0 评论 -
第6章 函数 第24题
题目:创建一个函数Fib。每调用一次就返回Fibonacci序列的下一个值。即第一次调用返回1,第二次调用返回1,第三次调用返回2,第四次调用返回3……代码:1.递归://递归#include <iostream> using namespace std;int function(int n);int main(){ cout << "(每按一次回车调用一...原创 2018-03-29 16:24:36 · 1000 阅读 · 0 评论