2 solutions

  • 0
    @ 2024-10-18 23:14:10

    #include <stdio.h> int main() { int cnt=0; int n,i,t; scanf("%d",&n); int a[n]; for(i=0;i<n;i++){ scanf("%d",&a[i]); } for(i=0;i<n;i++){ for(int i=0;i<n-1;i++){ if(a[i]>a[i+1]){ t=a[i]; a[i]=a[i+1]; a[i+1]=t; cnt++; } } } printf("%d",cnt); return 0; }

    • 0
      @ 2024-10-8 20:40:42
      #include<bits/stdc++.h>
      typedef long long LL;
      using namespace std;
      const int N = 1e3 + 10;
      int a[N],cnt,n;
      int main()
      {
      	cin >> n;
      	for(int i = 1;i <= n;++ i)
      	{
      		cin >> a[i];
      	}
      	//冒泡排序部分:
      	for(int i = 1;i < n;++ i)
      	{
      		for(int j = 1;j <= n - i;++ j)
      		{
      			if(a[j] > a[j + 1])
      			{
              int tmp = a[j];
              a[j] = a[j + 1];
              a[j + 1] = tmp;//交换
              cnt ++;//计数
      			}
      		}
      	} 
      //	for(int i = 1;i <= n;++ i)
      //	{
      //		cout << a[i] << " ";
      //	}
      	cout << cnt; 
      	return 0;
       } 
      
      • 1

      Information

      ID
      6823
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      7
      Tags
      # Submissions
      294
      Accepted
      65
      Uploaded By