当前位置:网站首页>The Martian - Simple Math Problems
The Martian - Simple Math Problems
2022-08-09 08:02:00 【scwMason】
The question is simply that five fingers represent 1 2 3 4 5, and then arrange them from small to large, and then calculate the largest number in the entire arrangement according to the second data given
Then the first thing we have to solve is: the order problem of sorting from small to large, here we can use two methods:
1. Handwritten code
2.STL function
The handwritten code mainly masters the following logic
1. First, look for two adjacent elements from the end, let the first element be *i, the second element be *ii, and satisfy *i<*ii.
2. After finding such a set of adjacent elements, check forward from the end to find the first element greater than *i, let it be *j, and swap the i and j elements (swap).
3. Reverse all elements after ii.
Then you can write the code:
#include#include#includeusing namespace std;int arr[10005],n,m;void work(){int flag;//find the first set of arr[i]=0;i--){if(arr[i]=0;i--){if(arr[i]>arr[flag]){int cur=arr[flag];arr[flag]=arr[i];arr[i]=cur;break;}}//Reverse all numbers after flagint 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
If you use STL functions, you can:
#include#include#includeusing namespace std;int arr[10005],n,m;}int main(){scanf("%d%d",&n,&m);for(int i=0;i
The principle is the same
边栏推荐
猜你喜欢
C language: reverse character order
9.进程和计划任务管理(1)
[STL]stack与queue
网络层协议介绍
LeetCode·每日一题·761.特殊的二进制序列·分治
pc端ncnn搭建与测试
Four departments including the Ministry of Industry and Information Technology promote green smart home products to the countryside
JS基础1
SOLIDWORKS Simulation教程:计算物体的固有频率
3D软件开发工具HOOPS全套产品开发介绍 | HOOPS Exchange、HOOPS Communicator
随机推荐
实现弹簧柔性状态的2种方式 | Solidworks教程
传输层协议介绍
Set集合
Cookie和Session详解
测试流程
.net(四) 数据层实现
SDRAM的数据存储实现并对其数据进行读写操作
.net(一)WebService创建
C language: adjust the order of odd and even numbers
Solidworks 2022 Inspection新增功能:光学字符识别、可自定义的检查报告
String类创建的对象在JVM中的内存分配和equals与==的区别
Shell之函数与数组
三层交换机原理及配置
收藏!Solidworks从设计到制造流程解决方案 2022来了!
oracle权限问题
Pytorch中 nn.BatchNorm2d() 归一化操作
【Rust指南】快速入门|开发环境|hello world
VRRP原理及配置
Redis(七) 主从复制(二)哨兵模式
pragma comment的使用(转载)重新排版