Hash Algorithm

#include <cstring>
#include <iostream>

template <class T>
class HashTable {
#ifndef HASH_K
#define HASH_K 100007
#endif
private:
	struct {
		int key;
		T value;
		bool used;
	} hashTable[HASH_K];
public:
	HashTable() {
		memset(hashTable, 0, sizeof(hashTable));
	}
	
	T &operator [] (const int &k) {
		int h = k % HASH_K;
		
		for (int i = 0; i < HASH_K; i++) {
			int hh = (h + i) % HASH_K;
			if (hashTable[hh].key == k || hashTable[hh].used == false) {
				hashTable[hh].used = true;
				hashTable[hh].key = k;
				return hashTable[hh].value;
			}
		}
	}
};

HashTable<int> ht;

int main() {
	using namespace std;

	ht[1] = 2;
	ht[3] = 5;
	ht[100008] = 233;
	
	cout << ht[1] << " " << ht[2] << " " << ht[100008] << endl;
	return 0;
}

Some thoughts of the CrazyCoding Union

Union activities:

  1. “Reading Books”: Reading something fun… such as The World Is Flat. This made a difference to my opinion…
  2. “Hi, New Teacher”: In our union, almost every member has a “teacher”. In fact, this “teacher” is another union member who learns better than the “student”: By this way, we will be better, together.
  3. “Producing New Problems”:  Any of us can “produce” problems for “weekly tests”. On weekly tests, we can get something fun from the problems.
  4. “Weekly Tests”: Almost every week, we have to take some “small” tests. If we don’t pass the tests, we have to take them again… Until we AK them.
  5. “Solution Sharing”: After the tests, there are always somebody sharing their solutions. By doing this action, we can learn about the different solutions to each problem.
  6. So fun…

Above all, I think… the union will get better… (w)

[Solution]《进制转换》解题报告

题目很简单:把一个十进制数转换成十六进制。

亮点:使用常量字符数组来存储0 – 15的对应值0 – F

不多说,直接上代码。


#include <iostream>
#include <string>
using namespace std;

// 定义进制数
const int radix = 16;

// 定义 0-F 对应 0 - 15
const char digit[radix] = "0123456789ABCDEF";

// 定义无符号64位长整型变量 n
unsigned long long n;

// 定义字符串变量 ans,用于存储结果
string ans;

// 主过程
void f(unsigned long long x){
    
    // 将字符串 ans 后连接 x mod radix 的对应 0-F 字符
    ans += digit[x % radix];
    
    // 将 x 赋值为 x / radix (进制数(16))
    x /= radix;
    
    // 如果 x 已被除尽,则退出过程
    if (x == 0) 
        return;
    else
        // 递归调用本过程
        f(x);
}


int main(){
    // 读入n
    cin >> n;
    
    // 执行过程
    f(n);
    
    // 倒序输出字符串
    for (int i = ans.length() - 1; i >= 0; i-- )
        cout << ans[i];
    
    return 0;
}