zoukankan      html  css  js  c++  java
  • P1116 车厢重组

    https://www.luogu.com.cn/problem/P1116

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int n, ans, a[10005];
     4 int main()
     5 {
     6     //冒泡排序 
     7     
     8     cin>>n;
     9     for(int i=1; i<=n; i++)
    10         cin>>a[i];
    11         
    12     for(int i=1; i<=n; i++){
    13         for(int j=1; j<n; j++){  //此处注意j取值范围为[1,n-1] 
    14             if(a[j]>a[j+1]){
    15                 swap(a[j],a[j+1]);
    16                 ans++;
    17             }
    18         }
    19     }
    20     cout<<ans;
    21     return 0;
    22  } 
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int n, ans, a[10005];
     4 int main()
     5 {
     6     //冒泡排序 -- 优化 
     7     cin>>n;
     8     for(int i=1; i<=n; i++)
     9         cin>>a[i];
    10         
    11     for(int i=1; i<=n; i++){
    12         bool f=1;                //设定交换标记 
    13         for(int j=1; j<n; j++){  //此处注意j取值范围为[1,n-1]
    14             if(a[j]>a[j+1]){
    15                 swap(a[j],a[j+1]);
    16                 ans++;
    17                 f=0;             //说明有交换,仍然需要下一轮排序 
    18             }
    19         }
    20         if(f)break;              //该轮没有交换则说明已排好序,直接退出 
    21     }    
    22     cout<<ans;
    23     return 0;
    24  } 
  • 相关阅读:
    ble_app_hrs心率程序 nrf51822
    2019.05.08 《Linux驱动开发入门与实战》
    函数指针
    typedef
    回调函数
    android2
    android1
    每周总结2
    HTML
    数组(续)
  • 原文地址:https://www.cnblogs.com/tflsnoi/p/13152851.html
Copyright © 2011-2022 走看看