算法竞赛入门经典 1.4 分支结构程序设计

//例题1-4 鸡兔同笼
//已知鸡和兔的总数量为n,总腿数为m。输入n和m,依次输出鸡的数目和兔的数目。
//如果无解,则输出“No answer”
//样例输入:14 32
//样例输出:12 2
//样例输入:10 16
//样例输出:No answer
#include<iostream>
using namespace std;

int main()
{
	int a,b,n,m;
	cin>>n>>m;
	a=(4*n-m)/2;
	b=n-a;
	if(m%2==1||a<0||b<0)
		cout<<"No answer"<<endl;
	else 
		cout<<a<<" "<<b<<endl;
	return 0;
}


//例题1-5 三整数排序
//输入3个整数,从小到大排序后输出。
//样例输入:20 7 33
#include<iostream>
using namespace std;

int main()
{
	int a,b,c;
	cin>>a>>b>>c;
	if(a<=b && b<=c)  cout<<a<<" "<<b<<" "<<endl;
	else if(a<=c && c<=b) cout<<a<<" "<<c<<" "<<b<<endl;
	else if(b<=a && a<=c) cout<<b<<" "<<a<<" "<<c<<endl;
	else if(b<=c && c<=a) cout<<b<<" "<<c<<" "<<a<<endl;
	else if(c<=a && a<=b) cout<<c<<" "<<a<<" "<<b<<endl;
	else if(c<=b && b<=a) cout<<c<<" "<<b<<" "<<a<<endl;
	return 0;
}
//三整数排序(2)
#include<iostream>
using namespace std;

int main()
{
	int a,b,c,t;
	cin>>a>>b>>c;
	if(a>b) { t=a; a=b; b=t; }
	if(b>c) { t=b; b=c; c=t; }
	if(a>c) { t=a; a=c; c=t; }
	cout<<a<<" "<<b<<" "<<c<<endl;
	return 0;
}

//三整数排序(3)
#include<iostream>
using namespace std;

int main()
{
	int a,b,c,x,y,z;
	cin>>a>>b>>c;
	x=a; if(b<x) x=b; if(c<x) x=c;
	z=a; if(b>z) z=b; if(c>z) z=c;
	y=a+b+c-x-z;
	cout<<x<<" "<<y<<" "<<z<<endl;
	return 0;
}

你可能感兴趣的:(程序设计)