forked from openFudan/fudan-coursera
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
153 changed files
with
3,142 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <iostream> | ||
#include <algorithm> | ||
int partition (int *a,int l,int r){ | ||
int fence = r; | ||
--r; | ||
while (l <= r) | ||
{ | ||
while (l <= r) | ||
{ | ||
if (a[l] > a[fence]) | ||
{ | ||
std::swap(a[l], a[fence]); | ||
fence = l; | ||
l++; | ||
break; | ||
} | ||
l++; | ||
} | ||
while (l <= r) | ||
{ | ||
if (a[r] < a[fence]) | ||
{ | ||
std::swap(a[r], a[fence]); | ||
fence = r; | ||
r--; | ||
break; | ||
} | ||
r--; | ||
} | ||
|
||
} | ||
return fence; | ||
} | ||
|
||
void quickSort (int *a,int l,int r ){ | ||
if (l <= r) { | ||
int p = partition(a, l, r); | ||
quickSort(a, l, p - 1); | ||
quickSort(a, p + 1, r); | ||
} | ||
} | ||
int main() | ||
{ | ||
int a[9] = { 9,1,8,2,7,3,6,4,5 }; | ||
quickSort(a, 0, 8); | ||
for (int i = 0; i<9; ++i) | ||
std::cout << a[i] << std::endl; | ||
getchar(); | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
#ifndef ARRAY_CLASS | ||
#define ARRAR_CLASS | ||
#include<iostream> | ||
#include<cstdlib> | ||
using namespace std; | ||
#ifndef NULL | ||
const int NULL = 0; | ||
#endif //����NULL=0 | ||
//�������ͼ��� | ||
enum ErrorType | ||
{ | ||
invalidArraySize, memoryAllocationError, indexOutOfRange | ||
|
||
}; | ||
//������Ϣ | ||
char *errorMsg[] = | ||
{ | ||
"invalidArraySize", | ||
"Memory allocation error", | ||
"Invalid index:" | ||
}; | ||
|
||
//������ģ������ | ||
template <class T> | ||
class Array | ||
{ | ||
private: | ||
T* alist; //Tָ�����ͣ����ڴ�Ŷ�̬����������ڴ���ַ | ||
int size;//�����С | ||
void Error(ErrorType error, int badIndex = 0) const;//���������� | ||
public: | ||
Array(int sz = 50); | ||
Array(const Array<T>& A);//�������캯�� | ||
~Array(void);//�������� | ||
Array<T>&operator =(const Array<T> &rhs);//���ء�=��ʹ���������������帳ֵ | ||
T& operator[](int i);//����[]��ʹ��Array ���������C++��ͨ��������� | ||
operator T*(void)const;//����T*,ʹ��Array���������C++��ͨ��������� | ||
int ListSize(void)const;//ȡ�����С�� | ||
void Resize(int sz);//������Ĵ�С | ||
|
||
}; | ||
//����Ϊ���Ա�����Ķ��� | ||
//ģ�庯��Errorʵ�����������Ϣ�Ĺ��� | ||
template<class T> | ||
void Array<T>::Error(ErrorType error, int badIndex)const | ||
{ | ||
cout << errorMsg[error]; | ||
if (error == indexOutOfRange) | ||
cout << badIndex; | ||
cout << endl; | ||
exit(1); | ||
} | ||
|
||
//���캯�� | ||
template <class T> | ||
Array<T>::Array(int sz) | ||
{ | ||
if (sz <= 0) | ||
Error(invalidArraySize); | ||
size = sz;//��Ԫ�ظ�����ֵ������size | ||
alist = new T[size];//��̬�����ڴ棬��size��T���͵�Ԫ�ؿռ������� | ||
if (alist == NULL) | ||
Error(memoryAllocationError); | ||
|
||
} | ||
//�������� | ||
template<class T> | ||
Array<T>::~Array(void) | ||
{ | ||
delete[]alist; | ||
} | ||
|
||
//�������캯�� | ||
template <class T> | ||
Array<T>::Array(const Array<T>& X) | ||
{ | ||
//�Ӷ���Xȡ�������С����������ǰ�����Ա | ||
int n = X.size; | ||
size = n; | ||
//Ϊ���������ڴ沢���г������ | ||
alist = new T[n]; | ||
if (alist == NULL) | ||
Error(memoryAllocationError); | ||
//�Ӷ���X��������Ԫ�ص������� | ||
T* srcptr = X.alist;//X.alist�Ƕ���X��������ַ | ||
T* destptr = alist;//alist�DZ������е�������ַ | ||
while (n--) | ||
*destptr++ = *srcptr++; | ||
} | ||
|
||
//����'=' | ||
template<class T> | ||
Array<T>& Array<T>::operator =(const Array<T> &rhs) | ||
{ | ||
int n = rhs.size;//ȡrhs����Ĵ�С | ||
//����������е������С��rhs��ͬ����ɾ������ԭ�е��ڴ棬Ȼ�����·��� | ||
if (size != n) | ||
{ | ||
delete[]alist; | ||
alist = new T[n]; | ||
if (alist == NULL) | ||
Error(memoryAllocationError); | ||
size = n; | ||
} | ||
|
||
//��rhs������Ԫ�� | ||
T* destptr = alist; | ||
T* srcptr = rhs.alist; | ||
while (n--) | ||
*destptr++ = *srcptr++; | ||
return *this;//���ص�ǰ��������� | ||
} | ||
|
||
//����[] | ||
template <class T> | ||
T &Array<T>::operator[](int n) | ||
{ | ||
if (n<0 || n>size - 1) | ||
Error(indexOutOfRange, n); | ||
return alist[n+1]; | ||
} | ||
|
||
//����ָ��ת������ | ||
template <class T> | ||
Array<T>::operator T*(void)const | ||
{ | ||
return alist; | ||
} | ||
|
||
//ȡ��ǰ�����С | ||
template<class T> | ||
int Array<T>::ListSize(void)const | ||
{ | ||
return size; | ||
} | ||
|
||
//�������С��Ϊsz | ||
template <class T> | ||
void Array<T>::Resize(int sz) | ||
{ | ||
if (sz <= 0) | ||
Error(invalidArraySize); | ||
if (sz == size) | ||
return; | ||
T *newlist = new T[sz]; | ||
if (newlist == NULL) | ||
Error(memoryAllocationError); | ||
int n = (sz <= size) ? sz : size; | ||
T *srcptr = alist; | ||
T *destptr = newlist; | ||
while (n--) | ||
*destptr++ = *srcptr++; | ||
delete[]alist; | ||
alist = newlist; | ||
size = sz; | ||
} | ||
#endif //ARRAY_CLASS | ||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <iostream> | ||
using namespace std; | ||
//n a1 a2 ... an | ||
//T | ||
//b1 b2.. bi | ||
|
||
void main() | ||
{ | ||
int n,T,i,k; | ||
cin>>n; | ||
int *p= new int[n]; | ||
for(i=0;i<n;i++) | ||
{ | ||
cin>>p[i]; | ||
} | ||
cin>>T; | ||
int *q= new int[T]; | ||
for(i=0;i<T;i++) | ||
{ | ||
cin>>q[i]; | ||
} | ||
|
||
int *p1= new int[n]; | ||
for(i=0;i<n;i++) | ||
{ | ||
p1[i]=n; | ||
} | ||
|
||
for(i=0;i<n;i++) | ||
for(k=0;k<n;k++) | ||
{ | ||
if(p[i]>p[k]) | ||
p1[i]--; | ||
} | ||
for(i=0;i<T;i++) | ||
{ | ||
for(k=0;k<n;k++) | ||
if(p1[k]==q[i]) | ||
cout<<p[k]<<endl; | ||
} | ||
|
||
cout<<"Hello World"<<endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <iostream> | ||
#include <istream> | ||
#include <ostream> | ||
using namespace std; | ||
//n a1 a2 ... an | ||
//k b1 b2 ... bk | ||
//k y n | ||
void main() | ||
{ | ||
int k,i,m,n; | ||
cin>>n; | ||
int *p=new int[n]; | ||
for(i=0;i<n;i++) | ||
{ | ||
cin>>p[i]; | ||
} | ||
cin>>k; | ||
int *q=new int[k]; | ||
for(i=0;i<k;i++) | ||
{ | ||
cin>>q[i]; | ||
} | ||
|
||
int *q1=new int[k]; | ||
for(i=0;i<k;i++) | ||
{ | ||
q1[i]=0; | ||
} | ||
|
||
|
||
|
||
for(m=0;m<k;m++) | ||
for(i=0;i<n;i++) | ||
{ | ||
if(q[m]==p[i]) q1[m]=1; | ||
} | ||
for(m=0;m<k;m++) | ||
if(q1[m]==1) cout<<"y"<<endl; | ||
else cout<<"n"<<endl; | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include"q1.h" | ||
#include<iostream> | ||
#include<string> | ||
using namespace std; | ||
void findandset(string pre, string mid, BTNODE<char>& p)//build up a tree by two strings | ||
{ | ||
|
||
string a, b; | ||
char c = pre[0]; | ||
|
||
p.data = c; | ||
|
||
char t; int i, j, m = 0, k; | ||
if (pre.length() == 2) | ||
{ | ||
for (i = 0; mid[i] != '\0'; i++) | ||
if (mid[i] == c) break; | ||
if (i == 0) | ||
{ | ||
p.fch = NULL; | ||
p.rch = new BTNODE<char>(); | ||
p.rch->data = mid[1]; | ||
} | ||
if (i == 1) | ||
{ | ||
p.rch = NULL; | ||
p.fch = new BTNODE<char>(); | ||
p.fch->data = mid[0]; | ||
} | ||
} | ||
else if (pre.length() == 3) | ||
{ | ||
p.fch = new BTNODE<char>(); | ||
p.rch = new BTNODE<char>(); | ||
p.fch->data = mid[0]; | ||
p.rch->data = mid[2]; | ||
} | ||
else { | ||
p.fch = new BTNODE<char>('a'); | ||
|
||
p.rch = new BTNODE<char>('a'); | ||
for (i = 0; mid[i] != '\0'; i++) | ||
if (mid[i] == c) break; | ||
|
||
for (j = 0; j < i; j++) | ||
for (k = 0; pre[k] != '\0'; k++) | ||
if ((mid[j] == pre[k]) && k>m) m = k; | ||
|
||
a = pre.substr(1, m); | ||
b = mid.substr(0, i); | ||
findandset(a, b, *(p.fch)); | ||
|
||
a = pre.substr(m + 1, pre.length() - m - 1); | ||
b = mid.substr(i + 1, mid.length() - i - 1); | ||
findandset(a, b, *(p.rch)); | ||
} | ||
} | ||
void output(BTNODE<char>& r)//output the tree by backorder | ||
{ | ||
BTNODE<char>* p; | ||
p = r.fch; | ||
if (p != NULL) | ||
output(*p); | ||
p = r.rch; | ||
if (p != NULL) | ||
output(*p); | ||
cout << r.data; | ||
} | ||
int main() | ||
{ | ||
BTNODE<char> r; | ||
string pre, mid; | ||
|
||
getline(cin, pre); | ||
getline(cin, mid); | ||
findandset(pre, mid, r); | ||
output(r); | ||
|
||
|
||
|
||
return 0; | ||
} | ||
|
||
|
||
|
Oops, something went wrong.