日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Day 20: Sorting

發布時間:2024/1/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Day 20: Sorting 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目:

?

?

C++:

#include <stdio.h> #include <stdlib.h> #include <vector> #include <map> #include <string> #include <exception> #include <stdexcept> #include <algorithm> #include <stack> #include <queue> #include <math.h> #include <iostream>using namespace std; int main() {int n;cin >> n;vector<int> a(n); //聲明動態數組for (int a_i = 0; a_i < n; a_i++) {cin >> a[a_i];}// Write Your Code Hereint numSwaps = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < n-1; j++) {if (a[j] > a[j + 1]) {swap(a[j],a[j+1]); //使用swap()函數,交換兩個變量numSwaps++;}}}cout << "Array is sorted in "<< numSwaps <<" swaps." << endl;cout << "First Element: " << a[0] << endl;cout << "Last Element: " << a[n - 1] << endl;system("pause");return 0; }

python:

#!/bin/python3 #coding:utf-8import sysn = int(input().strip()) a = list(map(int, input().strip().split(' '))) # Write Your Code Here numSwaps=0 for i in range(a.__len__()):for j in range(a.__len__()-1):if a[j]>a[j+1]:temp=a[j]a[j] = a[j + 1]a[j+1]=tempnumSwaps+=1print("Array is sorted in",numSwaps,"swaps.") print("First Element:",a[0]) print("Last Element:",a[n-1])

總結:

C++:

使用vector聲明動態數組:

int n; cin >> n; vector<int> a(n); for (int a_i = 0; a_i < n; a_i++) {cin >> a[a_i]; }

swap()交換函數:

swap(a[j],a[j+1]);

python:

輸入數據,將其轉換為整型:

n = int(input().strip())

將輸入的數據,以空格分開,轉換為數組:

a = list(map(int, input().strip().split(' ')))

list數組的長度:

a.__len__()

python不支持n++這種寫法。

???? 因此,正確的自增操作應該 n = n + 1 或者 n += 1。

?

參考:https://www.cnblogs.com/mlgjb/p/7866941.html

總結

以上是生活随笔為你收集整理的Day 20: Sorting的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。