当前位置:网站首页>编写一个shell脚本,求一个数的”逆序数“
编写一个shell脚本,求一个数的”逆序数“
2022-06-29 12:23:00 【小天天666】
在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数。也就是说,对于n个不同的元素,先规定各元素之间有一个标准次序(例如n个 不同的自然数,可规定从小到大为标准次序),于是在这n个元素的任一排列中,当某两个元素的实际先后次序与标准次序不同时,就说有1个逆序。一个排列中所有逆序总数叫做这个排列的逆序数。
如2431中,21,43,41,31是逆序,逆序数是4。
#!/bin/bash
echo -n 请输入一个数:
read n
count=0 #用于记录逆序数
while [ $n -gt 0 ]
do
n1=`expr $n % 10` #获取个位的数
n2=`expr $n / 10` #获取除了个位的其它数
n=$n2
#再一个一个比过去
while [ $n2 -gt 0 ]
do
n3=`expr $n2 % 10`
n2=`expr $n2 / 10`
if [ $n1 -lt $n3 ]
then
count=`expr $count + 1`
fi
done
done
echo 该数的逆序数为:$count
边栏推荐
- UI file introduction in QT
- [intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial
- Hystrix circuit breaker
- C # clue binary tree through middle order traversal
- MFC dialog program core -isdialogmessage function -msg message structure -getmessage function -dispatchmessage function
- 360数科新能源专项产品规模突破60亿
- Beifu controls the third-party servo to follow CSV mode -- Taking Huichuan servo as an example
- LeetCode_ Double pointer_ Medium_ 328. parity linked list
- Nacos startup error
- 趣谈网络协议(二)传输层
猜你喜欢

qt 自定义控件 :取值范围

Tutorial on building pytoch model from zero (V) writing training process -- some basic configurations

从零搭建Pytorch模型教程(五)编写训练过程--一些基本的配置

C#二叉树结构定义、添加节点值

RT thread memory management

3D model downloading and animation control

C # clue binary tree through middle order traversal

CVPR 2022 | 未知目标检测模块STUD:学习视频中的未知目标

RT-Thread内存管理

ArcGIS中对面状河流进行等距分段【渐变赋色、污染物扩散】
随机推荐
Record the process of a solid-state update and system migration debug
Interesting talk on network protocol (II) transport layer
Golang image/png 处理图片 旋转 写入
How to calculate win/tai/loss in paired t-test
AES-128-CBC-Pkcs7Padding加密PHP实例
C binary tree structure definition and node value addition
倍福TwinCAT3 的OPC_UA通信测试案例
Cereal mall project
Install the terrain ovirt plug-in to provide automated management for ovirt
如何計算win/tai/loss in paired t-test
leetcode 903. DI 序列的有效排列
从零搭建Pytorch模型教程(五)编写训练过程--一些基本的配置
clickhouse数据库使用jdbc存储毫秒和纳秒
Nacos startup error
Go learning - build a development environment vscode development environment golang
Problem solving: modulenotfounderror: no module named 'pip‘
ArcGIS中对面状河流进行等距分段【渐变赋色、污染物扩散】
趣谈网络协议(二)传输层
nvtmpp
代码整洁之道学习笔记