[甘肃电大]C++4套形考任务编程题目部分答案参考

[复制链接]
发表于 2018-6-26 14:08:48 | 显示全部楼层 |阅读模式
26.编写函数void sum(int &s,int n),计算1+2+3+…+n的值, 并将值存放在s中
  1. void sum(int &s,int n){
  2.   s=0;
  3.   for(int i-1;i<=n;i++)
  4. }
复制代码
编写程序,找出二维数组所有元素中的最大值。
  1. #include<stdio.h>
  2. void main() {
  3.         int m,n,a[10][10],i,j,max;
  4.         printf("请输入行数和列数:");
  5.         scanf("%d,%d",&m,&n);
  6.         for(i = 0;i < m;i++)
  7.                 for(j = 0;j < n;j++)
  8.                 scanf("%d",&a[i][j]);
  9.         max = a[0][0];
  10.         for(i = 0;i < m;i++)
  11.                 for(j = 0;j < n;j++)
  12.         if(max < a[i][j]) max = a[i][j];
  13.         printf("max = %d\n",max);
  14. }
复制代码
27.编写函数int max(int a,int b,int c),求出a、b、c中的最大值,并返回。
  1. int max(int a,int b,int c){
  2.         if(a>b)
  3.         if(a>c) return a;
  4.         else return c;
  5.         else if(b>c) return b;
  6.         else return c;
  7. }
复制代码
27.编写一个程序,求出一维数组所有元素的最小值

  1. #include<stdio.h>
  2. #define N 5
  3. int main(){
  4.     int a[N]={35, 8, 26, 4, 17};
  5.     int min = a[0], i;
  6.     for(i=1; i<N; i++){
  7.         if(min>a[i])
  8.             min = a[i];
  9.     }
  10.     printf("%d", min);
  11.     return 0;
  12. }
复制代码
编写函数void max(int a[],int n,int &max),求出n个元素的数组a中的最大值,并将最大值存放在max中。
  1. void max(int a[],int n,int &max){
  2.         max=a[0];
  3.         for(int i=1;i<n;I++)
  4.         if(max<a[i]) max=a[i];
  5. }
复制代码
28.计算并输出1^2+2^2+……+n^2的值,其中n值由键盘输入。
  1. #include<stdio.h>
  2. int sum(int item,int n){
  3.     if(item>=n) return n*n;
  4.     return item*item+sum(item+1,n);
  5. }
  6. int main(){
  7.     int n;
  8.     scanf("%d",&n);
  9.     printf("sum=%d",sum(1,n));
  10.     return 0;
  11. }
复制代码
24.利用类与对象,编写一个程序计算两个给定的长方形的周长和面积。

  1. #include<isostream.h>
  2. class rectangle
  3. {double len,wid;
  4. public:
  5. rectangle(double i=0.0,double j=0.0)
  6. {len = i ;wid =j;}
  7. double area()
  8. {return len*wid;}
  9. double peri()
  10. return 2*(len+wid);
  11. }
  12. void main()
  13. {
  14. rectangle a(10,20);
  15. cout <<"长方形的周长和面积为"<<a.per()<<','<<a.area()<<endl;
  16. rectangle b(100,200);
  17. cout<<"长方形的周长面积为"<<b.peri()<<','<<b.area()<<endl;
  18. }
复制代码
编程建立一个有10个结点的有序的单向链表,要求完成如下操作:①插入一个新结点,并保持链表有序性;②输出插入新结点后的链表各结点值;③删除链表的第5个的结点;④输出删除第5个结点后的链表各结点值

  1. /*编程建立一个有10个结点的有序的单向链表,要求完成如下操作: 5
  2. ①插入一个新结点,并保持链表有序性;
  3. ②输出插入新结点后的链表各结点值;
  4. ③删除链表的第5个的结点;
  5. ④输出删除第5个结点后的链表各结点值。*/
  6. #include <iostream>
  7. using namespace std;
  8. struct Node
  9. {
  10. int data;
  11. Node *next;
  12. };
  13. struct Node *create(int n)  //创建有n个节点的链表 ,降序排列  
  14. {
  15. Node *head=NULL,*p;
  16. int i=0;
  17. int d;
  18. while(i<n)
  19. {
  20.   p=new (struct Node);
  21.      scanf("%d",&p->data);
  22.   p->next=NULL;
  23.   if(head==NULL)
  24.     head=p;
  25.   else
  26.   {
  27.      p->next=head;
  28.      head=p;
  29.   }     
  30.    i++;
  31. }
  32. return head;
  33. }
  34. void show(Node *h)  //显示链表
  35. {
  36. Node *p;
  37. p=h;
  38. while(p!=NULL)
  39. {
  40.   printf("%d\t",p->data);
  41.   p=p->next;
  42. }
  43.     printf("\n");
  44. }
  45. Node * insertNode(Node *h,int d)  //插入节点,仍然有序
  46. {
  47. Node *p,*q;
  48. p=new Node;
  49. p->data=d;
  50. q=h;
  51. if(d>q->data)
  52. {
  53.   p->next=h;
  54.   h=p;
  55. }
  56. else
  57. {
  58.   while(d<q->data && d<q->next->data && q->next!=NULL)
  59.     q=q->next;
  60.   if(q->next==NULL)
  61.   {
  62.      q->next=p;
  63.      p->next=NULL;
  64.   }  
  65.   if(q->data>d && q->next->data<=d)
  66.   {
  67.    p->next=q->next;
  68.    q->next=p;
  69.   }      
  70. }
  71. return h;
  72. }
  73. Node *deleteNode(Node *h,int n)  //删除第n个节点
  74. {
  75. Node *p=h,*q;
  76. int count=1;
  77.     while(p!=NULL && count<n)
  78.     {
  79.      p=p->next;
  80.      count++;
  81.     }
  82.     if(count==n )
  83. if(count==1)
  84.     {
  85.      h=h->next;
  86.      delete p;
  87.     }
  88.     else
  89.     {
  90.       q=h;
  91.    while(q->next!=p)
  92.      q=q->next;
  93.       q->next=p->next;
  94.    delete p;   
  95.     }
  96.      
  97.     if(p==NULL)
  98.     {
  99.      cout<<"没有第"<<n<<"个节点"<<endl;
  100.     }
  101.     return h;
  102. }
  103. int main()
  104. {
  105. Node *h1,*h2;
  106. h1=create(10);
  107. show(h1);
  108. h2=insertNode(h1,108);
  109. show(h2);
  110. h1=deleteNode(h2,5);
  111. show(h1);
  112. return 0;
  113. }
复制代码
编写计算并输出梯形的面积,其中梯形的上底、下底和高通过键盘输入。
  1. #include<iostream.h>
  2. void main{
  3.         float sd,xd,h,s; //上底 下底 高 面积
  4.         cout <<"请输入一个提醒的上底、下底和高:";
  5.         cin>>sd>>xd>>h;
  6.         s=(sd+xd)*h/2;
  7.         cout<<"上底为"<<sd<<,下底为"<<xd<<"高为"<<h<<"梯形的面积为:"
  8.         <<s<<endl;
  9.         
  10. }
复制代码
34.编写计算并输出圆柱体的侧面积和体积,其中圆柱体的底面半径和高通过键盘输入。
  1. include<iostream.h>
  2. void main(){
  3.         float r,h,sc,v;
  4.         cout<<"请输入一个圆柱体的底面半径和高";
  5.         cin>>r>>h;
  6.         sc=2*3.14f*r*h;
  7.         v=3.14f*r*r*h;
  8.         cout<<"该圆柱体的侧面积为"<<sc<<",体积为"<<v<<endl;
  9. }
复制代码
35.编程计算并输出圆的周长和面积,其中圆半径通过键盘输入

  1. #include<iostream.h>
  2. #define PI 3.14
  3. void main(){
  4.         doubler,l,s;
  5.         cout<<"请输入一个圆的半径";
  6.         cin>>r;
  7.         l=2*PI*r;
  8.         s=PI*r*r;
  9.         count<<"半径为"<<r<<"圆的周长为"<<l<<",面积为"<<s<<endl;
  10. }
复制代码







快速回复 返回顶部 返回列表