コード回収局
5715 ワード
程設実习课程工作MOOC POJ魔獣世界の一つ
N皇后問題:
wx.grid更新方法:
Collatz sequence
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class CSamurai {
private:
string type;
int id;
int strength;
int atk;
public:
CSamurai(string name=""): type(name){};
void set_strength(int strength_num) {strength = strength_num;}
void set_id(int id) {this->id = id;}
const int get_strength() {return strength;}
const string get_type() {return type;}
};
class CHeadquater {
private:
string name; //name of the headquater
int * samurai_spawn_order;
int step; //point to which samurai it will produce
int total_strength;
int samurai_num[5]; //store the numbers of each samurai
int serial_num;
bool productable; //enough or not enough strength to produce samurai
static int time;
static CSamurai samurais[5];
public:
CHeadquater(int *order, int strength, int step=0, const string camp_name="");
void SamuraiSpawner();
static void SetOriginStrength();
static void StartPrepareWar(CHeadquater &, CHeadquater &);
};
CHeadquater::CHeadquater(int *order, int strength, int step, const string camp_name): name(camp_name) {
samurai_spawn_order = order;
total_strength = strength;
this->step = step;
productable = true;
serial_num = 0;
for (int i = 0; i < 5; i++)
samurai_num[i] = 0;
}
void CHeadquater::SamuraiSpawner() {
const int primitive_step = step;
bool first = true; //ignore the first time of the loop
while (first | primitive_step != step) {
int strength_needed = samurais[samurai_spawn_order[step]].get_strength();
string current_type = samurais[samurai_spawn_order[step]].get_type();
if (total_strength >= strength_needed) {
serial_num++;
samurai_num[step]++;
cout << setw(3) << setfill('0') << time << " " << name << " " << current_type
<< " " << serial_num << " " << "born with strength " << strength_needed
<< "," << samurai_num[step] << " " << current_type << " "
<< "in " << name << " headquarter" << endl;
total_strength -= strength_needed;
return;
}
step = (step + 1) % 5;
first = false;
}
//the headquater is obviously not productable.
cout << setw(3) << setfill('0') << time << " " << name << " headquarter stops making warriors" << endl;
productable = false;
}
void CHeadquater::SetOriginStrength() {
int strength = 0;
for (int i = 0; i < 5; i++) {
cin >> strength;
samurais[i].set_strength(strength);
}
time = 0;
}
void CHeadquater::StartPrepareWar(CHeadquater &a, CHeadquater &b) {
while (true) {
if (a.productable) {
a.SamuraiSpawner();
a.step = (a.step + 1) % 5;
}
if (b.productable) {
b.SamuraiSpawner();
b.step = (b.step + 1) % 5;
}
time++;
if (a.productable == false && b.productable == false)
break;
}
}
int CHeadquater::time = 0;
CSamurai CHeadquater::samurais[5] = {CSamurai("dragon"), CSamurai("ninja"), CSamurai("iceman"),
CSamurai("lion"), CSamurai("wolf")};
int main() {
int test_case = 0;
cin >> test_case;
for (int i = 1; i <= test_case; i++) {
cout << "Case:" << i << endl;
int strength = 0;
cin >> strength;
int red_order[5] = {2, 3, 4, 1, 0};
int blue_order[5] = {3, 0, 1, 2, 4};
CHeadquater red(red_order, strength, 0, "red");
CHeadquater blue(blue_order, strength, 0, "blue");
CHeadquater::SetOriginStrength();
CHeadquater::StartPrepareWar(red, blue);
}
return 0;
}
N皇后問題:
#!/usr/bin/env python
def nqueens(n):
''' (int) -> list of list
Return all the resolusions of the nqueues problem
'''
k = 1 #the current row
x = [0] * (n+1)
x[k] = 0 #x[k] stands for the current column
while k > 0:
x[k] = x[k] + 1
while x[k] <= n and not place(k, x):
x[k] = x[k] + 1
if x[k] <= n:
if k == n:
print x[1:]
else:
k = k + 1
x[k] = 0
else:
k = k - 1
def place(k, x):
''' (int, list) -> bool
'''
i = 1
while i < k:
if x[i] == x[k] or abs(x[i] - x[k]) == abs(i - k):
return False
i = i + 1
wx.grid更新方法:
def updateData(self, flag):
result = self.lmsDatabase.execute('SELECT * FROM BOOKINFO ORDER BY BOOKID ASC')
bookData = []
for eachTuple in result:
bookData.append(list(eachTuple))
self.bookEntry.data = bookData
if flag:
if flag == 'Add':
msg = wx.grid.GridTableMessage(self.tableBase,
wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED, 1)
elif flag == 'Delete':
msg = wx.grid.GridTableMessage(self.tableBase,
wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED,
self.tableBase.GetNumberRows(), 1)
self.tableBase.GetView().ProcessTableMessage(msg)
self.ForceRefresh()
Collatz sequence
def collatz(n):
i = n
while i > 1:
print i
if i % 2:
i = i * 3 + 1
else:
i /= 2
collatz(1000)