当前位置:网站首页>火星人 --简单的数学题
火星人 --简单的数学题
2022-08-09 07:57:00 【scwMason】
题目简单来说就是五根手指代表1 2 3 4 5,然后按照从小到大全排列,然后根据给出的第二个数据计算出整个全排列中第几大的数
那么我们首先要解决的是:从小到大全排列的顺序问题,这里我们可以用两种方法:
1.手写代码
2.STL函数
手写代码主要掌握下面的逻辑
1.首先从最尾端开始往前寻找两个相邻元素,令第一元素为*i,第二元素为*ii,且满足*i<*ii。
2.找到这样一组相邻元素后,再从最尾端开始往前检验,找出第一个大于*i的元素,令为*j,将i,j元素对调(swap)。
3.再将ii之后的所有元素颠倒(reverse)排序。
然后就可以写出代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int arr[10005],n,m;
void work()
{
int flag;
//寻找第一组arr[i]<arr[i+1]的组合
for(int i=n-2;i>=0;i--)
{
if(arr[i]<arr[i+1])
{
flag=i;
break;
}
}
//从后往前寻找到第一个大于arr[flag]的数,与arr[flag]交换
for(int i=n-1;i>=0;i--)
{
if(arr[i]>arr[flag])
{
int cur=arr[flag];
arr[flag]=arr[i];
arr[i]=cur;
break;
}
}
//将flag后面的所有数都反转
int left=flag+1,right=n-1;
while(left<=right)
{
int ds=arr[left];
arr[left]=arr[right];
arr[right]=ds;
left++;
right--;
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
while(m--)
{
//next_permutation(arr,arr+n);
work();
}
for(int i=0;i<n-1;i++)
{
printf("%d ",arr[i]);
}
printf("%d",arr[n-1]);
return 0;
}
如果是用STL函数的话,就可以:
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int arr[10005],n,m;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
while(m--)
{
next_permutation(arr,arr+n);
}
for(int i=0;i<n-1;i++)
{
printf("%d ",arr[i]);
}
printf("%d",arr[n-1]);
return 0;
}
原理是一样的
边栏推荐
- HOOPS是什么?这4款3D软件开发工具包你还不知道?
- yolov5 detects the number of labels in the dataset
- libtorch示例
- 【机器学习】降维代码练习
- BIM技术多牛逼?BIM技术在建筑工程行业的四大发展趋势
- 主键id,Snowflake雪花算法,优点:生成有顺序的id,提高数据库的性能
- SA-Siam:用于实时目标跟踪的双重连体网络A Twofold Siamese Network for Real-Time Object Tracking
- C language: reverse character order
- 【Template】Tree Chain Segmentation P3384
- VLAN与静态VLAN的配置
猜你喜欢
毕业我选择了保家卫国,退伍我选择了华为外包
实现弹簧柔性状态的2种方式 | Solidworks教程
SA-Siam:用于实时目标跟踪的双重连体网络A Twofold Siamese Network for Real-Time Object Tracking
弹性盒样式、移动端、VW适配、响应式布局
转换为onnx模型错误汇总
在今天这个特殊的日子,我想要开始我的代码技术博客之路
The String class objects created by the JVM memory allocation and the difference between equals and = =
C语言:打印菱形
Shell之函数与数组
Data storage implementation of SDRAM and read and write operations on its data
随机推荐
Redis(八)集群
resourcemanager启动失败,别的节点成功
Exclude null values when Oracle limits
Four departments including the Ministry of Industry and Information Technology promote green smart home products to the countryside
VRRP原理及配置
DIMP:Learning Discriminative Model Prediction for Tracking 学习判别模型预测的跟踪
3D精彩案例,清软英泰建成综合轻量化显示平台!
LeetCode·每日一题·636.函数的独占时间·栈模拟
Kotlin Coroutines - Exception Handling
定时任务组件Quartz
String类创建的对象在JVM中的内存分配和equals与==的区别
收藏!Solidworks从设计到制造流程解决方案 2022来了!
(五)、马尔科夫预测模型
tianqf's problem-solving ideas
权限(下)
3D软件开发工具HOOPS全套产品开发介绍 | HOOPS Exchange、HOOPS Communicator
低成本、大容量、高交互…Polkadot 引领 GameFi 实现新突破
Selenium测试案例一步步学之(2)Selenium自动测试脚本模块化(下)
Solidworks 2022 Inspection新增功能:光学字符识别、可自定义的检查报告
Data storage implementation of SDRAM and read and write operations on its data