25

闲来没事,睡觉前写了这个程序。

#include <cctype>
#include <string>
#include <iostream>
 
using namespace std;
 
void ToOtherCase(char &c) 
{
    if( isupper(c) )
        c = tolower(c); 
    else
        c = toupper(c);
}
 
void AllCase(string &sInput, int iIndex)
{
    if( iIndex == sInput.size() )   
    {   
        cout << sInput << endl;
        return;
    }   
 
    AllCase(sInput, iIndex + 1); 
 
    if( !isalpha(sInput[iIndex]) )
        return;
 
    ToOtherCase(sInput[iIndex]);
    AllCase(sInput, iIndex + 1); 
}
 
int main(int c, char **v)
{
    if( c != 2 ) 
    {   
        cout << "Usage: " << v[0] << " word" << endl;
        return 0;
    }   
    string sInput = v[1];
    AllCase(sInput, 0);
    return 0;
}

ps, 为什么我的网站这么慢?

Tagged with:
19
Tagged with:
18

当可能出现几个进程争用(读、写)同一个Critical Section的时候,加锁是常用的做法。
Linux加锁的方法,除了经典的IPC(Semophore)之外,记录锁(Record Locking)提供了更简单的方法。

其实记录锁的名字叫文件锁会比较贴切一点,因为其加锁和解锁都是通过对文件的操作完成的。
文件锁的粒度大可到整个文件,小可到一个字节,长度可变,但都可以说是对应一个Record(逻辑意义上)。

对锁的控制是通过调用fcntl实现的,基本的方式如下:

fcntl(fd, operation, flock);

fd是某个文件的句柄,该fd需要以与type相匹配的方式open
operation是操作类型

  • F_GETLK 读取锁信息
  • F_SETLK 设置锁,在锁已经被占用的情况下,马上返回错误,有点类似于pthread的trylock
  • F_SETLKW 设置锁,如果锁被其他进程占用,则阻塞

flock是个struct,用来传递锁的详细信息

  • short int l_type 锁的类型,可以是F_RDLCK、 F_WRLCK、F_UNLCK,分别对应读锁、写锁和解锁
  • short int l_whence 与l_start一起决定锁的起始位置,SEEK_SET、SEEK_CUR、SEEK_END分别对应文件的开始、当前位置和末尾,和fseek、lseek里的含义一致
  • off_t l_start 起始位置
  • off_t l_len 长度,0表示从l_start到文件的末尾。据说某些实现支持负数
  • pid_t l_pid 拥有锁的进程,operation为GETLK的时候会被设置

fd所对应的文件,本身不需要有数据。

由operation的取值,flock的定义可以看出,其实记录锁非常灵活。
它既可以实现排他锁(F_WRLCK),也可以实现共享锁(F_RDLCK);
同时也支持同步锁(F_SETLKW)和异步锁(F_SETLK)。

记录锁的另一个好处时,进程退出时,会自动释放掉自己所占用的锁。
这就避免了进程异常退出时资源无法回收的问题。

速度也是需要考虑的因素,根据测试,记录锁相对最慢;
但综合考虑易用性和灵活性,我认为这样的速度损耗是可以接受的。

flock(2)是另一种实现文件锁的方法,详情可以man 2 flock。

Tagged with:
11

保存下来,以备不时之需

# /dev/sda8
UUID=2cf88d46-8b6e-4eb3-bc2f-5f0680983299 /               ext3    relatime,errors=remount-ro 0       1
# /dev/sda11
UUID=5822ad0d-4fc6-428f-9334-0a5d022a808d /usr            ext3    relatime,errors=remount-ro 0       1
# /dev/sda7
UUID=1c2431ce-68cf-4c10-89c9-f86a52389c9a /data            ext3    relatime,errors=remount-ro 0       1
# /dev/sda9
UUID=5c73fb68-6e3e-49e6-b7a8-0f21e9479aaa /home           ext3    relatime        0       2
# /dev/sda10
UUID=99e2d59c-28e5-47fb-a3c7-3ea3ca10aebb none            swap    sw              0       0
Tagged with:
11

有没有试过在Firefox中用j,k滚屏的感觉?
如果你也同时是Firefox和Vim的用户,
强烈推荐。
可以在80%的情况下摆脱鼠标。

简单记录一下我现在用到的:
j,k : 不说了
C-i : History Next
C-o : History Back
gt : 下一个Tab
gT : 上一个Tab
gg : Home
G : End
:t URL: 新标签打开URL
:o URL: 同一窗口打开URL
r : 重新载入
f/F : 打开某个链接(最特别的功能
d : 关闭当前Tab
ZZ : 退出Firefox

Tagged with:
07

升级到9.04之后,面板上的DropBox图标点击没有了反应。
直觉上是DropBox不支持Jaunty,
到DropBox的网站,没有发现支持Jaunty的版本。

遇到解决不了问题的时候,有两件事我的确定的:
1. 我不会是世界上第一个遇到这样问题的人;
2. 问题的答案通过Google可以找到。

这次也不例外,下面就是结果:)

cd ~
wget http://dl.getdropbox.com/u/17/dropbox-lnx.x86-0.6.491.tar.gz
rm -r .dropbox-dist/
tar xzf dropbox-lnx.x86-0.6.491.tar.gz
killall nautilus

这是来源

这只是临时解决方法,相信DropBox很快会有新版本发布。

Tagged with:
06

不是官方网站上声称的4.23,而是今天:)


升级中……

Tagged with:
05

终于查到macbook完整的升级历史
在五月中的时候应该会有一次习惯性的升级,
到时就是出手的时候了:)

btw,新蛋可以分期。

Tagged with:
04

来自stephen的推荐,花了一个小时细看,不甚了了。
scalable-network-programming
作者网站

Tagged with:
03

还是把伤脑筋的事情留给CPU吧

#include <iostream>
#include <cstdlib>
#include <cstdio>
 
using namespace std;
 
class CShuDu
{
	public:
		CShuDu()
		{
			_iLeft = 0;
			_GuestCount = 0;
			_Depth = 0;
			_CurrentX = _CurrentX = 0;
		};
		~CShuDu(){};
		void GetInput()
		{
			for( int i = 0; i < 9; i++ )
			{
				for( int j = 0; j < 9; j++ )
				{
					int n;
					cin >> n;
					switch(n)
					{
						case 1:
						case 2:
						case 3:
						case 4:
						case 5:
						case 6:
						case 7:
						case 8:
						case 9:
							_Board[i][j].iType 	= FIXED;
							_Board[i][j].iValue	= n;
							break;
						case 0:
							_Board[i][j].iType 	= BLANK;
							_Board[i][j].iValue	= n;
							_iLeft++;
							break;
						default:
							cout << "Input Error" << endl;	
							exit(-1);
					}
				}
			}
		}
 
		void OutputBoard(int x, int y)
		{
			cout << _GuestCount << "\t" << _Depth << "\t" << _iLeft << "(" << x << "," << y << ")" << endl;
			cout << "\t\t  ";
			for( unsigned int i = 0; i < 9; i++ )
				if( i == y )
					cout << " " << "x  ";
				else
					cout << " " << i << "  ";
 
			cout << endl;
 
			for(unsigned int i = 0; i < 9; i++)
			{
				if( i == x )
					cout << "\t\t" << "x ";
				else
					cout << "\t\t" << i << " ";
				for( unsigned int j = 0; j < 9; j++ )
				{
					if( _Board[i][j].iType == FIXED )
						cout << " " << _Board[i][j].iValue << "  ";
					else
						cout << "(" << _Board[i][j].iValue << ") ";
				}
				cout << endl;
			}
		}
 
		void Guest(int x, int y)
		{
			_Depth++;
			_CurrentX = x;
			_CurrentY = y;
			_GuestCount++;
 
			if( _iLeft == 0 )	
			{
				OutputBoard(-1, -1);
				return;
			}
 
			for( unsigned int i = 1; i <= 9; i++ )
			{
				_Board[x][y].iValue = i;	
				if( IsBoardValid(x, y) )
				{
					int _x = x;
					int _y = y;
					GetNextPosition(_x, _y);
					_iLeft--;
					Guest(_x, _y);
					_iLeft++;
				}
			}
			_Board[x][y].iValue = 0;	
			_Depth--;
		}
 
		void Run()
		{
			int x, y;
			x = y = -1;
			GetNextPosition(x, y);
			Guest(x, y);
		}
 
	private:
		int _iLeft;
		int _GuestCount;
		int _Depth;
		int _CurrentX;
		int _CurrentY;
 
		enum NodeType{ FIXED, BLANK };
		struct Node{
			int iValue;
			int iType;
			bool operator==(const Node &r)
			{
				return iValue == r.iValue;
			}
		};
 
		Node _Board[9][9];
 
		bool IsBoardValid(int x, int y)
		{
			int X = x / 3;
			int Y = y / 3;
			for( unsigned int i = 0; i < 9; i++ )
			{
				if( i == x )
					continue;
				if( _Board[i][y] == _Board[x][y] )
					return false;
			}
 
			for( unsigned int i = 0; i < 9; i++ )
			{
				if( i == y )
					continue;
				if( _Board[x][i] == _Board[x][y] )
					return false;
			}
 
			for( unsigned i = X * 3; i < (X+1)*3; i++ )
			{
				for( unsigned j = Y * 3; j < (Y+1)*3; j++ )
				{
					if( i == x && j == y )
						continue;
					if( _Board[x][y] == _Board[i][j] )
						return false;
				}
			}
		//	OutputBoard(x, y);
			return true;
		}
 
		void GetNextPosition(int &x, int &y)
		{
			if( x == -1 )
				x = 0;
			if( y == -1 )
				y = 0;
			else
			{
				if( y == 8 )
				{
					x++;
					y = 0;
				}
				else
				{
					y++;
				}
			}
 
			while( _Board[x][y].iType == FIXED )
			{
				if( y == 8 )
				{
					x++;
					y = 0;
				}
				else
				{
					y++;
				}
				CheckPosition(x, y);
			}
		}
 
		void CheckPosition(int x, int y)
		{
			if( x >= 9 || y >= 9 )
			{
				cerr << "Position Error" << endl;
				exit(-3);
			}
		}
};
 
int main()
{
	CShuDu cShudu;
	cShudu.GetInput();
	cShudu.Run();
	return 0;
}
0 0 7 2 8 0 3 0 0
2 3 0 0 0 7 1 0 0
0 4 0 0 0 6 7 2 0
9 0 0 0 7 0 0 1 0
4 0 1 0 0 5 6 0 8
0 7 0 8 2 0 0 0 4
0 8 6 4 0 0 0 5 0
0 0 2 9 0 0 0 6 7
0 0 4 0 6 2 8 0 0
161	46	0(-1,-1)
		   0   1   2   3   4   5   6   7   8
		0 (1) (6)  7   2   8  (9)  3  (4) (5)
		1  2   3  (9) (5) (4)  7   1  (8) (6)
		2 (8)  4  (5) (1) (3)  6   7   2  (9)
		3  9  (5) (8) (6)  7  (4) (2)  1  (3)
		4  4  (2)  1  (3) (9)  5   6  (7)  8
		5 (6)  7  (3)  8   2  (1) (5) (9)  4
		6 (7)  8   6   4  (1) (3) (9)  5  (2)
		7 (3) (1)  2   9  (5) (8) (4)  6   7
		8 (5) (9)  4  (7)  6   2   8  (3) (1)
Tagged with:
preload preload preload

无觅相关文章插件,快速提升流量