C++編程練習——2014/3/11

對應21-25題

1

#include<iostream>
class NODE
{
public:
	int data;
	NODE *next;
};
void fun(NODE *list,int x)
{
	NODE *u,*v,*p;
	u=list;
	v=u->next;
	while(v!=NULL&&x<v->data)
	{
		u=v;
		v=v->next;
	}
	if(v==NULL||x>v->data)
	{
		p=new NODE;
		p->data=x;
		p->next=v;
		u->next=p;
	}

}
void main()
{
	int x;
	NODE *head,*p;
	head =new NODE;
	head->next=NULL;
	std::cout<<"Enter integers,end with 0"<<std::endl;
	while(1)
	{
		std::cin>>x;
		if(x==0)
			break;
		fun(head,x);
	}
	for(p=head->next;p!=NULL;p=p->next)
		std::cout<<p->data<<' ';
	std::cout<<std::endl;
	do{
		p=head->next;
		delete head;
		head=p;
	}while(p);
	
}

2.

#include <iostream.h>
#define N 100
int a[N][N];
int fun(int n)
{
	int max[N];

	for(int i=0;i<n;i++)
	{
	    max[0]=a[i][0];		
		for(int j=1;j<n;j++)
		{
			max[i]=max[i]>a[i][j]?max[i]:a[i][j];
		}
	}
	int min=max[0];
	for(int k=1;k<n;k++)
	{
		min=min<max[k]?min:max[k];
	}
	return min;


}
void main()
{
	int n;
	cout<<"please input N:"<<endl;
	cin>>n;
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
		{
			cout<<"please input a Number: "<<endl;
			cin>>a[i][j];
		}
		cout<<"The min of max numbers is "<<fun(n)<<endl;
}


3.

#include<iostream.h>
void fun(int N[4])
{
	for(int i=0;i<4;i++)
		for(int j=0;j<4;j++)
			for(int k=0;k<4;k++)
			{
				if(j!=k&&k!=i&&N[i]!=0)
					cout<<N[i]<<N[j]<<N[k]<<endl;

			}
	

}
int main()
{
	int N[4]={1,2,3,0};
	fun(N);
	return 0;
}

4.

#include<iostream.h>
#include<cmath>
void fun()
{
	long n=0,x,y;
	while(n<100000)
	{
		x=sqrt(n+100);
		y=sqrt(n+268);
		if(x*x==(n+100)&&y*y==(n+268))
			cout<<n<<endl;
		n++;

	}

}
int main()
{
	fun();
	return 0;
}

5.

#include <iostream.h>
#include <cmath>
int fun(int n)
{
	int x,y,i,j,k;
  	x=((n%10)+3)%9;//the forth
    y=(((n/10)%10)+3)%9;//the third
    i=((n/100)%10+3)%9;//the second
	j=((n/1000)+3)%9;//the first
	k=1000*x+100*y+10*i+j;
	return k;

}
int main()
{
	int i=1234;
	cout<<fun(i)<<endl;
	i=5678;
	cout<<fun(i)<<endl;
	return 0;
}

21題沒懂。。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章