【发布时间】:2015-11-28 11:41:13
【问题描述】:
假设给定一个合数 (n>3),可以写成:n = a*b,其中 a 和 b 是任意整数。
现在,我们的任务是计算 a 和 b 的值,以使函数 f(a,b) = |a-b| 最小化。
.
int n;
cin >> n; // Take it from the user
/* Now, find the value of the a and b */
int a = 1;
int b = n;
int temp_a;
int temp_b;
for(temp_a=1; temp_a<=sqrt(n); temp_a++) {
if(n % temp_a == 0) {
temp_b = n / temp_a;
if((temp_b - temp_a) < (b - a)) {
b = temp_b;
a = temp_a;
}
}
}
print a and b
【问题讨论】:
标签: algorithm optimization numbers