|
|
用了C++和Python写第一次作业。先写心得再贴代码。总的来说目标如下:想写的代码比较通用,比如适应不同输入的文件大小以及空行的处理;还有就是要用语言的特点;再者程序要简短但是不失可读性,所以我一般用长变量名,压缩行数;最后,就是尽量进行对数据批量操作(当初学Matlab养成的习惯), 可能的话避免for循环。想来在这个位置就不需要用白色了吧。
C++:
1 关于空行的处理,用getline判断string长度是否为零,是则略过该行。
2 使用了C++里面的vector,很好用,建议大家玩一下,自从用了这个,再也不想用int*了。
3 由于MergeSort比较难以实现in-place-operation,所以需要在vector里面大规模复制数据,这点我用for循环做的,估计比较费时了,不知有没有更好的办法。
4 之前有错,错在下标,一要仔细,还有就是先在纸上写写,不要直接敲代码。
5 这个是唯一没有自己检查出来的地方,数据溢出,要用unsigned long或者long long....orz- #include <fstream>
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <cstdio>
- #include <cstdlib>
- #include <vector>
- using namespace std;
- void copyVector(vector<int> &outVec, vector<int> &inVec, int low, int high) {
- outVec.clear();
- for (int k = low; k <= high; k++) {
- outVec.push_back(inVec[k]);
- }
- }
- void mergeSortInv(vector<int> &arr, int low, int high, long long &invCount) {
- if (high - low < 1) return;
- int pivot = (high + low) / 2;
- mergeSortInv(arr, low, pivot, invCount);
- mergeSortInv(arr, pivot + 1, high, invCount);
- vector<int> temparr;
- copyVector(temparr, arr, low, high);
- int i = 0, j = pivot - low + 1, idx = low, mid = pivot - low;
- while (i <= mid && j <= high - low) {
- if (temparr[i] > temparr[j]) {
- arr[idx] = temparr[j];
- j++;
- invCount = invCount + mid - i + 1;
- } else {
- arr[idx] = temparr[i];
- i++;
- }
- idx++;
- }
- while (i <= mid) {
- arr[idx] = temparr[i];
- idx++;
- i++;
- }
- while (j <= high - low) {
- arr[idx] = temparr[j];
- idx++;
- j++;
- }
- return;
- }
- int main() {
- string InputFileName = "IntegerArray.txt";
- ifstream infile(InputFileName.c_str());
- if (!infile.is_open()) {
- cerr << "Error with file opening";
- exit(1);
- }
- vector<int> IntArray, NewArray;
- string templine;
- while (!infile.eof()) {
- getline(infile, templine);
- if (templine.size() == 0)
- continue;
- IntArray.push_back(std::atoi(templine.c_str()));
- }
- infile.close();
- cout << IntArray.size() << " numbers acquired." << endl;
- NewArray = IntArray;
- long long InvCount = 0;
- mergeSortInv(NewArray, 0, NewArray.size() - 1, InvCount);
- cout <<"Total # of inversion pairs is:"<< InvCount;
- return 0;
- }
复制代码 Python:
最近才开始正式的用python,之前的都是小打小闹,
1 我的def的函数要返回两个值,一个是sorted list,一个是inv #。 查了一下,用tuple做的。
2 利用list的方法,最后写出来的东西相当简洁,核心函数15行,总共不到20来行,我自己也惊奇了一下。- def mergeSortInv(toSort,ctInv):
- if len(toSort)<=1: return (toSort, ctInv)
- pivot = len(toSort)/2
- (left, ctInv) = mergeSortInv(toSort[:pivot],ctInv)
- (right, ctInv) = mergeSortInv(toSort[pivot:],ctInv)
- result=[]
- while ( left ) and ( right):
- if left[0]>right[0]:
- ctInv=ctInv+len(left)
- result.append(right.pop(0))
- else:
- result.append(left.pop(0))
- result.extend(left)
- result.extend(right)
- return (result, ctInv)
- InputFileName = "IntegerArray.txt"
- FileIn = open(InputFileName, 'r')
- IntArray = map(int, FileIn.readlines())
- FileIn.close()
- print "Read " + str(len(IntArray)) + " integers from file: " + InputFileName
- (res, invCount)=mergeSortInv(IntArray,0)
- print "The # of inversion paris is \t %d" %(invCount)
复制代码 |
|