加法变乘法
我们都知道:1 + 2 + 3 + . . . + 49 = 1225
现在要求你把其中两个 不相邻的加号变成乘号,使得结果为2015
比如:
1 + 2 + 3 + . . . + 10 ∗ 11 + 12 + . . . + 27 ∗ 28 + 29 + . . . + 49 = 2015
就是符合要求的答案。
请你寻找另外一个可能的答案,并把位置靠前的那个乘号左边的数字提交(对于示例,就是提交10 1010)。
注意:需要你提交的是一个整数,不要填写任何多余的内容。
答案:16
题目解析
方法一
利用加法变乘法之后的差值得到结果,有意思哈
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
for(int i = 1; i <= 46; i++){ //第一个乘法的左边数字
for(int j = i+2; j <= 48; j++){ //第二个乘法的左边数字
if(i*(i+1)-(i+i+1) + j*(j+1)-(j+j+1) == 2015-1225){
cout << i << endl;
}
}
}
return 0;
}
方法二
#include <iostream>
using namespace std;
void solve(){
int first, second;
for(first = 1; first <= 48; first++){
if(first == 10){
continue;
}
for(second = first + 2; second <= 48; second++){
int res = 0, i = 1;
while(i <= 49){
if((i == first) || i == second){
res += i * (i+1);
i += 2;
}else{
res += i;
i++;
}
}
if(res == 2015){
cout << first;
}
}
}
}
int main(int argc, char** argv) {
solve();
return 0;
}