|
|
26.编写函数void sum(int &s,int n),计算1+2+3+…+n的值, 并将值存放在s中
- void sum(int &s,int n){
- s=0;
- for(int i-1;i<=n;i++)
- }
复制代码 编写程序,找出二维数组所有元素中的最大值。
- #include<stdio.h>
- void main() {
- int m,n,a[10][10],i,j,max;
- printf("请输入行数和列数:");
- scanf("%d,%d",&m,&n);
- for(i = 0;i < m;i++)
- for(j = 0;j < n;j++)
- scanf("%d",&a[i][j]);
- max = a[0][0];
- for(i = 0;i < m;i++)
- for(j = 0;j < n;j++)
- if(max < a[i][j]) max = a[i][j];
- printf("max = %d\n",max);
- }
复制代码 27.编写函数int max(int a,int b,int c),求出a、b、c中的最大值,并返回。
- int max(int a,int b,int c){
- if(a>b)
- if(a>c) return a;
- else return c;
- else if(b>c) return b;
- else return c;
- }
复制代码 27.编写一个程序,求出一维数组所有元素的最小值
- #include<stdio.h>
- #define N 5
- int main(){
- int a[N]={35, 8, 26, 4, 17};
- int min = a[0], i;
- for(i=1; i<N; i++){
- if(min>a[i])
- min = a[i];
- }
- printf("%d", min);
- return 0;
- }
复制代码 编写函数void max(int a[],int n,int &max),求出n个元素的数组a中的最大值,并将最大值存放在max中。
- void max(int a[],int n,int &max){
- max=a[0];
- for(int i=1;i<n;I++)
- if(max<a[i]) max=a[i];
- }
复制代码 28.计算并输出1^2+2^2+……+n^2的值,其中n值由键盘输入。
- #include<stdio.h>
- int sum(int item,int n){
- if(item>=n) return n*n;
- return item*item+sum(item+1,n);
- }
- int main(){
- int n;
- scanf("%d",&n);
- printf("sum=%d",sum(1,n));
- return 0;
- }
复制代码 24.利用类与对象,编写一个程序计算两个给定的长方形的周长和面积。
- #include<isostream.h>
- class rectangle
- {double len,wid;
- public:
- rectangle(double i=0.0,double j=0.0)
- {len = i ;wid =j;}
- double area()
- {return len*wid;}
- double peri()
- return 2*(len+wid);
- }
- void main()
- {
- rectangle a(10,20);
- cout <<"长方形的周长和面积为"<<a.per()<<','<<a.area()<<endl;
- rectangle b(100,200);
- cout<<"长方形的周长面积为"<<b.peri()<<','<<b.area()<<endl;
- }
复制代码 编程建立一个有10个结点的有序的单向链表,要求完成如下操作:①插入一个新结点,并保持链表有序性;②输出插入新结点后的链表各结点值;③删除链表的第5个的结点;④输出删除第5个结点后的链表各结点值
- /*编程建立一个有10个结点的有序的单向链表,要求完成如下操作: 5
- ①插入一个新结点,并保持链表有序性;
- ②输出插入新结点后的链表各结点值;
- ③删除链表的第5个的结点;
- ④输出删除第5个结点后的链表各结点值。*/
- #include <iostream>
- using namespace std;
- struct Node
- {
- int data;
- Node *next;
- };
- struct Node *create(int n) //创建有n个节点的链表 ,降序排列
- {
- Node *head=NULL,*p;
- int i=0;
- int d;
- while(i<n)
- {
- p=new (struct Node);
- scanf("%d",&p->data);
- p->next=NULL;
- if(head==NULL)
- head=p;
- else
- {
- p->next=head;
- head=p;
- }
- i++;
- }
- return head;
- }
- void show(Node *h) //显示链表
- {
- Node *p;
- p=h;
- while(p!=NULL)
- {
- printf("%d\t",p->data);
- p=p->next;
- }
- printf("\n");
- }
- Node * insertNode(Node *h,int d) //插入节点,仍然有序
- {
- Node *p,*q;
- p=new Node;
- p->data=d;
- q=h;
- if(d>q->data)
- {
- p->next=h;
- h=p;
- }
- else
- {
- while(d<q->data && d<q->next->data && q->next!=NULL)
- q=q->next;
- if(q->next==NULL)
- {
- q->next=p;
- p->next=NULL;
- }
- if(q->data>d && q->next->data<=d)
- {
- p->next=q->next;
- q->next=p;
- }
- }
- return h;
- }
- Node *deleteNode(Node *h,int n) //删除第n个节点
- {
- Node *p=h,*q;
- int count=1;
- while(p!=NULL && count<n)
- {
- p=p->next;
- count++;
- }
- if(count==n )
- if(count==1)
- {
- h=h->next;
- delete p;
- }
- else
- {
- q=h;
- while(q->next!=p)
- q=q->next;
- q->next=p->next;
- delete p;
- }
-
- if(p==NULL)
- {
- cout<<"没有第"<<n<<"个节点"<<endl;
- }
- return h;
- }
- int main()
- {
- Node *h1,*h2;
- h1=create(10);
- show(h1);
- h2=insertNode(h1,108);
- show(h2);
- h1=deleteNode(h2,5);
- show(h1);
- return 0;
- }
复制代码 编写计算并输出梯形的面积,其中梯形的上底、下底和高通过键盘输入。
- #include<iostream.h>
- void main{
- float sd,xd,h,s; //上底 下底 高 面积
- cout <<"请输入一个提醒的上底、下底和高:";
- cin>>sd>>xd>>h;
- s=(sd+xd)*h/2;
- cout<<"上底为"<<sd<<,下底为"<<xd<<"高为"<<h<<"梯形的面积为:"
- <<s<<endl;
-
- }
复制代码 34.编写计算并输出圆柱体的侧面积和体积,其中圆柱体的底面半径和高通过键盘输入。
- include<iostream.h>
- void main(){
- float r,h,sc,v;
- cout<<"请输入一个圆柱体的底面半径和高";
- cin>>r>>h;
- sc=2*3.14f*r*h;
- v=3.14f*r*r*h;
- cout<<"该圆柱体的侧面积为"<<sc<<",体积为"<<v<<endl;
- }
复制代码 35.编程计算并输出圆的周长和面积,其中圆半径通过键盘输入
- #include<iostream.h>
- #define PI 3.14
- void main(){
- doubler,l,s;
- cout<<"请输入一个圆的半径";
- cin>>r;
- l=2*PI*r;
- s=PI*r*r;
- count<<"半径为"<<r<<"圆的周长为"<<l<<",面积为"<<s<<endl;
- }
复制代码
|
|