钱能c++习题答案_第1页
钱能c++习题答案_第2页
钱能c++习题答案_第3页
钱能c++习题答案_第4页
钱能c++习题答案_第5页
已阅读5页,还剩85页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

二 二 2 1 include void main 本题原考虑在本题原考虑在 16 位机器上实验目前多为位机器上实验目前多为 32 位机器 故已过时 位机器 故已过时 int a 42486 cout oct a endl hex a endl unsigned b 42486 cout dec signed b endl 2 2 include include const double pi 3 1415926 void main double radius1 radius2 cout radius1 radius2 cout setw 10 pi setw 10 radius1 setw 10 pi radius1 radius1 endl setw 10 pi setw 10 radius2 setw 10 pi radius2 radius2 endl 2 3 include include const double e 2 718281828 void main cout setprecision 10 e endl setiosflags ios fixed setprecision 8 e endl setiosflags ios scientific e endl 2 4 include void main cout How many students here n 500 n 2 5 include void main cout size of char sizeof char byte n size of unsigned char sizeof unsigned char byte n size of signed char sizeof signed char byte n size of int sizeof int byte n size of unsigned sizeof unsigned byte n size of signed sizeof signed byte n size of short sizeof short byte n size of unsigned short sizeof unsigned short byte n size of long sizeof long byte n size of signed long sizeof signed long byte n size of unsigned long sizeof unsigned long byte n size of float sizeof float byte n size of double sizeof double byte n size of long double sizeof long double byte n 2 6 1 please input 3 sides of one triangle 6 6 8 a 6 00 b 6 00 c 8 00 area of triangle is 17 88854 2 该程序计算三角形的面积该程序计算三角形的面积 前后分为三部分 输入 处理 输出 前后分为三部分 输入 处理 输出 3 include include include include void main float a b c s area printf please input 3 sides of one triangle n cout a b c 输入时以空格作为数据间隔输入时以空格作为数据间隔 s a b c 2 area sqrt s s a s b s c printf a 7 2f b 7 2f c 7 2f n a b c cout setiosflags ios fixed setprecision 2 a setw 7 a b setw 7 b c setw 7 c endl printf area of triangle is 10 5f area cout area of triangle is setw 10 setprecision 5 area endl 4 include include include float area float a float b float c 函数声明函数声明 void main float a b c cout a b c 输入时以空格作为数据间隔输入时以空格作为数据间隔 float result area a b c 函数调用函数调用 cout setiosflags ios fixed setprecision 2 a setw 7 a b setw 7 b c setw 7 c endl cout area of triangle is setw 10 setprecision 5 result endl float area float a float b float c 函数定义函数定义 float s a b c 2 return sqrt s s a s b s c 2 7 In main Enter two numbers 3 8 Calling add In add received 3 and 8 and return 11 Back in main c was set to 11 Exiting 2 8 include include double Cylinder double r double h void main double radius height cout radius height double volume Cylinder radius height cout 该圆柱体的体积为 该圆柱体的体积为 volume endl double Cylinder double r double h return r r M PI h 三 三 3 1 1 sqrt pow sin x 2 5 2 a x a x 4 a 2 3 pow c x x sqrt 2 M PI M PI 为为 BC 中中 math h 中的圆周率常数中的圆周率常数 3 2 13 7 2 5 9 3 3 1 a1 1 a2 1 2 1 1 3 2 0 0 4 20 3 4 include void main int x cout x if x 1 cout x 1 1 if 2 x 3 5 include void main int a cout a int c1 a 3 0 int c2 a 5 0 int c3 a 7 0 switch c1 2 c2 1 c3 case 0 cout 不能被不能被 3 5 7 整除整除 n break case 1 cout 只能被只能被 7 整除整除 n break case 2 cout 只能被只能被 5 整除整除 n break case 3 cout 可以被可以被 5 7 整除整除 n break case 4 cout 只能被只能被 3 整除整除 n break case 5 cout 可以被可以被 3 7 整除整除 n break case 6 cout 可以被可以被 3 5 整除整除 n break case 7 cout 可以被可以被 3 5 7 整除整除 n break 3 6 include void main int grade cout grade if grade 100 grade 0 cout 90 cout 80 cout 70 cout 60 cout D n else cout E n 四 四 4 1 1 include include void main double sum 1 t 1 x int i 1 cout x do t 1 x i sum t i while fabs t 1e 8 cout sum sum endl 2 include include void main double sum 1 t 1 x cout x int i 1 while fabs t 1e 8 t 1 x i sum t i cout sum sum endl 3 include include void main double sum 1 t 1 x cout x for int i 1 fabs t 1e 8 i t 1 x i sum t cout sum sum endl 4 2 include void main long sum 0 t 1 for int i 1 i 15 i t i sum t cout sum sum endl 4 3 include void main for int i 1 i 9 i for int j 0 j 9 j for int k 0 k 9 k if i i i j j j k k k 100 i 10 j k cout 100 i 10 j k 是水仙花数是水仙花数 n 4 4 include void main for int i 1 i 1000 i int sum 0 for int j 1 j i 2 j if i j 0 sum j if sum i cout i 是完数是完数 n 4 5 include void main float s 100 h 100 for int i 1 i 10 i s h h 2 cout 共经过共经过 s 米 第米 第 10 次反弹次反弹 h 米高米高 n 4 6 include void main int peachs 1 for int i 1 i 10 i peachs peachs 1 2 cout 第一天共摘下第一天共摘下 peachs 个桃子个桃子 n 4 7 include include void main double x a cout a x a 2 while fabs x a x 2 1e 7 x x a x 2 cout a 的平方根是的平方根是 x endl 4 8 1 include void main for int i 1 i 10 i for int j 1 j 10 i j cout for int j 1 j 2 i 1 j cout cout endl 2 include void main for int i 1 i 8 i for int j 1 j i j cout for int j 1 j 18 i j cout cout endl 4 9 1 include include void main cout for int i 1 i 9 i cout setw 4 i cout n n for int i 1 i 9 i cout setw 3 i for int j 1 j 9 j cout setw 4 i j cout endl 2 include include void main cout for int i 1 i 9 i cout setw 4 i cout n n for int i 1 i 9 i cout setw 3 i for int j 1 j i j cout setw 4 i j cout endl 3 include include void main cout for int i 1 i 9 i cout setw 4 i cout n n for int i 1 i 9 i cout setw 3 i if i 1 cout setw 4 i 4 for int j i j 9 j cout setw 4 i j cout endl 4 10 include void main int n long a 1 b 1 c 1 temp cout n for int i 4 i n i temp a c a b b c c temp cout c endl 五 五 5 1 include include include bool isprime long n void main input long a b l 1 cout a b cout primes from a to b is n process if a 1 a 2 a 2 cout setw 5 2 if a 2 0 a for long m a m b m 2 if isprime m output if l 10 0 cout endl cout setw 5 m bool isprime long n int sqrtm sqrt n for int i 2 i sqrtm i 判明素数判明素数 if n i 0 return false return true 5 2 include include include double f double x double integral double a double b const double eps 1e 8 void main double a 0 b 1 cout the integral of f x from a to b is n setiosflags ios fixed setprecision 8 setw 8 integral a b eps tn t2n in i2n double sigma 0 0 for int k 0 k n k double x a k 0 5 h sigma f x t2n tn h sigma 2 0 变步长梯形变步长梯形 i2n 4 t2n tn 3 0 辛普生公式辛普生公式 n 2 h 2 return i2n 5 3 include include void multab1 void multab2 void multab3 void main multab1 multab2 multab3 void multab1 cout for int i 1 i 9 i cout setw 4 i cout n n for int i 1 i 9 i cout setw 3 i for int j 1 j 9 j cout setw 4 i j cout endl cout endl endl void multab2 cout for int i 1 i 9 i cout setw 4 i cout n n for int i 1 i 9 i cout setw 3 i for int j 1 j i j cout setw 4 i j cout endl cout endl endl void multab3 cout for int i 1 i 9 i cout setw 4 i cout n n for int i 1 i 9 i cout setw 3 i if i 1 cout setw 4 i 4 for int j i j 9 j cout setw 4 i j cout endl cout endl endl 5 4 Main x 5 y 1 n 1 Func x 6 y 21 n 11 Main x 5 y 1 n 11 Func x 8 y 31 n 21 5 5 include void main int n long a 1 b 1 temp cout n for int i 3 i n i temp a b a b b temp cout b endl 5 6 double poly int n double if n 0 return 1 if n 0 return x return 2 n 1 x poly n 1 x n 1 poly n 2 x n 5 7 include include void main double x y x 3 14159 4 do y x x cos x x sin x 1 x cos x while fabs x y 1e 6 cout x endl 答案为答案为 0 739085 5 8 include void display double d cout A double d endl void display int i cout A int i endl void display char c cout A char c endl void main double a 100 0 float f 1 0 int n 120 char ch c short s 50 display a display f display n display ch display s 5 9 include long cattle int n void main int n cout n cout cattle n endl long cattle int n if n 0 return 0 if n 3 return 1 return cattle n 1 cattle n 3 六 六 6 1 1 file1 cpp int x 1 int func file2 cpp extern int x int func void g x func file3 cpp extern int x 2 error extern int 变量若有赋值 则成定义变量若有赋值 则成定义 int g error 函数声明与前面不一致函数声明与前面不一致 void main x g 2 file1 cpp int x 5 int y 8 extern int z file2 cpp int x error int x 重复定义重复定义 extern double y error y 同一名字不同类型定义同一名字不同类型定义 extern int z error z 只有声明 却无定义只有声明 却无定义 6 2 25 6 3 include multab h void main multab1 multab2 multab3 6 3 1 include multab h void multab1 cout for int i 1 i 9 i cout setw 4 i cout n n for int i 1 i 9 i cout setw 3 i for int j 1 j 9 j cout setw 4 i j cout endl cout endl endl 6 3 2 include multab h void multab2 cout for int i 1 i 9 i cout setw 4 i cout n n for int i 1 i 9 i cout setw 3 i for int j 1 j i j cout setw 4 i j cout endl cout endl endl 6 3 3 include multab h void multab3 cout for int i 1 i 9 i cout setw 4 i cout n n for int i 1 i 9 i cout setw 3 i if i 1 cout setw 4 i 4 for int j i j 9 j cout setw 4 i j cout endl cout endl endl 6 3 h include include void multab1 void multab2 void multab3 七 七 7 1 include int findMinIndex int a int n void main int array 34 91 83 56 29 93 56 12 88 72 int size sizeof array sizeof array int minIndex findMinIndex array size cout 最小数最小数 array minIndex endl 相应的下标相应的下标 minIndex endl int findMinIndex int a int n int index 0 for int i 1 ia i index i return index 7 2 include int insert int a int n int value void main int array 12 29 34 56 72 83 88 91 int size sizeof array sizeof array cout 插入前的数组插入前的数组 n for int i 0 i size i cout array i int aValue cout aValue int max insert array size aValue cout n 最大数最大数 max endl cout 插入后的数组插入后的数组 n for int i 0 i size i cout array i int insert int a int n int value if a n 1 0 i a i 1 a i a i 1 value return retValue 7 3 include const int num 17 void main int interval 3 int a num for int i 0 i num i cout a i i 1 cout endl int i interval 1 num for int k 1 k num k cout a i a i 0 for int j 1 a i i i 1 num 数数数数 cout nNo a i boy has won n 输出胜利者输出胜利者 7 4 void Swap int a b b temp void Bsort int a int n bool dontLoopAgain false while dontLoopAgain dontLoopAgain for int i 0 pass n ia i 1 dontLoopAgain false Swap a i a i 1 7 5 include const int n 5 int sum int a 5 int size void main int array n n 3 2 4 1 5 8 7 2 5 6 6 9 1 4 3 5 5 3 6 2 2 8 1 8 6 cout sum array n int sum int a 5 int size int s 0 for int i 0 i size i s a i i a i size i 1 if size 2 1 s a size 2 size 2 return s 7 6 include void findMax int a 4 int row int col void findBad int a 4 int row int col void average int a 4 int row int col void main int array 5 4 88 67 48 91 61 65 37 77 92 81 73 60 51 55 60 60 77 63 70 80 findMax array 5 4 findBad array 5 4 average array 5 4 void findMax int a 4 int row int col int r 0 c 0 for int i 0 i row i for int j 0 ja r c r i c j cout 成绩最高的学生序号成绩最高的学生序号 r 1 for int i 0 i col i cout a r i cout endl void findBad int a 4 int row int col for int i 0 i row i for int j 0 j col j if a i j 60 cout 有不及格课程的学生序号有不及格课程的学生序号 i 1 for int k 0 k col k cout a i k cout endl break void average int a 4 int row int col double sum 0 for int i 0 i row i for int j 0 j col j sum a i j cout 所有课程平均分数为所有课程平均分数为 sum row col endl 7 7 include int a 3 3 5 7 8 2 2 4 1 1 1 int b 3 3 4 2 3 3 9 4 8 1 2 int c 3 3 void main for int i 0 i 3 i for int j 0 j 3 j c i j a i j b i j cout c i j cout endl 八 八 8 1 include int findmax int array int size int index void main int a 10 33 91 54 67 82 37 85 63 19 68 int maxaddr int idx maxaddr findmax a sizeof a sizeof a cout the index of maximum element is idx endl the address of it is maxaddr endl the value of it is a idx endl int findmax int array int size int index index 0 for int i 0 iarray index index i return 8 2 include bool assign int num interval void main if assign return int a new int num for int i 0 i num i cout a i i 1 cout endl int i interval 1 num for int k 1 k num k cout a i a i 0 for int j 1 a i i i 1 num 数数数数 cout nNo a i boy has won n 输出胜利者输出胜利者 delete a bool assign while 1 cout num interval if num 1 bool sign true while sign cout error on your input data n cout please select following operation n cout t tstop at once 1 n cout t tinput again 2 n cout sele switch sele case 1 return false case 2 sign false break case 3 num 10 interval 3 return true end of switch end of inner while end of out while 8 3 include include int CompIntBitSum const void a const void b int CompFloat const void a const void b int CompStrLen const void a const void b void main int a 12 32 42 51 8 16 21 19 9 int anum sizeof a sizeof a cout n 排序前排序前 n for int i 0 i anum i cout a i cout endl qsort a anum sizeof a CompIntBitSum cout 排序后排序后 n for int i 0 i anum i cout a i cout endl float f 32 1 456 87 332 67 442 0 98 12 451 79 340 12 54 55 99 87 72 5 int fnum sizeof f sizeof f cout n 排序前排序前 n for int i 0 i fnum i cout f i cout endl qsort f fnum sizeof f CompFloat cout 排序后排序后 n for int i 0 i fnum i cout f i cout endl char str enter number size begin of cat case program certain a int strnum sizeof str sizeof str cout n 排序前排序前 n for int i 0 i strnum i cout str i cout endl qsort str strnum sizeof str CompStrLen cout 排序后排序后 n for int i 0 i strnum i cout str i cout endl int CompIntBitSum const void a const void b int ia int a ib int b int suma 0 sumb 0 while ia suma ia 10 ia 10 while ib sumb ib 10 ib 10 return suma sumb int CompFloat const void a const void b return float a float b int CompStrLen const void a const void b if strlen char a strlen char b return strcmp char a char b return strlen char a strlen char b 8 4 the result of 3 and 2 is not 8 t 116 4 h 104 9 e 101 6 32 2 r 114 8 e 101 7 s 115 3 u 117 4 l 108 9 t 116 6 32 2 o 111 8 f 102 7 32 3 3 51 4 32 9 a 97 6 n 110 2 d 100 8 32 7 2 50 3 32 4 i 105 9 s 115 6 32 2 n 110 8 o 111 7 t 116 3 32 4 8 56 9 得到密文为得到密文为 xqk zlvyuzqn 6 jtf 9 m char jiemi const char str const char key 4 9 6 2 8 7 3 const keylen sizeof key void main char s the result of 3 and 2 is not 8 cout n 原文为原文为 n s endl char result1 jiami s cout n 密文为密文为 n result1 endl char result2 jiemi result1 cout n 解密之后为解密之后为 n result2 endl delete result1 delete result2 char jiami const char str int len strlen str char result new char len 1 for int i 0 j 0 i122 result i 90 return result char jiemi const char str int len strlen str char result new char len 1 for int i 0 j 0 i len i j j 1 keylen result i str i key j if result i 32 result i 90 return result 8 5 int strcmp const char str1 const char str2 char p1 char str1 char p2 char str2 while p1 p2 return p1 p2 8 6 char strcpy char dest const char src char pdest dest char psrc char src while pdest psrc return dest 8 7 include int strcmp const char str1 const char str2 void main int argc char argv if argc 3 cout 8 7 string1 string2 n return int result strcmp argv 1 argv 2 char c if result 0 c if result 0 c cout argv 1 c argv 2 endl int strcmp const char str1 const char str2 char p1 char str1 char p2 char str2 while p1 p2 return p1 p2 8 8 include include include void transpose int a int size 降维处理降维处理 void main int A 5 5 randomize for int i 0 i 5 i for int j 0 j 5 j cout setw 7 A i j rand cout endl transpose int A 5 cout n 转置之后转置之后 n for int i 0 i 5 i for int j 0 j 5 j cout setw 7 A i j cout endl void transpose int a int size for int i 1 i size i for int j 0 j i j int temp a i size j a i size j a j size i a j size i temp 8 9 include include void main void p malloc 1000000 int i 1 while p p malloc 1000000 i cout 堆容量为堆容量为 M i endl 9 9 1 1 include const size 10 void findmax int a int n int i int void main int a size int n 0 cout please input size datas n for int i 0 i a i findmax a size 0 n cout the maximum is a n endl It s index is n endl void findmax int a int n int i int findmax a n i 1 2 求数组中最大元素的下标求数组中最大元素的下标 放入主函数的变量放入主函数的变量 n 中中 3 include const size 10 void findmax int a int n int void main int a size int n 0 cout please input size datas n for int i 0 i a i findmax a size n cout the maximum is a n endl It s index is n endl void findmax int a int n int ia pk pk i 9 2 include include int put value into the array int get int n obtain a value from the array int vals 10 int error 1 void main put 0 10 put values into the array put 1 20 put 9 30 cout get 0 endl cout get 1 endl cout get 9 10 cerr 10 cerr range error in get value n exit 1 return vals n 9 3 include void Swap char void main char ap hello char bp how are you cout ap endl bp endl Swap ap bp cout 交换以后交换以后 n cout ap endl bp endl void Swap char str1 str2 str2 temp 一 一 10 1 include struct Student int mathmidterm int mathfinal void main Student s cout please input midterm and final math n s mathmidterm s mathfinal cout the average grade of midterm and final math is s mathmidterm s mathfinal 2 0 endl 10 2 include include struct Node char str 20 Node next void Insert Node void main Node p Node x new Node strncpy x str hello 20 x next NULL p x x new Node strncpy x str jone 20 x next p p x x new Node strncpy x str good 20 x next p p x x new Node strncpy x str better 20 x next p p x cout next cout str cout 0 n Insert p cout next cout str cout str marit 20 head str 19 0 if head head p p next NULL return if strcmp head str jone p next head head p return Node sp for sp head sp next sp sp next p next sp next sp next p 10 3 include struct Node char c Node next Node reverse Node head void main Node x 4 for int i 0 i 4 i x i c A i cout x i c x i next cout NULL next cout c cout NULL next pT next newHead newHead pT return newHead 10 4 1 include struct Lnode double data Lnode next void ShowList Lnode head for Lnode p head p p p next cout data next p p next p next pnew pnew next NULL Lnode GetNode Lnode item new Lnode if item item next NULL item data 0 0 else cout Nothing allocated n return item void main Lnode head NULL Lnode temp double d cout d while d 0 AddToEnd temp head cout d ShowList head 2 data 3 data 5 data 7 data 6 data 4 data 8 data 3 3 5 7 6 4 8 3 include struct Lnode double data Lnode next void ShowList Lnode head void AddToEnd Lnode pnew Lnode Lnode GetNode void DeleteList Lnode head void main Lnode head NULL Lnode temp double d cout d while d 0 AddToEnd temp head cout d ShowList head DeleteList head void ShowList Lnode head if head cout data next ShowList head next 递归调用递归调用 void AddToEnd Lnode pnew Lnode pnew next NULL else AddToEnd pnew head next 递归调用递归调用 Lnode GetNode Lnode item new Lnode if item item next NULL item data 0 0 else cout next delete t 10 5 include struct Lnode double data Lnode next void ShowList const Lnode const head void DeleteList Lnode head void AddToEnd Lnode pnew Lnode Lnode GetNode Lnode Merge Lnode h1 Lnode h2 void Insert Lnode void main Lnode head1 NULL Lnode temp double d cout d while d 0 AddToEnd temp head1 cout d ShowList head1 Lnode head2 NULL cout d while d 0 AddToEnd temp head2 cout d ShowList head2 Lnode head Merge head1 head2 ShowList head DeleteList head void ShowList const Lnode const head for Lnode p Lnode head p p p next cout data cout next delete t Lnode Merge Lnode h1 Lnode h2 Lnode newHead h1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论