Last active
August 29, 2015 14:11
-
-
Save Anebrithien/3b5dcf19575849ed3eab to your computer and use it in GitHub Desktop.
某日从聊天记录里翻出来的C++代码
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
/*没记错的话,应该是从n个元素中选出m个。当时以英文字母a~z加上数字0~9为元素范围,那么n最大是36了。 | |
印象里1对应该元素被输出,0则不输出,然后……然后??? | |
不过,三年过去了,完全看不懂了怎么办?! | |
似乎去掉system("pause")就能在Linux上编译通过了 | |
*/ | |
#include <fstream> | |
#include <iostream> | |
using namespace std; | |
#define getbit(bits, bit_index) ( bits[bit_index / sizeof(char)] & (1<< (bit_index % sizeof(char))) ) | |
#define setzero(bits, bit_index) ( bits[bit_index / sizeof(char)] &= ~(1<< (bit_index % sizeof(char))) ) | |
#define setone(bits, bit_index) ( bits[bit_index / sizeof(char)] |= (1<< (bit_index % sizeof(char))) ) | |
#define min(a,b) ((a) < (b) ? a:b) | |
#define max(a,b) ((a) < (b) ? b:a) | |
template <typename T> | |
class ListAll{ | |
public : | |
ListAll(){} | |
~ListAll(){if (bits == 0) delete[] bits;} | |
void set_vec(T v[], int n){vec = v; this->n = n;} | |
int list(int m); | |
private: | |
void print_one(); | |
char* bits; | |
T* vec; | |
int n; | |
int m; | |
ofstream fout ; | |
}; | |
template <typename T> | |
void ListAll<T>::print_one(){ | |
int c = 0; | |
for(int i = 0; i < n, c < m; i++){ | |
if(getbit(bits, i) == 1){ | |
fout << vec[i]; | |
c++; | |
} | |
} | |
fout<<endl; | |
} | |
template <typename T> | |
int ListAll<T>::list(int m){ | |
int lastOne, lastZero, lastZeroBF; | |
int ones, counter = 0; | |
this->m = m; | |
fout.open("out.txt"); | |
bits = new char[n / sizeof(char) +1]; | |
for(int i = 0; i < m; i++) setone(bits,i); | |
for(int i = m; i < n; i++) setzero(bits,i); | |
lastOne = 0; | |
lastZero = m; | |
ones = 0; | |
while(true){ | |
print_one(); | |
counter ++; | |
if(counter % 100 == 0) | |
cout<<"已输出"<<counter<<endl; | |
//找最右边的0(右边至少1个1) | |
ones = 0; | |
//if(lastZero > lastOne) | |
for(lastZeroBF = lastOne; lastZeroBF < n && getbit(bits,lastZeroBF) != 0; lastZeroBF ++, ones ++); | |
if( n <= lastZeroBF) break; | |
setone(bits, lastZeroBF); | |
ones --; | |
for(int i = 0; i < ones; i++) setone(bits, i); | |
for(int i = ones; i < lastZeroBF; i++) setzero(bits,i); | |
if(ones > 0){ | |
lastOne = 0; | |
lastZero = ones; | |
}else{ | |
lastOne = lastZeroBF; | |
lastZero = 0; | |
} | |
} | |
// fout.flush(); | |
fout.close(); | |
delete[] bits; | |
bits =0; | |
return counter; | |
} | |
int main(void) | |
{ | |
char v[] = "abcdefghijklmnopqrstuvwxyz1234567890"; | |
int m,n; | |
int c; | |
ListAll<char> lall; | |
while(true){ | |
do{ | |
cout<<"请输入n"<<endl; | |
cin>>n; | |
}while(n < 1 || 36 <n); | |
do{ | |
cout<<"请输入m"<<endl; | |
cin>>m; | |
}while(m < 1 || m >n); | |
lall.set_vec(v,n); | |
c = lall.list(m); | |
cout << "total size " << c<<endl; | |
system("pause"); | |
cout<<"输入1继续,其它退出"<<endl; | |
cin>>c; | |
if(c != 1) | |
break; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment