C++作业题与答案

更新时间:2024-04-12 20:22:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

e1 利用公式“π/4≈1-1/3+1/5-1/7+......”, 求π的近似值。

① 要求迭代项本身的绝对值小于1E-5。(结果:π为3.14157) ② 要求相邻两次迭代项的差的绝对值小于1E-5。(结果:π为3.146) e2 输入一个整数m,判断其是否为素数。

① 要求循环次数i=2 ~ i<=m-1。 ② 要求循环次数i=2 ~ i<=sqrt(m)。 e3 编程求 1!+2!+3!+4!+...+15!

累乘与累加的练习。结果:当i=5 ,s=153; 当i=15,累加和s=1.4016e+012。 e4 (即习题2-10)编程求“水仙花数”。“水仙花数”是指一个三位数, 其各位数字立方

和等于该数本身。例如,153就是一个水仙花数。结果:

153 = 1^3+5^3+3^3 370 = 3^3+7^3+0^3 371 = 3^3+7^3+1^3 407 = 4^3+0^3+7^3

e5 编程求

① 分解一个数的质因子。例如,540=1*2*2*3*3*3*5。(即习题2-6) ② 编程求1000之内所有“完数”“。完数”是指一个数等于它的约数之和。例如6=1+2+3,就是一个完数(即习题2-11)。注意,与分解质因子不一样,相同约数仅取一个。结果:

6=1+2+3

28=1+2+4+7+14

496=1+2+4+8+16+31+62+124+248

e6 用迭代法编程求x=√a , 求平方根的迭代公式为:X(n+1)=1/2(X(n)+a/X(n))。初值取

1,要求两次迭代的差的绝对值ε小于1E-7。例如,当a=2,求得√2 ≈ 1.41421。(参考例2-16)

e7 用循环语句编程,显示如下两个图形。(基于习题2-12) ① * 1个星 ②

*** ******************* 19个星 ***** *****************

******* ***************

********* *************

*********** 共10行 *********** 共8行 ************* ********* *************** *******

***************** ***** 5个星 ******************* 19个星

e8 编程,分别打印如下3个乘法九九表:

① * 1 2 3 4 5 6 7 8 9 ② * 1 2 3 4 5 6 7 8 9 ③ * 1 2 3 4 5 6 7 8 9

----------------------------- ---------------------------- ---------------------------- 1 1 2 3 4 5 6 7 8 9 1 1 1 1 2 3 4 5 6 7 8 9 2 2 4 6 8 10 12 14 16 18 2 2 4 2 4 6 8 10 12 14 16 18 3 3 6 9 12 15 18 21 24 27 3 3 6 9 3 9 12 15 18 21 24 27 4 4 8 12 16 20 24 28 32 36 4 4 8 12 16 4 16 20 24 28 32 36 5 5 10 15 20 25 30 35 40 45 5 5 10 15 20 25 5 25 30 35 40 45 6 6 12 18 24 30 36 42 48 54 6 6 12 18 24 30 36 6 36 42 48 54 7 7 14 21 28 35 42 49 56 63 7 7 14 21 28 35 42 49 7 49 56 63 8 8 16 24 32 40 48 56 64 72 8 8 16 24 32 40 48 56 64 8 64 72 9 9 18 27 36 45 54 63 72 81 9 9 18 27 36 45 54 63 72 81 9 81

e9* 用循环语句编程,分别显示如下3个图形。

① ② 4 ③ 4

3 3 3 3 3 3 3 4 4 4 4 3 4 3 2 2 2 2 2 3 3 3 3 3 3 4 3 2 3 4 3 2 1 1 1 2 3 3 3 3 3 3 3 3 4 3 2 1 2 3 4 3 2 1 0 1 2 3 2 2 2 2 2 2 2 2 2 4 3 2 1 0 1 2 3 4 3 2 1 1 1 2 3 2 2 2 2 2 2 2 4 3 2 1 2 3 4 3 2 2 2 2 2 3 1 1 1 1 1 4 3 2 3 4 3 3 3 3 3 3 3 1 1 1 4 3 4 0 4

e10* 编程。若一头小母牛,从出生起第四个年头开始每年生一头母牛,第n年有多少头牛? (分别使用递推和递归的方法)

e11 输入一个n位长的无符号十进制整数(例如,123),用递归方法编写下列3个函数:

① 输出 1 最高位数 12 前两位数,…

123 前n位数 (共n行)

② 输出 321 把数逆转输出,即最高位变成最低位

③ 输出 0001 0010 0011 即把n位整数转换为n个4位长的BCD码。(又如:456=>0100 0101 0110;

1098=>0001 0000 1001 1000) e12 把以下程序中的print()函数改写为等价的递归函数。(即习题3.4-6)

#include

#include // 运行显示:

void print(int w){ // 1 for(int i=1; i<=w; i++) // 2 2 {for(int j=1; j<=i; j++) // 3 3 3 cout <

e13 已知一个正整数的个位是6,把个位上的6移到该数的最前面,新数是旧数的4倍,

编程求这个数。

e14 完成教材上的两道习题。

① (习题3.4-8) 编写一个程序,包含三个重载的display函数和一个主函数。 要求: 第一个函数输出double值,前面用字符串\:\引导; 第二个函数输出一个int值,前面用字符串\:\引导;

第三个函数输出一个char字符值,前面用字符串\:\引导;

在主函数中分别用double、int和char型变量作为实参调用display函数。

② (习题3.4-10)多文件练习。

e15 编写一个函数findmin,从含有10个整数的数组中找出最小数及其下标,并把它和数

组中最前面的元素对换。函数返回最小数的下标。

要求在主函数中定义整数数组(34,91,83,56,29,93,56,12,88,72),输出此原始数组;调用

函数findmin后,打印最小数的下标及最小数,并输出调整后的数组。

e16 在主函数中先定义并初始化一个整数组,数组元素按升序排列,输出该数组;再键入

一个整数,然后调用函数ins把键入的数插入到原有的数组中,保持数组元素从小到大的排列顺序,并把最大数(有可能是刚插入的数)挤出。最后,在主函数中输出改变

后的数组和被挤出的数。

e17 下面的程序中,调用了findmax函数,该函数功能是寻找数组中最大元素,返回其地

址值,同时将最大元素的下标通过参数传回调用处,编程实现findmax函数。

int* findmax(int* array,int size,int& index); int* iptr;

void main(){ int a[10]={33,54,91,67,82,37,85,63,19,68}; int* maxaddr;int idx;

for(int i=0;i

cout<<\ <<\

<<\

e18 编写程序,实现两个字符串比较的自定义版:int strcmp(const char* str1,const char* str2);

当str1>str2时,返回正数; 当str1==str2时,返回0; 当str1

e19* 编程。使用标准库函数qsort,对各类数组进行排序。函数原型为:

void qsort((void*)a,int size,int width,int(*fcmp)(const void*,const void*)); qsort函数在头文件stdlib.h中说明。

(1) 对整数数组 (12,32,42,51,8,16,51,21,19,9) 排序。要求按每个元素各位数字之和,降序排列。例如,8>16。

(2) 对浮点数组 ( 32.1, 456.87, 332.67, 442.0, 98.12, 451.79, 340.12, 54.55, 99.87, 72.5 )排序。升序排列。例如,54.55<72.5。

(3) 对字符串数组 ( enter, number, size, begin, of, cat, cap, program, certain, a )排序。升序排列,先按串长度排,若长度相等,再比较字符串的值。例如,cap

/*/ e1 ① #include #include void main()

{ double term,sum,pi,k; int i; i=1; term=1; sum=0; do

{sum=sum+term; ++i;

k=pow(-1,(i+1)); term=k*1/(2*i-1);

}while(fabs(term)>=1E-5); pi=4*sum;

cout<<\} //*/ /*

pi=3.14157

Press any key to continue */

/*/ e1 ②

#include #include void main()

{ double a,b,sum,lim=1E-5; int i=3; a=1;

for(b=-1/(1/fabs(a)+2),sum=a+b;fabs(fabs(b)-fabs(a))>=lim;i++) {a=b;

b=pow(-1,(i+1))/(1/fabs(a)+2); sum+=b; }

cout<<\} //*/ /*

pi=3.146

Press any key to continue*/

/*/ e2 ①

#include void main() { int m,i=1,k; cout<<\ cin>>m; do {i++;

k=(m%i); }while(k!=0); if (m==i) cout<

Press any key to continue*/ /*m=24

24不是素数

Press any key to continue */

/*/ e2 ②

#include #include void main() {int m,i=1,k; cout<<\ cin>>m;

k=int(sqrt(m)); do i++;

while(m%i&&i<=k); if (m%i==0) cout<

Press any key to continue*/ /*m=24

24不是素数

Press any key to continue */

/*/ e3

#include void main() { int i,n;

long int sum,term; term=1; sum=0; cout<<\ cin>>n;

for (i=1;i<=n;i++) {term*=i; sum+=term; }

cout<<\}

//*/ /*n=5 s=153

Press any key to continue*/

/*/ e4

#include #include void main() { int a,b,c,n;

for(n=100;n<=999;n+=1) {a=n/100;

b=(n-a*100)/10; c=n-100*a-10*b; if(n==pow(a,3)+pow(b,3)+pow(c,3)) cout<

Press any key to continue */

/*/ e5 ①

#include void main() { int m,i=2; cout<<\ cin>>m;

cout<

{ if (m%i==0) {cout<<\ m=m/i;} else i++; }while (i<=m); cout<

/*m=540

540=1*2*2*3*3*3*5

Press any key to continue*/

/*/ e5②

#include void main()

{int n=1,i,sum,y=0; do{sum=0;

for(i=1;i

{ if(n%i==0) sum+=i;} if(sum==n) {cout<

if(y%5==0) cout<

6 28 496 Press any key to continue*/ /*/ e6

#include #include void main()

{double x,x0,x1,a,epson; cout<<\ cin>>a;

epson=1E-7; x0=1;

for ( x1=0.5*(1+a);fabs(x1-x0)>=epson;x=x1) { x0=x1;

x1=0.5*(x0+a/x0); }

cout<

2的平方根x=1.41421 Press any key to continue*/

/*/ e7 ①

#include void main()

{int i,j,k;

for (i=1;i<=10;i++)

{ for(j=1;j<=10-i;j++) cout<<\

for (k=1;k<=2*i-1;k++) cout<<\ cout<

* *** ***** ******* ********* *********** ************* *************** ***************** ******************* Press any key to continue*/

/*/ e7 ②

#include void main() {int i,j,k;

for (i=1;i<=8;i++)

{ for (j=0;j<=i-1;j++) cout<<\

for(k=1;k<=21-2*i;k++) cout<<\ cout<

* *** ***** ******* ********* *********** *************

*************** ***************** ******************* Press any key to continue*/

/*/ e8①

#include void main()

{ cout<<\ for(int i=1;i<=9;i++) {cout<

for(int j=1;j<=9;j++) {cout<<\ cout<

* 1 2 3 4 1 1 2 3 4 2 2 4 6 8 3 3 6 9 12 4 4 8 12 16 5 5 10 15 20 6 6 12 18 24 7 7 14 21 28 8 8 16 24 32 9 9 18 27 36 Press any key to continue*/

/*/ e8②

#include void main()

{ cout<<\ for (int i=1;i<=9;i++) { cout<

for (int j=1;j<=i;j++) cout<<\ cout<

5 6 5 6 10 12 15 18 20 24 25 30 30 36 35 42 40 48 45 54 7 8 7 8 14 16 21 24 28 32 35 40 42 48 49 56 56 64 63 72 9 9 18 27 36 45 54 63 72 81 * 1 2 3 4 5 6 7 8 9 1 1

2 2 4

3 3 6 9

4 4 8 12 16

5 5 10 15 20 25

6 6 12 18 24 30 36

7 7 14 21 28 35 42 49

8 8 16 24 32 40 48 56 64

9 9 18 27 36 Press any key to continue*/

/*/ e8③

#include void main()

{ cout<<\ for (int i=1;i<=9;i++) { cout<

for (int j=1;j

* 1 2 3 4 1 1 2 3 4 2 4 6 8 3 9 12 4 16 5 6 7 8 9 Press any key to continue*/

/*/ e9 ①

#include #include void main()

45 54 5 6 5 6 10 12 15 18 20 24 25 30 36 63 72 7 8 7 8 14 16 21 24 28 32 35 40 42 48 49 56 64 81 9 9 18 27 36 45 54 63 72 81

{ int i,j,a,b;

for(i=-3;i<=3;i++) { a=abs(i);

for(j=-3;j<=3;j++) { b=abs(j); (a>b)?cout<<\ } cout<

3 3 3 3 3 3 3 3 2 2 2 2 2 3 3 2 1 1 1 2 3 3 2 1 0 1 2 3 3 2 1 1 1 2 3 3 2 2 2 2 2 3 3 3 3 3 3 3 3

Press any key to continue*/

/*/ e9 ②

#include #include #include void main()

{signed i=4;int x,y=4; do

{x=(4-abs(i))*2;

for(cout<0;x--) cout<

if(i%2==0) y--;}while(i>=-4); } //*/ /*

4 4 4 4 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1

1 1 1 0

Press any key to continue*/

/*/ e9 ③

#include #include #include void main() {signed i=4,j,x; do {j=4;

x=(4-abs(i))*2;

for(cout<

if(abs(i)>=abs(j)) cout< i--;}while(i>=-4); } //*/ /*

4 4 3 4 4 3 2 3 4 4 3 2 1 2 3 4 4 3 2 1 0 1 2 3 4 4 3 2 1 2 3 4 4 3 2 3 4 4 3 4 4

Press any key to continue*/ /*/ e10 递推法 #include void main() {int n,sum=0;

cout<<\cin>>n; if(n>=4)

{for(n=n-3;n>0;n--) sum=sum+1;}

cout<<\

//*/ /*

How many years? 10

The nunber of cow is 7 Press any key to continue*/ /*/ e10递归法 #include int cow(int); void main() {int n,x;

cout<<\cin>>n; x=cow(n);

cout<<\int cow(int n)

{if(n<=3) return 0; else return cow(n-1)+1 ;} /*

How many years? 10

The nunber of cow is 7 Press any key to continue*/

/*/ e11

#include

void fc(int n){if(!n)return;fc(n/10);cout<

void fe(int n){char s[]=\void ff(int a){if(!a)return;ff(a/10);fe(a);cout<<\void main()

{long a;cout<<\请输入一个无符号整数: \cout<<\ \\n\

cout<<\ \\n\cout<<\ \\n\} //*/ /*

请输入一个无符号整数: 789 (1) 7 78 789

(2) 987 (3)

0111 1000 1001

Press any key to continue //*/

/*/ e12

#include #include void print(int w)

{ if(w==1) cout<

for(int i=1; i<=w; i++) cout <

void main() {print(5);} //*/ /* 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

Press any key to continue*/ /*/ e13

#include #include void main()

{int m=0,n=0,t;double x,y; do

{x=10*n+6; t=pow(10,m);

if(n%t==0&&n!=0) m++; y=n+6*pow(10,m); n++;}while(y!=4*x);

cout<

615384=4*153846

Press any key to continue*/

/*/ e14 ① #include void display(double); void display(int); void display(char); void main()

{double a=3.14;int b=10; char c='W'; display(a); display(b); display(c); }

void display(double a)

{cout<<\void display(int b)

{cout<<\void display(char c)

{cout<<\//*/ /*

a double:3.14 a int:10 a char:W

Press any key to continue */

/*/ e14 ② // my function.h

int Fabricate(int m,int n) int Multi(int m,int n) // my Fabricate.cpp

int Fabricate(int m,int n) { int g

if (n==0&&m==n) return 1; else g=n*Fabricate(n-1); return Multi(m,n)/g; }

// my Multi.cpp

int Multi(int m,int n) {if (m=n) return 1;

else return m*Multi(m-1); }

// my main.cpp

#include

#include\void main() { int m,n,a;

cout<<\cin>>m,n;

a=Fabricate(m,n); cout<

/*/ e15

#include void findmin(int[],int); void main()

{ int a[10]={34,91,83,56,29,93,56,12,88,72}; for (int i=0;i<10;i++) cout<

void findmin(int x[],int n) { int k,t=0;

for(int i=t+1;i

/*34 91 83 56 29 93 56 12 88 72 第11位数最小,为:12 neworder:

12 91 83 56 29 93 56 34 88 72 Press any key to continue */

/*/ e16

#include void ins(int [],int,int);

void main() { int n;

int a[10]={0,1,2,3,4,5,6,7,8,9}; for(int i=0;i<10;i++) cout<>n; ins(a,n,10); for(i=0;i<10;i++) cout<

void ins(int x[],int n,int num) { int min,t; if(x[num-1]>n) {cout<

for(int i=0;i<(num-1);i++) {t=i; for(int j=i+1;j

else cout<

0 1 2 3 4 5 6 7 8 9 n=8 9

0 1 2 3 4 5 6 7 8 8

Press any key to continue*/

/*/ e17

#include

int*findmax(int*array,int size,int& index); int*ipt; void main()

{int a[10]={33,54,91,67,82,37,85,63,19,68}; int*maxaddr;int idx;

for(int i=0;i<10;i++)cout<

cout<<\ <<\ <<\int*findmax(int*array,int size,int&index) { index=0; for(int i=1;i

if(array[i]>array[index])index=i; if(index!=0)

return array+index; } //*/ /*

0 0x0012FF58 33 1 0x0012FF5C 54 2 0x0012FF60 91 3 0x0012FF64 67 4 0x0012FF68 82 5 0x0012FF6C 37 6 0x0012FF70 85 7 0x0012FF74 63 8 0x0012FF78 19 9 0x0012FF7C 68 the index of maximum elemen is:2 the address of it is:0x0012FF60 the value of it is:91

Press any key to continue */ /*/e18

#include #include

int strcmp(const char* str1,const char* str2) { if (strlen(str1)!=strlen(str2)) { if (strlen(str1)>strlen(str2)) return 1; else return -1; } else { int t=strlen(str1);

for(int i=0;istr2[i]) return 1; if (str1[i]

void main() { char *a=\ char *b=\ cout<

Press any key to continue*/ //*/

/*e19

#include #include #include

int fcmpa(const void* a1,const void* b1) { return (*(int *)b1+*(int *)b1/10)-(*(int *)a1+*(int *)a1/10); }

int fcmpb(const void* a2,const void* b2) { return *(double

*) a2 > *(double *) b2 ? 1 : -1; }

int fcmpc(const void* a3,const void* b3) {

if(strlen(*(char**)a3)==strlen(*(char**)b3)) return strcmp(*(char**)a3, *(char**)b3); return strlen(*(char**)a3)-strlen(*(char**)b3); }

void main() { int a[10]={12,32,42,51,8,16,51,21,19,9};

double b[10]={32.1,456.87,332.67,442.0,98.12,451.79,340.12,54.55,99.87,72.5}; char* c[]={\ qsort(a,10,sizeof(int),fcmpa); cout<<\ for(int i=0;i<=9;i++) cout<

)/sizeof(*c),sizeof(*c),fcmpc); cout<<\ for(int k=0;k<=9;k++) cout<

a[i]after:19 9 8 16 42 51 51 32 21 12

b[i]after:32.1 54.55 72.5 98.12 99.87 332.67 340.12 442 451.79 456.87 c[i]after:a of cap cat size begin enter number certain program Press any key to continue*/

/*/ e20

#include #include using namespace std; void main() { ifstream fin (\ char ch[200]; fin>>ch; while(ch[0]!='\\0') { cout<>ch; } cout<

} //*/

//blue.txt

//This is a test. //Answer: //Thisisatest.

本文来源:https://www.bwwdw.com/article/j00p.html

Top