当前位置:网站首页>*2-1 OJ 254 翻煎饼
*2-1 OJ 254 翻煎饼
2022-08-09 13:31:00 【叶萧白】
题目描述

输入

输出

样例输入

样例输出

源代码
方法1
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define N 1001
using namespace std;
int n, ans, a[N];
void change(int x)
{
int l, r;
ans++;
l = r = x / 2;
r++;
if (x % 2 == 1) r++;
while (l > 0) swap(a[l--], a[r++]);
}
int main()
{
int i, k;
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
ans = 0;
while (n > 1)
{
k = 1;
for (i = 2; i <= n; i++)
{
if (a[k] < a[i]) k = i;
}
if (k != n)
{
if (k > 1) change(k);
change(n);
}
n--;
}
printf("%d\n", ans);
return 0;
}
方法2
#include<stdio.h>
#include<stdlib.h>
int a[1002]={
0};
bool flag=true;//是否满足非从小到大排序
int dex=0;//已经排好位置的数量
int conut=0;//最大值下标
int ans=0; //翻转次数
void print(int n){
for(int i=1;i<=n;i++){
printf("%d ",a[i]);
}
printf("\n");
}
void juge(int n){
int flag1=0 ;
for(int i=1;i<=n-1;i++){
if(a[i]>a[i+1]){
flag=true;
flag1++;
}
}
if(flag1==0){
flag=false;
}
}
void turn(int n){
//翻转函数
//翻转第一次
int low=1,high=conut;
if(conut!=1){
//最上面和不用翻转
while(low<=high){
int temp=a[low];
a[low]=a[high];
a[high]=temp;
low++;
high--;
}
// print(n)
ans++;
}
juge(n);//判断是否从小到大有序
if(flag==false){
return ;
}
//翻转第二次
low=1,high=n-dex;
while(low<=high){
int temp=a[low];
a[low]=a[high];
a[high]=temp;
low++;
high--;
}
// print(n);
ans++;
juge(n);//判断是否从小到大有序
dex++;
}
void find(int n){
//查找元素
int max=-1;
for(int i=1;i<=n-dex;i++){
if(a[i]>=max){
max=a[i];
conut=i;
}
}
if(conut==n-dex){
//目前位置已经是最大位置
dex++;
find(n);
}
else
return ;
}
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
juge(n);//判断是否从小到大有序 防止BUG
while(flag&&dex!=n-1){
find(n);
turn(n);
}
printf("%d\n",ans);
}
关于这题
这里解释一下第一种方法
思路:先找到一个最大的数 把他移到最前面 再翻转
此时最大的数已经到了末尾 我们就不再处理他 看前面的元素
即处理子数列问题
注:我们要单独去判断 最大的数是不是就是最后一个 不然会出BUG
这里ans 和 数组 我们设立的是全局变量 因为在函数里也要用到
l 和 r 是判断数组位置的 左标和 右标 当 x%2==1时 右标要加一 (不要标记成中间的数了)
边栏推荐
猜你喜欢

Zero Basic Learning CANoe Panel Design Catalog

From the Dutch flag problem to the optimization and upgrade of quick row

汇编语言学习(二)

Operating system migration practice deploying MySQL database on openEuler

Where to go to IOE-EBS

华为ensp静态路由、DHCP

阿里云PAI与香港大学合作论文入选INFOCOM 2022,有效减少大规模神经网络训练时间

openharmony容器组件之Column

手机厂商失守元宇宙

iptables防火墙
随机推荐
远程控制软件-向日葵
202五一杯数学建模ABC三题
手机厂商失守元宇宙
Jetpack Compose——Button(按钮)的使用
VNet
【面试高频题】可逐步优化的链表高频题
用asp.net基于C#编写简单登录注册界面(访问数据库)
汇编语言学习(六)课程设计一
使用 NVIDIA Kaolin Wisp 重建3D场景
1.11 flink中的动态加载udf jar包
数据增广
Jetpack Compose——TextField及OutlinedTextField(文本框)的使用
tianchi过往方法
阿里云发布中国云原生数据湖应用洞察白皮书
Using the NVIDIA Kaolin Wisp reconstruction of 3 d scene
目标检测类间不平衡问题
Jetpack Compose - the use of Image (picture)
System Scalability Thinking
处理JSON,fastjson、json-lib简单使用
Jetpack Compose——Image(图片)的使用