




已阅读5页,还剩77页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C 基础题 100 题 第一部分 1 显示 Hello Worle 编写 C 程序 在屏幕上显示 Hello World include int main using namespace std cout Hello World endl return 0 2 显示唐诗 编写 C 程序 在屏幕上显示下列唐诗 慈母手中线 游子身上衣 临行密密缝 意恐迟迟归 谁言寸草心 报得三春晖 include int main using namespace std cout 慈母手中线 n 游子身上衣 n临行密密缝 n意恐迟迟归 n 谁言寸草心 n 报得三春晖 endl return 0 3 显示一句话 编写 C 程序 输入姓名 在屏幕上显示如下格式的文字 This program is coded by 其中 是输入的名字 如输入 ZhangSan 则显示 This program is coded by ZhangSan 注意 姓名中间没有空格 末尾有英文句号 include int main using namespace std char name 50 cin name cout This program is coded by name endl return 0 4 还是一句话 编写 C 程序 输入姓名 在屏幕上显示如下格式的文字 This program is coded by 其中 是输入的名字 如输入 Zhang San 则显示 This program is coded by Zhang San 注意 姓名中间可能有空格 末尾有英文句号 include int main using namespace std char name 50 cin getline name 49 cout This program is coded by name endl getchar return 0 5 计算矩形周长 输入矩形的两个边的长度 计算矩形的周长 include int main using namespace std int a b c cin a b c a b 2 cout c endl return 0 6 已知直角边求斜边 输入一个三角形的两个直角边的长度 求其斜边的长度 计算公式是 c sqrt a a b b 其中 a b 是两个直角边的长度 c 是斜边 sqrt 表示开平方 include include int main using namespace std double a b c cin a b c sqrt a a b b cout c endl return 0 第二部分 1 求过平面上两点的直线的斜率 编写程序 输入平面上的两个点的坐标 x1 y1 x2 y2 求过这两点的直线的斜率 设斜率不为无穷 提示 数据类型都用 double include int main using namespace std double x1 y1 x2 y2 double k cin x1 y1 x2 y2 k y2 y1 x2 x1 cout k endl return 0 2 计算平面上两点之间的距离 编写程序 输入平面上的两个点的坐标 x1 y1 x2 y2 计算这两点之间的距离 提示 数据类型用 double 包含头文件 cmath 计算公式 distance x2 x1 x2 x1 y2 y1 y2 y1 distance sqrt distance include include int main using namespace std double x1 y1 x2 y2 double k cin x1 y1 x2 y2 k x2 x1 x2 x1 y2 y1 y2 y1 k sqrt k cout k endl return 0 3 判断大小写 输入一个英文字母 判断大小写 大写输出 1 小写输出 0 include int main using namespace std char c cin c if c A else cout 0 include int main using namespace std char c cin c if c 0 else cout 0 return 0 5 判断闰年 编写程序 输入年份 判断是否闰年 是 输出 IsLeapYear 否 输出 NotLeapYear include int main using namespace std int year cin year if year 4 0 else cout NotLeapYear endl return 0 6 求商和余数 输入两个正整数 求它们的商和余数 例如 输入 18 和 10 则它们的商是 1 余数是 8 提示 使用 int 类型 用来求商 用来求余数 include int main using namespace std int a b cin a b cout a b a b endl return 0 7 计算平均分取整 某招聘面试 7 个专家给考生打分 编写程序 计算 7 个专家给分的平均分 取整 提示 四舍五入 y int x 0 5 include using namespace std int main double a sum 0 0 double average for int i 0 i a sum a average sum 7 average int average 0 5 cout average endl return 0 8 计算点到直线的距离保留两位小数 直线方程 Ax By C 0 编写程序 输入 A B C 和点 x y 计算该点到直线的距离 点到直线的距离公式 d Ax By C sqrt A A B B 其中 z 表示绝对值 程序中使用条件表达式 如 z 0 z z z z include include using namespace std int main double A B C D x y z cin A B C cin x y z A x B y C z 0 z z z z D z sqrt A A B B D int D 100 0 5 100 00 cout D endl return 0 9 输入字符显示 ASCII 值 编写程序 输入一个字符 显示其 ASCII 值 如输入 A 显示 65 输入 a 显示 97 include using namespace std int main char ch ch cin get cout ch 0 endl return 0 10 输入整数显示 ASCII 字符 编写程序 输入一个 0 127 之间的整数 显示对应的 ASCII 字符 提示 若 k 是整数 将它赋值给字符变量或使用 char k 就得到字符 注意 有些字符是不能在屏幕上显示 出来的 include using namespace std int main int ch cin ch cout char ch endl return 0 11 输入整数显示十六进制 编写程序 输入一个非负整数 显示其十六进制形式 如输入 31 输出 1f include using namespace std int main unsigned int ch cin ch cout hex ch endl return 0 12 输入整数显示十六进制和八进制 编写程序 输入整数 显示其十进制 十六进制和八进制形式 如输入 31 输出 31 1f 37 include using namespace std int main int n l cin n n 0 l n l n if n 0 cout l hex l oct l endl else cout l hex l oct l endl return 0 第三部分 1 判断奇偶数 编写程序 输入正整数 判断是奇数还是偶数 是奇数显示 odd 是偶数显示 even 输入 1 显示 odd include using namespace std int main int i cin i if i 2 0 cout even endl else cout odd endl return 0 2 判断数的类型 编写程序 输入实数 判断输入的数据是正实数 负实数 正整数 负整数 还是零 分别显示 positive real negative real positive integer negative integer zero 注意 两个单词的中间有一个空格 include using namespace std int main double i 使用 int 只对一半 cin i if i 1e 10 if int i i cout positive integer endl else cout positive real endl else if i 1e 10 if int i i cout negative integer endl else cout negative real endl else cout zero endl return 0 3 判断点的象限 编写程序 输入平面直角坐标的 x y 值 判断点在哪个象限 不考虑在坐标轴上的情况 分别输出 1 2 3 或 4 include using namespace std int main double i j 使用 int 只对一半 cin i j if i 0 else if i 0 cout 2 endl else if i 0 else cout 4 endl return 0 4 判断字符类型 编写程序 输入一个字符 判断其是数字 大写字母 小写字母还是其他 分别显示 0 1 2 或 1 include using namespace std int main char ch ch cin get if ch A else cout 1 endl return 0 5 百分制成绩转五分制成绩 编写程序 输入百分制的分数 非负整数 将其转换为 5 分制成绩 成绩对应关系如下 90 100 5 80 89 4 70 79 3 60 69 2 10 59 1 0 9 0 include using namespace std int main int i j cin i j i 10 switch j case 10 case 9 cout 5 endl break case 8 cout 4 endl break case 7 cout 3 endl break case 6 cout 2 endl break case 5 case 4 case 3 case 2 case 1 cout 1 endl break default cout 0 endl break return 0 6 显示 n 个字符 编写程序 输如正整数 n 和字符 c 在一行输出 n 个字符 c 如输入 10 显示 include using namespace std int main int k char ch cin k ch for int i 0 i k i cout ch return 0 7 显示字符组成的矩形 编写程序 输入行数 n 列数 m 和一个字符 c 显示由字符 c 组成的 n 行 m 的矩形 如输入 5 10 输出 include using namespace std int main int k l char ch cin k l ch for int i 0 i k i for int j 0 j l j cout ch cout endl return 0 8 用循环计算 1 2 3 n 编写程序 输入非负整数 n 计算 s 1 2 3 n 的值 要求使用循环 而不是使用公式 include using namespace std int main unsigned int n s 0 cin n for unsigned int i 1 i n i s i cout s endl return 0 9 计算 1 1 2 1 3 1 n 编写程序 输入非负整数 n 计算 s 1 1 2 1 3 1 n 的值 输入 0 时 输出 0 输入 非负整数 n 输出 级数的前 n 项和 提示 1 n 应写成 1 0 n 和应为 double 型 请自己分别使用 for 和 while 实现 include using namespace std int main int n i 1 double s 0 0 cin n while i n s 1 0 i i cout s endl return 0 10 计算 n 编写程序 输入非负整数 n 计算 n 0 1 include using namespace std int main unsigned int n s 1 cin n if n 0 n 1 for unsigned int i 1 i n i s i else s 1 cout s endl return 0 11 交替输出 1 和 1 编写程序 输入正整数 n 从 1 开始交替输出 n 个 1 和 1 如输入 3 输出 1 1 1 输入 4 输出 1 1 1 1 数据间用一个空格隔开 include using namespace std int main int m n 1 cin m for int i 1 i m i if i 2 1 cout n if i 2 0 cout n if i m cout return 0 12 判断整数的位数 编写程序 输入非负整数 判断整数的位数 如输入 12 输出 2 include using namespace std int main unsigned int count 1 int n cin n for int i 0 i 10 i n n 10 count cout count endl return 0 13 求非负数整数的各位数字的 编写程序 输入非负整数 输出其各位数字的和 如输入 1234 输出 10 include using namespace std int main unsigned int sum 0 n cin n while n sum sum n 10 n 10 cout sum endl return 0 14 九九乘法表 编写程序 显示如下的 n 行的九九乘法表 如输入 5 显示的乘法表如下 1 1 1 2 1 2 2 2 4 3 1 3 3 2 6 3 3 9 4 1 4 4 2 8 4 3 12 4 4 16 5 1 5 5 2 10 5 3 15 5 4 20 5 5 25 include using namespace std int main unsigned n sum 0 cin n for unsigned int i 1 i n i for unsigned int j 1 j i j sum i j cout i j sum if j i cout cout n return 0 15 不一样的九九乘法表 编写程序 显示如下的 n 行的九九乘法表 如输入 5 显示的乘法表如下 5 1 5 5 2 10 5 3 15 5 4 20 5 5 25 4 1 4 4 2 8 4 3 12 4 4 16 3 1 3 3 2 6 3 3 9 2 1 2 2 2 4 1 1 1 include using namespace std int main unsigned n sum 0 cin n for unsigned int i n i 0 i for unsigned int j 1 j i j sum i j cout i j sum if j i cout cout n return 0 16 Fibonacci 序列 编写程序 显示 Fibonacci 序列的前 n 项 从 0 开始 F 0 0 F 1 1 F n F n 1 F n 2 include using namespace std int function int n int first 0 int second 1 int sum 0 if n 1 return n 1 1 0 for int i 2 i n for int i 0 i n i cout function i if i n cout return 0 第四部分 1 数组元素反序输出 编写程序 先输入 n 再输入 n 个整数 按相反的顺序输出这 n 个整数 如输入 5 个整数 1 2 3 4 5 输 出为 5 4 3 2 1 数据个数不超过 100 个 include using namespace std int main int n int arr 100 0 cin n for int i 0 i arr i for int i n 1 i 0 i if i 0 cout arr i else cout arr i return 0 2 求数组元素最大值 编写程序 输入若干整数到一维数组中 输入 9999 时表示结束 求数组元素的最大值 数组元素个数不 超过 100 输入数据不包含 9999 include include using namespace std int main int arr 99 0 int k n INT MIN for int i 0 i k if k 9999 break else arr i k for int i 0 i n n arr i cout n endl return 0 3 数组指定区间的元素的最大 最小 总和和平均值 有固定数组 编写程序 输入起始下标 i 和终止下标 j 求下标在 i j 之间的元素的最大值 最小值 总和和 平均值 平均值为 double 数 组 元 素 为 1 15 40 180 99 122 124 27 192 128 165 95 161 138 183 51 107 39 184 113 63 9 107 188 11 13 151 52 7 6 元素个数为 30 个 include include using namespace std int main unsigned i j int MAX INT MIN MIN INT MAX count sum 0 double average 0 00 int arr 1 15 40 180 99 122 124 27 192 128 165 95 161 138 183 51 107 39 184 113 63 9 107 188 11 13 151 52 7 6 cin i j if i j average MAX MIN sum 0 else for count 0 i MAX MAX arr i if arr i MIN MIN arr i sum arr i count average double sum count cout MAX MIN sum average endl return 0 4 求矩阵每行元素最大值 编写程序 输入行数 n 列数 m 然后输入 n 行 m 列的矩阵 求矩阵每行元素的最大值 行数 列数在 1 20 之间 矩阵元素为整数 include include using namespace std int main unsigned n m int max cin n m int arr n m 在 G 下 int arr 20 20 0 在 VC 下 for int i 0 i n i for int j 0 j arr i j for int i 0 i n i max INT MIN for int j 0 j max max arr i j cout max endl return 0 5 求矩阵每列元素最大值 编写程序 输入行数 n 列数 m 然后输入 n 行 m 列的矩阵 求矩阵每列元素的最大值 行数 列数在 1 20 之间 矩阵元素为整数 include include using namespace std int main unsigned n m int max cin n m int arr n m for int i 0 i n i for int j 0 j arr i j for int i 0 i m i max INT MIN for int j 0 j max max arr j i if i m 1 cout max else cout max return 0 6 计算向量的和 编写程序 输入 n 然后输入两个 n 维向量 计算并输出它们的和 例如输入 4 然后输入两个 4 维向量 1 2 3 4 5 6 7 8 它们的和就是对应元素相加 结果为 6 8 10 12 设向量的元素为整数 维数不超过 100 include using namespace std int main int n int arr1 100 arr2 100 cin n for int i 0 i arr1 i for int i 0 i arr2 i for int i 0 i n i if i n 1 cout arr1 i arr2 i else cout arr1 i arr2 i return 0 7 矩阵向量的内积 编写程序 输入 n 然后输入两个 n 维向量 计算并输出它们的内积 点积 数量积 inner product dot product scalar product 例如输入 4 然后输入两个 4 维向量 1 2 3 4 5 6 7 8 它们内积就是对应元素相 乘 求和 结果为 70 设向量的元素为整数 维数不超过 100 include using namespace std int main int n sum 0 int arr1 100 arr2 100 cin n for int i 0 i arr1 i for int i 0 i arr2 i for int i 0 i n i sum arr1 i arr2 i cout sum return 0 8 计算向量的范数 这里的范数指欧几里得范数 2 范数 是向量元素的平方再求和 然后开平方 计算公式为 编写程序 输入 n 然后输入一个 n 维向量 计算它的 2 范数 例如输入 4 然后输入一个 4 维向量 1 2 3 4 它的 2 范数为 设向量的元素为 double 维数不超过 100 include include using namespace std int main int n cin n double sum 0 0 arr n for int i 0 i arr i sum arr i arr i cout sqrt sum endl 9 计算向量的欧氏距离 欧几里得度量 euclidean metric 也称欧氏距离 指在 n 维空间中两个点之间的真实距离 在二维和三 维空间中的欧氏距离就是两点之间的实际距离 n 维空间两点间的欧氏距离计算公式为 编写程序 输入 n 然后输入两个 n 维向量 计算并输出它们的欧氏距离 例如输入 4 然后输入两个 4 维 向量 1 2 3 4 5 6 7 8 它们内积就是对应元素相乘 求和 结果为 70 设向量的元素为实数 维数不超过 100 include include using namespace std int main double n arr1 100 arr2 100 double m ans 0 cin n for int j 1 j arr1 j for int j 1 j arr2 j for int j 1 j n j ans arr1 j arr2 j arr1 j arr2 j m sqrt ans cout m endl return 0 10 矩阵求和 n m 矩阵的和 是对应元素的和 得到的还是还是 n m 的矩阵 编写程序 输入矩阵的行数 n 和列数 n 然后输入两个 n m 的矩阵 计算并输出它们的和 include using namespace std int main unsigned n m double arr1 100 100 0 arr2 100 100 0 arr3 100 100 0 cin n m for unsigned i 0 i n i for unsigned j 0 j arr1 i j for unsigned i 0 i n i for unsigned j 0 j arr2 i j for unsigned i 0 i n i for unsigned j 0 j m j arr3 i j arr1 i j arr2 i j for unsigned i 0 i n i for unsigned j 0 j m j if j m 1 cout arr3 i j else cout arr3 i j cout endl return 0 11 输入字符串 求长度 10 分 编写程序 输入可能带空格的字符串 计算并输入其长度 即字符个数 含空格数 字符串最大长度不超 过 100 使用字符数组实现 不使用字符串库函数 include include using namespace std int main string str getline cin str cout str length endl return 0 include using namespace std int main char ch 100 int count 0 cin getline ch 99 for int i 0 ch i 0 i count cout count return 0 12 统计字符串中大写字母的数量 编写程序 输入字符串 含空格 统计其中大写字母的数量 字符串最大长度不超过 200 使用字符数组 实现 不使用字符串库函数 include include using namespace std int main char ch 100 int count 0 cin getline ch 99 for int i 0 ch i 0 i if isupper ch i count cout count return 0 13 复制字符串 编写程序 输入字符串 不含空格 保存在字符数组 s1 中 再将 s1 中的复制到字符数组 s2 中 输出 s2 使用字符数组实现 不使用字符串库函数 字符串最大长度不超过 100 注意 本题的输入和输出是一样的 关键是有没有实现复制 提示 逐个赋值 s1 i 给 s2 i 直到遇到结束符 特别注意在 s2 末尾加结束符 0 include include using namespace std int main string str1 str2 getline cin str1 str2 str1 cout str2 endl char ch1 50 ch2 50 cin getline ch1 49 for int i 0 ch1 i 0 i ch2 i ch1 i ch2 i 1 0 cout ch2 endl return 0 14 字符串逆序 编写程序 输入字符串 不含空格 保存在字符数组 s 中 将 s 中的字符逆序 仍输出 s 使用字符数组 实现 不使用字符串库函数 字符串最大长度不超过 100 include include using namespace std int main string str cin str for int i str length 1 i 0 i cout str i return 0 15 定义表示平面坐标点的结构体计算两点距离 在主函数外定义表示平面坐标点的结构体 在主函数中用该结构体定义两个坐标点 输入两点的坐标值 计算并输出两点之间的距离 提示 参考程序如下 需要的同学可以参考 此题直接抄提示代码就行 第五部分 1 求两个数的和 编写函数 求两个实数的和 编写主函数 输入两个数 调用函数求和 在主函数中显示结果 建议函数 名 mysum include using namespace std double mysum double a double b double sum a b return sum int main double a b cin a b cout mysum a b return 0 2 求绝对值的函数 编写函数 求实数 x 的绝对值 在主函数中输入实数 x 调用函数求绝对值 在主函数中输出 建议函数 名 myfabs 注意 不用系统库函数 自己实现 返回绝对值 在主函数中输出 include using namespace std double myfabs double a return a 0 a 0 a int main double a cin a cout myfabs a return 0 3 x 的 k 次方 编写函数 求 x 的 k 次方 k 为整数 可正 可负 可 0 0 的任何次方为 0 任何非 0 数的 0 次方为 1 编写主函数 输入 x 和 k 调用函数计算 x 的 k 次方 在主函数中输出 结果为实数 建议函数名 mypow 注意 用函数实现 使用循环连乘或连除 不使用系统的库函数 pow include using namespace std double mypow double a int b double pow 1 00 if a 0 return 0 if b 0 return 1 else if b 0 b 0 b while b pow pow a pow 1 0 pow return pow else for int i 0 i a b cout mypow a b return 0 4 求 n 的函数 编写函数 求 n 0 1 编写主函数 输入 n 调用函数求阶乘 在主函数中输出结果 注意 必须用函数实现 在主函数中输出结果 函数中不能有 cout printf 等屏幕输出 include using namespace std int factorial int b long sum 1 if b 0 return 1 else for int i 1 i a cout factorial a return 0 5 输入数组元素 编写函数 输入数组元素 输入 9999 表示 结束 返回数组元素的个数 在主函数中定义数组 数组大小 足够大 满足问题规模要求 本题 100 即可 调用函数输入数组元素 在主函数中逆序输出数组元素 数 组元素为整型 提示 形参为数组 实参为数组名 在函数中添加数组元素 元素自然返回 函数应使用 return 返回实 际输入的元素个数 include using namespace std int function int arr int p arr int count 0 while p count return count int main int a 100 0 int k for int i 0 i a i if a i 9999 break k function a for int i k 2 i 0 i if i 0 cout a i else cout a i return 0 6 输出数组元素值 编写函数 输出数组元素 用一个空格隔开 末尾无空格 在主函数中定义数组 数组大小足够大 满足 问题规模要求 本题 100 即可 调用上题函数输入数组元素 调用本题函数输出数组元素 数组元素为 整型 注意 要用函数实现 include using namespace std void PrintArray int arr int count 0 for int i 0 i arr i if arr i 9999 arr i 0 break count for int i 0 i count i if i count 1 cout arr i endl else cout arr i int main int arr 100 0 PrintArray arr return 0 7 将数组元素逆序 编写函数 将数组元素逆序 编写主函数 定义数组 使用前面编写的函数输入数组元素 调用本题函数 逆序数组元素 调用前面编写的输出数组的函数输出 设数组元素为整数 不超过 100 个 include using namespace std void PrintArray int arr int count 0 while arr count 0 count for int i count 2 i 0 i if i 0 cout arr i else cout arr i int main int a 100 0 for int i 0 i a i if a i 9999 break PrintArray a return 0 8 求数组元素的和 编写函数 求数组元素的和 编写主函数 定义数组 使用前面编写的函数输入数组元素 调用本题函数 求和 在主函数中输出和 数组元素为整数 不超过 100 个 include using namespace std void ArraySum int arr int count 0 sum 0 while arr count 0 count for int i 0 i count 1 i sum arr i cout sum int main int a 100 0 for int i 0 i a i if a i 9999 break ArraySum a return 0 9 求字符串的长度的函数 编写函数 计算字符串的长度 编写主函数 定义字符数组 输入字符串 调用函数求长度 在主函数中 输出字符串的长度 字符串最大长度不超过 100 注意 必须用函数实现 不能使用字符串处理的库函数 不能使用 string 类型 include using namespace std int arrCount char arr int count 0 while arr count 0 count return count int main char str 100 0 cin get str 99 cout arrCount str return 0 10 字符串转大写 编写函数 将字符串中的所有字母转换为大写 在主函数中定义字符数组 输出字符串 调用函数转大写 在主函数中输出 字符串长度不超过 200 注意 必须用字符数组实现 不能使用字符串处理库函数 也不 能使用 string 类 include using namespace std char upCase char arr int count 0 while arr count 0 count for int i 0 i 97 return arr int main char str 200 0 cin getline str 199 cout upCase str endl return 0 11 字符串复制函数 编写函数 将字符串 s1 复制到字符串 s2 中 函数格式为 void mystrcpy char s1 char s2 编写主函数 定义字符数组 s1 100 s2 100 输入字符串 s1 调用函数将 s1 的内容复制到 s2 中 在主函 数中输出 s2 的内容 要求必须用字符数组实现 不能使用系统的库函数 不能使用 string 类型 include using namespace std void mystrcpy char s1 char s2 int count 0 while s1 count 0 count for int i 0 i count i s2 i s1 i s2 count 0 cout s2 s2 返回 1 若 s1 s2 返回 1 若 s1 等于 s2 返回 0 在主函数中定义字符数组 输入字符串 调用函数进行比较 在主函数中输出结果 字符串的比较就是比较它们在字母表中的顺序 如 a 在 b 前 就是 a b 实际上就是比较它们的 ASCII 码 注意 必须用字符数组实现 不能使用字符串处理库函数 也不能使用 string 类 字符串的长度不超 过 200 include include using namespace std int CompareElements char s1 char s2 assert NULL s1 断言 while s1 s2 if unsigned char s1 unsigned char s2 return 1 else if unsigned char s1 s1 s2 cout CompareElements s1 s2 return 0 第六部分 1 递归计算 n 编写递归函数 计算 n 在主函数中输入 n 调用函数计算 n 在主函数中输出 n 注意 应用递归函数实现 否则没有意义 include using namespace std int Recursion int n if n 1 n 0 return 1 else return Recursion n 1 n int main int a cin a cout Recursion a return 0 2 递归计算 1 2 3 n 编写递归函数 计算 1 2 3 n 在主函数中输入 n 调用函数计算 1 到 n 的和 在主函数中输出和 注意 应用递归函数实现 否则没有意义 输入 0 时 和为 0 include using namespace std int Recursion int n if n 0 return 0 else return Recursion n 1 n int main int a cin a cout Recursion a return 0 3 递归求数组元素的最大值 编写递归函数 求数组元素的最大值 函数的输入参数为数组和元素个数 返回最大值 在主函数中输入 元素个数和数组元素 调用函数求最大值 在主函数中输出结果 设数组类型为整型 元素不超过 100 个 注意 使用递归 否则没有意义 include using namespace std int iMax int arr int n if n arr n 1 tmp arr n 1 int main int a 100 int n cin n for int i 0 i a i cout iMax a n return 0 4 递归求数组元素的和 10 分 编写递归函数 求数组元素的和 函数的输入参数为数组和元素个数 返回最大值 在主函数中输入元素 个数和数组元素 调用函数求和 在主函数中输出结果 设数组类型为整型 元素不超过 100 个 提示 元素个数为 0 时返回和是 0 include using namespace std int iSum int arr int n if n n for int i 0 i a i cout iSum a n return 0 5 递归求 Fibonacci 序列的第 n 项 10 分 Fibonacci 费波那契 序列 f 0 0 f 1 1 f n f n 1 f n 2 n 2 3 4 编写递归函数 计算 Fibonacci 序列的第 n 项 n 0 1 2 3 在主函数中输入 n 调用函数计算 Fibonacci 第 n 项 在主函数中输出结果 注意 应用递归函数实现 include using namespace std int Fibonacci int n if n n cout Fibonacci n return 0 6 递归逆序数组元素 编写递归函数 将数组元素逆序 函数的输入参数是数组 起始下标和元素个数 在主函数中输入元素个 数和数组元素 调用函数逆序 在主函数中输出结果 设数组类型为整型 元素不超过 100 个 提示 本函数不需返回值 注意 必须用递归函数实现 否则没有意义 include using namespace std void ReverseArray int arr int first int end int lenth if lenth end 2 int tmp arr first arr first arr lenth 1 arr lenth 1 tmp ReverseArray arr first 1 end lenth 1 void Show int arr int lenth for int i 0 i lenth i if i lenth 1 cout arr i else cout arr i len for int i 0 i arr i end len ReverseArray arr first end len Show arr len return 0 7 输入输出数组元素的函数重载 函数重载就是定义函数名相同 参数类型或参数个数不同的函数 这些函数称为重载函数 具有重载关系 的函数一般具有相同或相似的功能 但作用的对象不同 编写输入数组元素的重载函数 数组类型分别为 int 和 double 输入 9999 时结束输入 返回输入的元素 个数 编写输出数组元素的重载函数 数组类型分别为 int 和 double 编写主函数 定义数组 调用函数 输入元素 调用函数输出元素 元素个数不超过 100 个 主函数如下 提示 print 函数中最后输出一个换行 注意 不使用重载 无意义 需提交完成程序 include using namespace std int input int arr int count 0 for int i 0 i arr i if arr i 9999 arr i 0 break count return count double input double arr int count 0 for int i 0 i arr i if arr i 9999 arr i 0 0 break count return count void print int arr int n for int i 0 i n i if i n 1 cout arr i endl else cout arr i void print double arr int n for int i 0 i n i if i n 1 cout arr i endl else cout arr i int main int ai 100 double ad 100 int ni nd ni input ai nd input ad print ai ni print ad nd return 0 8 逆序函
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 人工智能深度学习案例分析题集
- 畜牧防疫与动物养殖责任承担协议
- 外包劳务承揽协议
- 某超市辐射源规定
- 我家的老物件老式闹钟作文(13篇)
- 2025年系列高效脱氧剂项目规划申请报告模板
- 专业服务公司与医院合作协议
- 2025年消防安全知识培训实操应用篇考试题库消防巡查试题
- 综合案例分析题2025年大学统计学期末考试题库实战解析与实战
- 2025年温室节能遮荫保温幕项目规划申请报告
- 玻璃粉烧工艺
- 云计算和边缘计算在工业互联网中的融合
- 24年海南生物会考试卷
- 中南大学学科发展与规划处
- 高危孕产妇管理课件培训
- 天一大联考海南省2024届高一物理第二学期期末考试试题含解析
- 夏季驾驶员安全培训
- 计量经济学论文eviews
- 《纳税筹划(第7版)》课件 第7章 其他税种的纳税筹划
- 儿童被忽视量表(CNS)
- 闽教版小学英语四年级下册总复习
评论
0/150
提交评论