程序設(shè)計(jì)導(dǎo)論-3 基本算法實(shí)現(xiàn) 代碼實(shí)現(xiàn)

輔助分析的工具

這一部分設(shè)置計(jì)數(shù)器采用的是程序設(shè)計(jì)導(dǎo)論2中厘线,二分查找的部分作為例子。

設(shè)置計(jì)數(shù)器

C

#include<stdio.h>
#include<math.h>
int cnt;
int binarysearch(int arr[],int l,int r,int x){
    if (l >= r)
        cnt += 1 ;
        return -1;
    int mid = l+r/2;
    cnt += 1;
    if (arr[mid]==x){
        return mid;
        cnt += 2; 
    }
    else if(arr[mid] >x){
        return binarysearch(arr,l,mid,x);
        cnt += 3;
    }
    else{
        return binarysearch(arr,mid,r,x);
        cnt += 4;
    }
}
int main(){
    int n;
    int arr[1000];
    int x;
    scanf("%d",&n);
    for(int i = 0;i<n;i++){
        scanf("%d",&arr[i]);
    }
    scanf("%d",&x);
    printf("%d",binarysearch(arr,0,n,x));
    printf("計(jì)數(shù)器指示:%d",cnt);
    return 0;
}

C++

#include<iostream>
#include<cmath>
using namespace std;
int cnt;
int binarysearch(int arr[],int l,int r,int x){
    if (l >= r)
        cnt += 1 ;
        return -1;
    int mid = l+r/2;
    cnt += 1;
    if (arr[mid]==x){
        return mid;
        cnt += 2; 
    }
    else if(arr[mid] >x){
        return binarysearch(arr,l,mid,x);
        cnt += 3;
    }
    else{
        return binarysearch(arr,mid,r,x);
        cnt += 4;
    }
}
int main(){
    int n;
    int arr[1000];
    int x;
    cin>>n;
    for(int i = 0;i<n;i++){
        cin>>arr[i];
    }
    cin>>x;
    cout<<binarysearch(arr,0,n,x)<<endl;
    cout<<"計(jì)數(shù)器指示:"<<cnt<<endl;
    return 0;
}

Java

import java.util.Scanner;
class binarysea{
    public static int cnt = 0;
    public static int binarysearch(int arr[],int l,int r,int x){
        if (l >= r){
            cnt+=1;
            return -1;
        }
        int mid = l+r/2;
        cnt+=1;
        if (arr[mid]==x){
            cnt+=2;
            return mid;
        }
        else if(arr[mid] >x){
            cnt+=3;
            return binarysearch(arr,l,mid,x);
        }
        else{
            cnt+=4;
            return binarysearch(arr,mid,r,x);
        }
    }
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();5
        int arr[] = new int[1000];
        for(int i = 0;i<n;i++){
            arr[i]=sc.nextInt();
        }
        int x = sc.nextInt();
        System.out.println(binarysearch(arr,0,n,x));
        System.out.println("計(jì)數(shù)器指示:"+cnt);
    }
}

Python

arr = input().split(" ")
x = input()
sorted(arr)
cnt = 0
def binarysearch(arr,l,r,x):
    global cnt
    if l >= r:
        cnt += 1 
        return -1
    mid = int((l + r)/2)
    cnt += 1
    if arr[mid] == x:
        cnt += 2 #將判斷條件的過(guò)程計(jì)算進(jìn)去
        return mid
    elif arr[mid] > x:
        cnt += 3 #將之前判斷的過(guò)程計(jì)算進(jìn)去
        return binarysearch(arr,l,mid,x)
    else :
        cnt += 4 #將之前判斷的過(guò)程計(jì)算進(jìn)去
        return binarysearch(arr,mid,r,x)
print(binarysearch(arr,0,len(arr)-1,x))
print("計(jì)數(shù)器指示:{}".format(cnt))

設(shè)置定時(shí)器

這一部分設(shè)置計(jì)數(shù)器采用的是程序設(shè)計(jì)導(dǎo)論2中谒拴,e的近似的部分作為例子臣咖。

C

#include<stdio.h>
#include<math.h>
#include<time.h>
int prod(int n){
    int prd = 1;
    for(int i = 1; i<= n;i++){
        prd = prd * i;
    }
    return prd;
}
int main(){
    int n;
    scanf("%d",&n);
    double e = 0;
    int cnt = 0;
    double item = 1;
    clock_t start_t,end_t;
    start_t = clock();
    while(fabs(item)>pow(0.1,n)){
        item = 1.0 / prod(cnt);
        e = e + item;
        cnt = cnt + 1;
    }
    end_t = clock();
    printf("%lf",e);
    printf("程序運(yùn)行耗時(shí):%f\n", (double)(end - start) / CLOCKS_PER_SEC);
    return 0;
}

C++

#include<iostream>
#include<cmath>
#include<ctime>
using namespace std;
int prod(int n){
    int prd = 1;
    for(int i = 1; i<= n;i++){
        prd = prd * i;
    }
    return prd;
}
int main(){
    int n;
    cin>>n;
    double e = 0;
    int cnt = 0;
    double item = 1;
    clock_t start_t,end_t;
    start_t = clock();
    while(fabs(item)>pow(0.1,n)){
        item = 1.0 / prod(cnt);
        e = e + item;
        cnt = cnt + 1;
    }
    end_t = clock();
    cout<<e<<endl;
    cout<<"程序運(yùn)行耗時(shí):%f\n"<<(double)(end - start) / CLOCKS_PER_SEC)<<endl;
    return 0;
}

Java

import java.util.Scanner;
class eapprox{
    public static int prod(int n){
        int prd = 1;
        for(int i = 1; i<= n;i++){
            prd = prd * i;
        }
        return prd;
    }
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        double e = 0;
        int cnt = 0;
        double item = 1;
        long startTime =  System.nanoTime();
        while(Math.abs(item)>Math.pow(0.1,n)){
            item = 1.0 / prod(cnt);
            e = e + item;
            cnt = cnt + 1;
        }
        long endTime =  System.nanoTime();
        long usedTime = endTime-startTime;
        System.out.println(e);
        System.out.println(usedTime+"ms");
    }
}

Python

import time
n = int(input())
e = 0
cnt = 0
item = 1
def prod(n):
    prd = 1.0
    for i in range(1,n+1):
        prd = prd * i
    return prd
start_time = time.process_time()#程序開(kāi)始
while abs(item) > pow(0.1,n):
    item = 1.0  /prod(cnt)
    e = e + item
    cnt = cnt +1
end_time = time.process_time()#程序結(jié)束
print(e)
print("計(jì)數(shù)器指示:{}".format(end_time-start_time))

設(shè)置進(jìn)程資源檢測(cè)

大數(shù)四則運(yùn)算

加法

C

#include<string.h>
#include<stdio.h>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
char a[1000000];
char b[1000000];
char c[1000001];
void add(char a[],char b[]){
    strrev(a);
    strrev(b);
    int tem = 0;
    int cnt = 0;
    int len_a = strlen(a);
    int len_b = strlen(b);
    int item = 0;
    for(int i = 0 ; i < min(len_a,len_b);i++){
        item = a[i]-'0'+b[i]-'0'+tem;
        if (item>10){
            tem = 1;
            item %= 10;
        }
        else{
            tem = 0;
        }
        c[cnt++]=item+'0';
    }
    if(len_a>len_b){
        for(int i = len_b;i<len_a;i++){
            item = a[i]-'0'+tem;
            if(item >= 10){
                tem = 1;
                item %= 10;
            }
            else{
                tem = 0;
            }
            c[cnt++]=item+'0';
        }
    }
    if(len_b>len_a){
        for(int i = len_a;i<len_b;i++){
            item = a[i]-'0'+tem;
            if(item >= 10){
                tem = 1;
                item %= 10;
            }
            else{
                tem = 0;
            }
            c[cnt++]=item+'0';
        }
    }
    if (tem != 0){
        c[cnt++]=tem+'0';
    }
    strrev(c);
}
int main(){
    scanf("%s %s",&a,&b);
    add(a,b);
    printf(c);
    return 0;
}

C++

#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
char a[1000000];
char b[1000000];
char c[1000001];
void add(char a[],char b[]){
    strrev(a);
    strrev(b);
    int tem = 0;
    int cnt = 0;
    int len_a = strlen(a);
    int len_b = strlen(b);
    int item = 0;
    for(int i = 0 ; i < min(len_a,len_b);i++){
        item = a[i]-'0'+b[i]-'0'+tem;
        if (item>10){
            tem = 1;
            item %= 10;
        }
        else{
            tem = 0;
        }
        c[cnt++]=item+'0';
    }
    if(len_a>len_b){
        for(int i = len_b;i<len_a;i++){
            item = a[i]-'0'+tem;
            if(item >= 10){
                tem = 1;
                item %= 10;
            }
            else{
                tem = 0;
            }
            c[cnt++]=item+'0';
        }
    }
    if(len_b>len_a){
        for(int i = len_a;i<len_b;i++){
            item = a[i]-'0'+tem;
            if(item >= 10){
                tem = 1;
                item %= 10;
            }
            else{
                tem = 0;
            }
            c[cnt++]=item+'0';
        }
    }
    if (tem != 0){
        c[cnt++]=tem+'0';
    }
    strrev(c);
}
int main(){
    cin>>a>>b;
    add(a,b);
    cout<<c<<endl;
    return 0;
}

Java

Python

def add(a,b):
    a = a[::-1]
    b = b[::-1]
    c = []
    tem = 0
    for x,y in zip(a,b):
        item = int(x)+int(y)+tem
        if item >= 10:
            tem = 1
            item %= 10
        else:
            tem = 0
        c.append(str(item))
    if len(a) > len(b):
        for i in range(len(b),len(a)):
            item = int(a[i])+tem
            if item >= 10:
                tem = 1
                item %= 10
            else:
                tem = 0
            c.append(str(item))
    if len(a) < len(b):
        for i in range(len(a),len(b)):
            item = int(b[i])+tem
            if item >= 10:
                tem = 1
                item %= 10
            else:
                tem = 0
            c.append(str(item))
    if tem != 0:
        c.append(str(tem))
    c = "".join(c)
    return c[::-1]
a,b = input().split(" ")
print(add(a,b))

減法

C

#include<string.h>
#include<stdio.h>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
char a[1000000];
char b[1000000];
char c[1000001];
void sub(char a[],char b[]){
    strrev(a);
    strrev(b);
    int tem = 0;
    int cnt = 0;
    int len_a = strlen(a);
    int len_b = strlen(b);
    int item = 0;
    for(int i = 0; i < min(len_a,len_b);i++){
        if(a[i]+tem>=b[i]){
            item = a[i]-'0'+tem-(b[i]-'0');
            tem = 0;
        }
        else{
            item = a[i]-'0'+10+tem-(b[i]-'0');
            tem = -1;
        }
        c[cnt++]=item+'0';
    }
    if(len_a>len_b){
        for(int i = len_b;i<len_a;i++){
            item = a[i]-'0'+tem;
            if(item<0){
                tem = -1;
                item += 10;
            }
            else{
                tem = 0;
            }
            c[cnt++] = item+'0';
        }
    }
    strrev(c);
}
int main(){
    scanf("%s %s",&a,&b);
    sub(a,b);
    printf(c);
    return 0;
}

C++

#include<string.h>
#include<iostream>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
using namespace std;
char a[1000000];
char b[1000000];
char c[1000001];
void sub(char a[],char b[]){
    strrev(a);
    strrev(b);
    int tem = 0;
    int cnt = 0;
    int len_a = strlen(a);
    int len_b = strlen(b);
    int item = 0;
    for(int i = 0; i < min(len_a,len_b);i++){
        if(a[i]+tem>=b[i]){
            item = a[i]-'0'+tem-(b[i]-'0');
            tem = 0;
        }
        else{
            item = a[i]-'0'+10+tem-(b[i]-'0');
            tem = -1;
        }
        c[cnt++]=item+'0';
    }
    if(len_a>len_b){
        for(int i = len_b;i<len_a;i++){
            item = a[i]-'0'+tem;
            if(item<0){
                tem = -1;
                item += 10;
            }
            else{
                tem = 0;
            }
            c[cnt++] = item+'0';
        }
    }
    strrev(c);
}
int main(){
    cin>>a>>b;
    sub(a,b);
    cout<<c<<endl;
    return 0;
}

Java

Python

def sub(a,b):
    a = a[::-1]
    b = b[::-1]
    c = []
    tem = 0
    for x,y in zip(a,b):
        if int(x)+tem >= int(y):
            item = int(x)+tem-int(y)
            tem = 0
        else:
            item = int(x)+tem+10-int(y)
            tem = -1
        c.append(str(item))
    if len(a) > len(b):
        for i in range(len(b),len(a)):
            item = int(a[i])+tem
            if item < 0:
                tem = -1
                item += 10
            else:
                tem = 0
            c.append(str(item))
    c = "".join(c)
    return c[::-1]
a,b = input().split(" ")
print(sub(a,b))

乘法

除法

素?cái)?shù)篩

暴力算法

C

#include<stdio.h>
#include<math.h>
int a[10000000];
int violet_sieve_core(int num){
    if (num<=1){
        return 0;
    }
    for(int i = 2;i < num;i++){
        if(num%i == 0){
            return 0;
        }
    }
    return 1;
}
int cnt;
void violet_sieve(int n){
    for(int i = 2;i < n+1;i++){
        if(violet_sieve_core(i)){
            a[cnt++]=i;
        }
    }
}
int main(){
    int n;
    scanf("%d",&n);
    violet_sieve(n);
    for(int i = 0 ;i<cnt;i++){
        printf("%d ",a[i]);
    }
    return 0;
}

C++

#include<iostream>
#include<cmath>
int a[10000000];
int violet_sieve_core(int num){
    if (num<=1){
        return 0;
    }
    for(int i = 2;i < num;i++){
        if(num%i == 0){
            return 0;
        }
    }
    return 1;
}
int cnt;
void violet_sieve(int n){
    for(int i = 2;i < n+1;i++){
        if(violet_sieve_core(i)){
            a[cnt++]=i;
        }
    }
}
int main(){
    int n;
    cin>>n;
    violet_sieve(n);
    for(int i = 0 ;i<cnt;i++){
        cout<<a[i]<<endl;
    }
    return 0;
}

Java

Python

def violet_sieve(num):
    if num < 2:
        return False
    for i in range(2,num):
        if num % i == 0:
            return False
    return True
n = int(input())
for i in range(2,n+1):
    if violet_sieve(i):
        print(i)

埃篩

C

#include<stdio.h>
#include<math.h>
int a[10000000];
int cnt;
int p[100000000];
void eratos_sieve(int n){
    for(int i = 2;i<n;i++){
        for(int j = 2; i *j <= n;j++){
            p[i*j] = 0;
        }
        if(!p[i]){
            a[cnt++]=i;
        }
    }
}
int main(){
    int n;
    scanf("%d",&n);
    eratos_sieve(n);
    for(int i = 0 ;i<cnt;i++){
        printf("%d ",a[i]);
    }
    return 0;
}

C++

#include<iostream>
#include<cmath>
int a[10000000];
int cnt;
int p[100000000];
void eratos_sieve(int n){
    for(int i = 2;i<n;i++){
        for(int j = 2; i *j <= n;j++){
            p[i*j] = 0;
        }
        if(!p[i]){
            a[cnt++]=i;
        }
    }
}
int main(){
    int n;
    cin>>n;
    eratos_sieve(n);
    for(int i = 0 ;i<cnt;i++){
        cout<<a[i]<<endl;
    }
    return 0;
}

Java

Python

def eratos_sieve(n):
    primes = [True] * (n+1)
    p = 2
    while p*p <= n:
        if primes[p]:
            for i in range(p*2,n+1,p):
                primes[i] = False
        p += 1
    primes = [element for element in range(2,n) if primes[element]]
    return primes
n = int(input())
for i in eratos_sieve(n):
    print(i)

歐拉篩

C

#include<stdio.h>
#include<math.h>
int a[10000000];
int cnt;
int p[100000000];
void euler_sieve(int n){
    for(int i = 2;i<n;i++){
        if(!p[i]){
            a[cnt++]=i;
        }
        for(int j = 0; j<cnt &&i *a[j] <= n;j++){
            p[i*a[j]]= 1;
            if(i % a[j] == 0) break;
        }
    }
}
int main(){
    int n;
    scanf("%d",&n);
    euler_sieve(n);
    for(int i = 0 ;i<cnt;i++){
        printf("%d ",a[i]);
    }
    return 0;
}

C++

#include<iostream>
#include<cmath>
using namespace std;
int a[10000000];
int cnt;
int p[100000000];
void euler_sieve(int n){
    for(int i = 2;i<n;i++){
        if(!p[i]){
            a[cnt++]=i;
        }
        for(int j = 0; j<cnt &&i *a[j] <= n;j++){
            p[i*a[j]]= 1;
            if(i % a[j] == 0) break;
        }
    }
}
int main(){
    int n;
    cin>>n;
    euler_sieve(n);
    for(int i = 0 ;i<cnt;i++){
        cin>>a[i]<<endl;
    }
    return 0;
}

Java

Python

def euler_sieve(n):
    cnt = 0
    flag = [True] * (n+1)
    primes = []
    for i in range(2,n):
        if flag[i]:
            primes.append(i)
            cnt += 1
        j = 0
        while j < cnt and i * prime[j] < n:
            flag[i*prime[j]] = False
            if i%prime[j] == 0:
                break
            j += 1
    return primes
n = int(input())
for i in euler_sieve(n):
    print(i)

求最大公約數(shù)

輾轉(zhuǎn)相除法

C

#include<stdio.h>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
int gcd(int a,int b){
    int a_ = max(a,b);
    int b_ = min(a,b);
    return b==0?a:gcd(b,a%b);
}

int main(){
    int m,n;
    scanf("%d %d",&m,&n);
    printf("%d",gcd(m,n));
    return 0;
}

C++

#include<iostream>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
using namespace std;
int gcd(int a,int b){
    int a_ = max(a,b);
    int b_ = min(a,b);
    return b==0?a:gcd(b,a%b);
}

int main(){
    int m,n;
    cin>>m>>n;
    cout<<gcd(m,n)<<endl;
    return 0;
}

Java

Python

def gcd(a,b):
    a,b = max(a,b),min(a,b)
    if b == 0:
        return a
    else:
        return gcd(b,a%b)
m,n = [int(i) for i in input().split()]
print(gcd(m,n))

stein算法

C

#include<stdio.h>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)

int stein(int m,int n){
    int a = max(m,n);
    int b = min(m,n);
    if(b == 0){
        return a;
    }
    if(a%2==0 && b%2==0){
        return stein(a>>1,b>>1)<<1;
    }
    if(a%2==0){
        return stein(a>>1,b);
    }
    if(b%2==0){
        return stein(a,b>>1);
    }
    return stein((a+b)>>1,(a-b)>>1);
}
int main(){
    int m,n;
    scanf("%d %d",&m,&n);
    printf("%d",stein(m,n));
    return 0;
}

C++

#include<iostream>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
using namespace std;
int stein(int m,int n){
    int a = max(m,n);
    int b = min(m,n);
    if(b == 0){
        return a;
    }
    if(a%2==0 && b%2==0){
        return stein(a>>1,b>>1)<<1;
    }
    if(a%2==0){
        return stein(a>>1,b);
    }
    if(b%2==0){
        return stein(a,b>>1);
    }
    return stein((a+b)>>1,(a-b)>>1);
}
int main(){
    int m,n;
    cin>>m>>n;
    cout<<stein(m,n)<<endl;
    return 0;
}

Java

Python

def stein(a,b):
    a,b = max(a,b),min(a,b)
    if b == 0:
        return a
    if a % 2 == 0 and b % 2 == 0:
        return stein(a>>1,b>>1)<<1
    if a % 2 == 0:
        return stein(a>>1,b)
    if b % 2 == 0:
        return stein(a,b>>1)
    return stein((a+b)>>1,(a-b)>>1)
m,n = [int(i) for i in input().split()]
print(stein(m,n))

排序算法

插入排序

C

#include<stdio.h>
void insert_sort(int arr[], int len){
    int i,j,key;
    for (i=1;i<len;i++){
        key = arr[i];
        j=i-1;
        while((j>=0) && (arr[j]>key)) {
            arr[j+1] = arr[j];
            j--;
        }
        arr[j+1] = key;
    }
}
int main(){
    int n;
    int arr[100000];
    scanf("%d",&n);
    for(int i = 0 ;i < n;i++){
        scanf("%d",&arr[i]);
    }
    insert_sort(arr,n);
    for(int i = 0 ;i < n;i++){
        printf("%d ",arr[i]);
    }
}

C++

#include<iostream>
using namespace std;
void insert_sort(int arr[], int len){
    int i,j,key;
    for (i=1;i<len;i++){
        key = arr[i];
        j=i-1;
        while((j>=0) && (arr[j]>key)) {
            arr[j+1] = arr[j];
            j--;
        }
        arr[j+1] = key;
    }
}
int main(){
    int n;
    int arr[100000];
    scanf("%d",&n);
    for(int i = 0 ;i < n;i++){
        scanf("%d",&arr[i]);
    }
    insert_sort(arr,n);
    for(int i = 0 ;i < n;i++){
        printf("%d ",arr[i]);
    }
}

Java

Python

def insertsort(arr):
    n = len(arr)
    for i in range(1,n):
        key = arr[i]
        j = i - 1
        while j >= 0 and key < arr[j]:
            arr[j+1]=arr[j]
            j -= 1
        arr[j+1] = key
arr = [int(i)for i in input().split(" ")]
insertsort(arr)
print(arr)

快速排序

C

#include<stdio.h>
void swap(int *x, int *y) {
    int t = *x;
    *x = *y;
    *y = t;
}
void quick_sort_recursive(int arr[], int start, int end) {
    if (start >= end)
        return;
    int mid = arr[end];
    int left = start, right = end - 1;
    while (left < right) {
        while (arr[left] < mid && left < right)
            left++;
        while (arr[right] >= mid && left < right)
            right--;
        swap(&arr[left], &arr[right]);
    }
    if (arr[left] >= arr[end])
        swap(&arr[left], &arr[end]);
    else{
        left++;
        swap(&arr[left], &arr[end]);
    }
    if (left)
        quick_sort_recursive(arr, start, left - 1);
    quick_sort_recursive(arr, left + 1, end);
}

void quick_sort(int arr[], int len) {
    quick_sort_recursive(arr, 0, len - 1);
}
int main(){
    int n;
    int arr[100000];
    scanf("%d",&n);
    for(int i = 0 ;i < n;i++){
        scanf("%d",&arr[i]);
    }
    quick_sort(arr,n);
    for(int i = 0 ;i < n;i++){
        printf("%d ",arr[i]);
    }
}

C++

#include<iostream>
using namespace std;
void swap(int *x, int *y) {
    int t = *x;
    *x = *y;
    *y = t;
}
void quick_sort_recursive(int arr[], int start, int end) {
    if (start >= end)
        return;
    int mid = arr[end];
    int left = start, right = end - 1;
    while (left < right) {
        while (arr[left] < mid && left < right)
            left++;
        while (arr[right] >= mid && left < right)
            right--;
        swap(&arr[left], &arr[right]);
    }
    if (arr[left] >= arr[end])
        swap(&arr[left], &arr[end]);
    else{
        left++;
        swap(&arr[left], &arr[end]);
    }
    if (left)
        quick_sort_recursive(arr, start, left - 1);
    quick_sort_recursive(arr, left + 1, end);
}

void quick_sort(int arr[], int len) {
    quick_sort_recursive(arr, 0, len - 1);
}
int main(){
    int n;
    int arr[100000];
    cin>>n;
    for(int i = 0 ;i < n;i++){
        cin>>arr[i];
    }
    quick_sort(arr,n);
    for(int i = 0 ;i < n;i++){
        cin>>arr[i];
    }
}

Java

Python

def quick_sort(arr, first, last):
    if first >= last:
        return
    mid = arr[first]
    low = first
    high = last
    while low < high:
        while low < high and arr[high] >= mid:
            high -= 1
        arr[low] = arr[high]
        while low < high and arr[low] < mid:
            low += 1
        arr[high] = arr[low]
    arr[low] = mid
    quick_sort(arr, first, low-1)
    quick_sort(arr, low+1, last)
arr = [int(i)for i in input().split(" ")]
quick_sort(arr,0,len(arr)-1)
print(arr)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末署咽,一起剝皮案震驚了整個(gè)濱河市自点,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌久妆,老刑警劉巖晌杰,帶你破解...
    沈念sama閱讀 212,222評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異筷弦,居然都是意外死亡肋演,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,455評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門(mén)烂琴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)爹殊,“玉大人,你說(shuō)我怎么就攤上這事奸绷」?洌” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 157,720評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵号醉,是天一觀的道長(zhǎng)反症。 經(jīng)常有香客問(wèn)我,道長(zhǎng)畔派,這世上最難降的妖魔是什么惰帽? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,568評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮父虑,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘授药。我一直安慰自己士嚎,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,696評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布悔叽。 她就那樣靜靜地躺著莱衩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪娇澎。 梳的紋絲不亂的頭發(fā)上笨蚁,一...
    開(kāi)封第一講書(shū)人閱讀 49,879評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音趟庄,去河邊找鬼括细。 笑死,一個(gè)胖子當(dāng)著我的面吹牛戚啥,可吹牛的內(nèi)容都是我干的奋单。 我是一名探鬼主播,決...
    沈念sama閱讀 39,028評(píng)論 3 409
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼猫十,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼览濒!你這毒婦竟也來(lái)了呆盖?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,773評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤贷笛,失蹤者是張志新(化名)和其女友劉穎应又,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體乏苦,經(jīng)...
    沈念sama閱讀 44,220評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡株扛,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,550評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了邑贴。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片席里。...
    茶點(diǎn)故事閱讀 38,697評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖拢驾,靈堂內(nèi)的尸體忽然破棺而出奖磁,到底是詐尸還是另有隱情,我是刑警寧澤繁疤,帶...
    沈念sama閱讀 34,360評(píng)論 4 332
  • 正文 年R本政府宣布咖为,位于F島的核電站,受9級(jí)特大地震影響稠腊,放射性物質(zhì)發(fā)生泄漏躁染。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,002評(píng)論 3 315
  • 文/蒙蒙 一架忌、第九天 我趴在偏房一處隱蔽的房頂上張望吞彤。 院中可真熱鬧,春花似錦叹放、人聲如沸饰恕。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,782評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)埋嵌。三九已至,卻和暖如春俱恶,著一層夾襖步出監(jiān)牢的瞬間雹嗦,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,010評(píng)論 1 266
  • 我被黑心中介騙來(lái)泰國(guó)打工合是, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留了罪,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,433評(píng)論 2 360
  • 正文 我出身青樓端仰,卻偏偏與公主長(zhǎng)得像捶惜,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子荔烧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,587評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容