数据结构---课后习题(第一章)

🎈数据结构基础篇

🎈👻👻课后习题

🎈👻👻😅😅😅第一章算法设计

🎈👻👻😅😅😅☀️☀️☀️☀️

题目1.16:

  写一个算法自大至小的顺序一次输出顺序读取的三个数据x,y,z

solution1(c++):第一次对所有排列组合数枚举,总共也就6种情况
#include<iostream>
#include<cstdio>

using namespace std;

//输入三个整数,从大到小的顺序输出三个数据 
int main(){
	int x,y,z;
	cin>>x>>y>>z;
	if(x>=y&&y>=z){
		cout<<x<<y<<z<<endl;
	}	
	else if(x>=z&&z>=y){
		cout<<x<<y<<z<<endl;
	} 
	else if(y>=x&&x>=z){
		cout<<y<<x<<z<<endl;
	} 
	else if(y>=z&&z>=x){
		cout<<y<<z<<x<<endl;
	}
	 else if(z>=x&&x>=y){
		cout<<z<<x<<y<<endl;
	}
	 else if(z>=y&&y>=x){
		cout<<z<<y<<x<<endl;
	}
}

输出检验(c++):

soultion1(c):
int main(){
	int x,y,z;
	scanf("%d %d %d",&x,&y,&z); 
	if(x>=y&&y>=z){
		printf("%d,%d,%d",x,y,z);
	}	
	else if(x>=z&&z>=y){
		printf("%d,%d,%d",x,z,y);
	} 
	else if(y>=x&&x>=z){
		printf("%d,%d,%d",y,x,z);
	} 
	else if(y>=z&&z>=x){
		printf("%d,%d,%d",y,z,x);
	}
	 else if(z>=x&&x>=y){
		printf("%d,%d,%d",z,x,y);
	}
	 else if(z>=y&&y>=x){
		printf("%d,%d,%d",z,y,x);
	}
}

输出检验(c):

solution2(c++):利用if-else条件语句
#include<iostream>
#include<cstdio>

using namespace std;

int main(){
	int x,y,z;
	cin>>x>>y>>z;
	int t1,t2;
	if (x>=y){
		t1 = x;
		t2 = y;
	}
	else{
		t1 = y;
		t2 = x; 
	} 
	if(z>=t1){
		cout<<z<<" "<<t1<<" "<<t2<<endl;
	}
	else{
		if(z>=t2){
	    cout<<t1<<" "<<z<<" "<<t2<<endl;
		}
		else{
		cout<<t1<<" "<<t2<<" "<<z<<endl;
		}
	}
	
}

输出检验(c++):

solution2(c):
int main(){
	int x,y,z;
	scanf("%d %d %d",&x,&y,&z); 
	int t1,t2;
	if (x>=y){
		t1 = x;
		t2 = y;
	}
	else{
		t1 = y;
		t2 = x; 
	} 
	if(z>=t1){
		printf("%d %d %d",z,t1,t2);
	}
	else{
		if(z>=t2){
	    		printf("%d %d %d",t1,z,t2);
		}
		else{
				printf("%d %d %d",t1,t2,z);
		}
	}
	
}

输出检验(c):

彩蛋:

  因为很长时间没有写过博客(bushi),算法实现能力远不及从前,代码写的很烂,主要目的还是便于自己日后复习使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Li&&Tao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值