当前位置:网站首页>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

原网站

版权声明
本文为[scwMason]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090757053668.html