代码回收站

程设实习 课程作业 MOOC POJ 魔兽世界之一:备战

#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)



转载于:https://my.oschina.net/seandor/blog/208599

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VERSION 5.00 Begin VB.Form Form1 Caption = "清空回收站" ClientHeight = 2595 ClientLeft = 60 ClientTop = 345 ClientWidth = 4680 Icon = "Form1.frx":0000 LinkTopic = "Form1" ScaleHeight = 2595 ScaleWidth = 4680 StartUpPosition = 1 '所有者中心 Begin VB.CommandButton Command1 Caption = "清空回收站" Height = 810 Left = 1020 Style = 1 'Graphical TabIndex = 1 Top = 840 Width = 1200 End Begin VB.CommandButton Command2 Caption = "退出" Height = 810 Left = 2385 TabIndex = 0 Top = 840 Width = 1200 End End Attribute VB_Name = "Form1" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False '函数声明 Private Declare Function SHEmptyRecycleBin Lib "shell32.dll" Alias "SHEmptyRecycleBinA" _ (ByVal hwnd As Long, ByVal pszRootPath As String, ByVal dwFlags As Long) As Long Private Declare Function SHUpdateRecycleBinIcon Lib "shell32.dll" () As Long Private Const SHERB_NOCONFIRMATION = &H1; Private Const SHERB_NOPROGRESSUI = &H2; Private Const SHERB_NOSOUND = &H4; Private Sub Form_Load() '为按钮加载图标 Command1.Picture = LoadPicture(App.Path & "\full.ico") End Sub Private Sub Command1_Click() Dim myval As Long ' 清空回收站 myval = SHEmptyRecycleBin(Form1.hwnd, "", SHERB_NOPROGRESSUI) If myval <> 0 Then myval = SHUpdateRecycleBinIcon() End If '为按钮加载图标 Command1.Picture = LoadPicture(App.Path & "\empty.ico") MsgBox "回收站已被清空!", vbInformation, "【提示信息】" End Sub Private Sub Command2_Click() End End Sub

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值