@[yubing_lml](/space/show?uid=237242) ?
by 引领天下 @ 2019-08-25 16:05:41
所以呢?
by SSerxhs @ 2019-08-25 16:08:06
“能计算正确结果的错误答案”是什么意思
by encore @ 2019-08-25 16:17:32
@[encore](/space/show?uid=113385) 就是机上测是对的,而提交上去是错的
by litaoyang2021 @ 2019-08-25 16:31:06
不是,就是按我这个答案也能算出来结果,只不过顺序不一样。比如题目是2+2(0),而我的是2(0)+2
by yubing_lml @ 2019-08-25 16:57:49
换了一下顺序,AC啦!下面是代码~(大家如果不明白可以上机跑一下哦~嘿嘿)\
这道题我调了近2个小时,还是太渣—_—
```cpp
#include<iostream>
#include<string>
using namespace std;
string getbit(int n)
{
string str;
do
{
if (n & 1 == 1)
str += '1';
else
str += '0';
n = n >> 1;
} while (n != 0);
return str;
}
void deal(int n)
{
if (n == 0)
{
cout << n;
return;
}
if (n == 2)
{
cout << 2;
return;
}
string str = getbit(n);
int len = str.length();
int i = len - 1;
bool flag = false;
while (i>= 0)
{
if (str[i] & 1 == 1)
{
if(flag==true)
cout << '+';
flag = true;
if (i != 1)
{
cout << 2 << '(';
deal(i);
cout << ')';
}
else
cout << 2;
}
i--;
}//while
return;
}
int main()
{
int n;
cin >> n;
deal(n);
return 0;
}
```
by yubing_lml @ 2019-08-25 16:59:58
还没有看题解怎么做的,一会看看去!
by yubing_lml @ 2019-08-25 17:00:43
本来就是瞎写的,以为肯定会超时来着,没想到过了O(∩_∩)O哈哈~,用的递归,很好理解哟~
by yubing_lml @ 2019-08-25 17:05:12