javascript - 使用 if-else 降序而不使用逻辑运算符 Javascript
问题描述
给定三个数字,使用上述if...else语句按降序打印它们。不允许使用任何逻辑运算符来解决此问题,但欢迎使用比较运算符。
var a = 3;
var b = 2;
var c = 1;
if(a<b<c){
console.log(c,b,a)
}
if(b<c<a){
console.log(b,c,a)
}
if(c<a<b){
console.log(c,a,b)
}
// I understand this is wrong as when I do a<b<c
// First it compares a<b = true(1) or false(0) and remaining compares to c
// the answer in boolean
// How do I approach this problem?
解决方案
var a = 5
var b = 2
var c = 3
if(a<b){
if(c<a){
console.log(b,a,c);
} else if(b<c){
console.log(c,b,a);
}else{
console.log(b,c,a);
}
} else if(a<c){
console.log(c,a,b);
} else if(c<b){
console.log(a,b,c);
}
else{
console.log(a,c,b);
}
//了解Bubble、Insertion排序的概念更好理解。
推荐阅读
- python-3.x - MacOS 10.14.6 更新后 Tkinter 导致计算机崩溃
- aws-lambda - 如果 x-amz-target 标头包含点,Amazon api 网关不会传递给 lambda 请求
- angular - 在Angular中将数据从指令传递到组件
- java - ListView 移除下方的按钮
- javascript - 来自 TextInput 的 onChangeText 正在复制 React Native 上的文本
- java - 比较 10 个数字的字符串的最佳方法
- ios - 为什么 return 0 永远不会在这段代码中执行?
- java - 如何使用带有 rxjava2 的 Observable zip 来获取多个响应?
- python-3.x - 通过 API IB 下订单
- django - Django 表单创建默认用户而不是自定义用户。如何创建自定义用户?