?? main.cpp
字號:
# include <iostream.h>
// a la russe(俄羅斯式)乘法算法:
// 把乘數和被乘數并排寫在一起,每個操作數一列。
// 將左邊操作數整除2,在該操作數下面寫下商;
// 將右邊操作數乘以2,在該操作數的下面寫下積。
// 以上一次的商和積作為操作數重復以上規則,直到左邊操作數為1為止。
// 接著把左列中商為偶數的行全部刪除,最后把右列中剩下的數字加起來就得到結果。
// 優點:不用保存乘法表,只需知道如何進行加法、如何把一個數整除以2。
//981 1234 1234
//490 2468
//245 4936 4936
//122 9872 9872
// 61 19744 19744
// 30 39488
// 15 78976 78976
// 7 157952 157952
// 3 315904 315904
// 1 631808 631808
// _______
// 1210554
// a la russe乘法
int russe(int m, int n)
{
int result = 0;
while (m > 0) //until m = 1
{
if (m%2 == 1) //m is odd
{
result += n;
}
m /= 2;
n = n + n;
}
return result;
}
int main()
{
int m, n;
cin >> m >> n;
cout << m << "*" << n << "=" << m*n << endl;
int product = russe(m, n);
cout << "product = " << product << endl;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -