閱讀183 返回首頁    go 阿裏雲 go 技術社區[雲棲]


兩個數的乘積結果全為1和0組成的數

/************************************************************************/
/*題目:任意給定一個自然數N,尋找一個M,要求M是N的倍數,且它的所有各位數字
/*      都是1或者0組成,並要求M盡可能小。
/*例:N = 3 -->M = 3 * 37 = 111    N = 31 -->M = 31 * 3581 = 111011                                                                      
/************************************************************************/
#include <iostream>

using namespace std;

int main()
{
	int N;
	long M;
	long t;
    bool bFound = false;

	cout << "Please specilize a value to N: ";
	cin >> N;

	for (M = N * 2; !bFound; M++)
	{
		t = M;
		if (M % N == 0)
		{
			while (t)//逐位數字比較
			{
				if (t % 10 != 1 && t % 10 != 0)
					break;
				t /= 10;
				if (t == 1)
				{
					bFound = true;
					cout << "M = " << N << " * " << M / N << " = " << M << endl;
				}
			}
		}
	}
	system("pause");
	return 0;
}

最後更新:2017-04-02 15:15:29

  上一篇:go 揭秘APP刷榜黑幕 暴利催生的產業鏈
  下一篇:go 基礎練習