C++求逆序?qū)Φ姆椒?/h1>
更新時(shí)間:2014年09月17日 11:33:24 投稿:shichen2014
這篇文章主要介紹了C++求逆序?qū)Φ姆椒?包含了字符串常見(jiàn)的操作方法,是非常實(shí)用的技巧,需要的朋友可以參考下
本文實(shí)例講述了C++求逆序?qū)Φ姆椒?,分享給大家供大家參考之用。具體實(shí)現(xiàn)方法如下:
#include <iostream>
#include <vector>
using namespace std;
int array[] = {3, 9, 7, 4, 5, 2};
const int size = sizeof array / sizeof *array;
int temp[size];
//int numbers[size];
int reversePair(int *numbers, int start, int last, int &index, int &count)
{
if(start == last)
return 0;
int mid = (last - start) / 2 + start;
reversePair(numbers, start, mid, index, count);
reversePair(numbers, mid + 1, last, index, count);
for(int i = start; i <= last; i++)
temp[i] = numbers[i];
int index1 = start, index2 = mid + 1;
index = start;
while(index1 <= mid && index2 <= last) {
if(temp[index1] > temp[index2]) {
numbers[index] = temp[index2];
count += mid - index1 + 1;
index++;
index2++;
} else if(temp[index1] == temp[index2]) {
numbers[index] = temp[index1];
index++;
index1++;
index2++;
} else if(temp[index1] < temp[index2]) {
numbers[index] = temp[index1];
index++;
index1++;
}
}
if(index1 <= mid) {
while(index1 <= mid) {
numbers[index] = temp[index1];
index++;
index1++;
}
} else {
while(index2 <= last) {
numbers[index] = temp[index2];
index++;
index2++;
}
}
return count;
}
void main()
{
int count = 0;
int index = 0;
reversePair(array, 0, size - 1, index, count);
cout << "count = " << count << endl;
}
希望本文所述對(duì)大家C++算法設(shè)計(jì)的學(xué)習(xí)有所幫助。
相關(guān)文章
-
OpenCV實(shí)現(xiàn)低對(duì)比度圖像臟污區(qū)域檢測(cè)
本文主要介紹了OpenCV實(shí)現(xiàn)低對(duì)比度圖像臟污區(qū)域檢測(cè),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下 2021-09-09
-
C語(yǔ)言光標(biāo)旋轉(zhuǎn)與倒計(jì)時(shí)功能實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了C語(yǔ)言實(shí)現(xiàn)光標(biāo)旋轉(zhuǎn)與倒計(jì)時(shí)功能的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪 2021-11-11
-
C語(yǔ)言實(shí)現(xiàn)個(gè)人財(cái)務(wù)管理
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)個(gè)人財(cái)務(wù)管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下 2021-11-11
-
c語(yǔ)言實(shí)現(xiàn)基數(shù)排序解析及代碼示例
這篇文章主要介紹了c語(yǔ)言實(shí)現(xiàn)基數(shù)排序解析及代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。 2017-12-12
最新評(píng)論
本文實(shí)例講述了C++求逆序?qū)Φ姆椒?,分享給大家供大家參考之用。具體實(shí)現(xiàn)方法如下:
#include <iostream> #include <vector> using namespace std; int array[] = {3, 9, 7, 4, 5, 2}; const int size = sizeof array / sizeof *array; int temp[size]; //int numbers[size]; int reversePair(int *numbers, int start, int last, int &index, int &count) { if(start == last) return 0; int mid = (last - start) / 2 + start; reversePair(numbers, start, mid, index, count); reversePair(numbers, mid + 1, last, index, count); for(int i = start; i <= last; i++) temp[i] = numbers[i]; int index1 = start, index2 = mid + 1; index = start; while(index1 <= mid && index2 <= last) { if(temp[index1] > temp[index2]) { numbers[index] = temp[index2]; count += mid - index1 + 1; index++; index2++; } else if(temp[index1] == temp[index2]) { numbers[index] = temp[index1]; index++; index1++; index2++; } else if(temp[index1] < temp[index2]) { numbers[index] = temp[index1]; index++; index1++; } } if(index1 <= mid) { while(index1 <= mid) { numbers[index] = temp[index1]; index++; index1++; } } else { while(index2 <= last) { numbers[index] = temp[index2]; index++; index2++; } } return count; } void main() { int count = 0; int index = 0; reversePair(array, 0, size - 1, index, count); cout << "count = " << count << endl; }
希望本文所述對(duì)大家C++算法設(shè)計(jì)的學(xué)習(xí)有所幫助。
相關(guān)文章
OpenCV實(shí)現(xiàn)低對(duì)比度圖像臟污區(qū)域檢測(cè)
本文主要介紹了OpenCV實(shí)現(xiàn)低對(duì)比度圖像臟污區(qū)域檢測(cè),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09C語(yǔ)言光標(biāo)旋轉(zhuǎn)與倒計(jì)時(shí)功能實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了C語(yǔ)言實(shí)現(xiàn)光標(biāo)旋轉(zhuǎn)與倒計(jì)時(shí)功能的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-11-11C語(yǔ)言實(shí)現(xiàn)個(gè)人財(cái)務(wù)管理
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)個(gè)人財(cái)務(wù)管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11c語(yǔ)言實(shí)現(xiàn)基數(shù)排序解析及代碼示例
這篇文章主要介紹了c語(yǔ)言實(shí)現(xiàn)基數(shù)排序解析及代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12