走方格的方案数
(n为横向的格子数,m为竖向的格子数)从棋盘左上角出发沿着边缘线从左上角走到右下角,总共有多少种走法,要求不能走回头路, 即:只能往右和往下走,不能往左和往上走。
代码实现
int Go(int x, int y )
{
if (x == 0 || y == 0)
{ //走到最右边或最下边,就只能直着走了
return 1;
}
else
{
return Go(x - 1, y) + Go(x, y - 1);
}
}
int main()
{
int a, b;
cin >> a >> b;
cout << Go(a, b) << endl;
}
另类加法
编写一个函数返回A+B的值,但不得使用+或其他算数运算符。
代码实现
class UnusualAdd
{
public:
int addAB(int A, int B)
{
int num;
while(B)
{
num=(unsigned)(A&B)<<1;
A^=B;
B=num;
}
return A;
}
};