我来搞事情了ヘ(´ー`ヘ)
这道题是一道简单题,但你可以像我一样自由地发挥
以下提供三种趣味十足(画蛇添足)的做法
①DP解法
是的,你没看错,这可能是一道DP题目!
先推出所有加法计算的和,然后就可以O(1)时间输出!!!幸好a和b都<=1000等等,貌似不这样做也是O(1)呀……
#include <bits/stdc++.h>
using namespace std;
int s[1001][1001];
int main()
{
for (int i=0; i<=1000; i++)
for (int j=0; j<=1000; j++)
{
if (i==0) s[i][j]=j;
else if (j==0) s[i][j]=i;
else s[i][j]=s[i-1][j-1]+2;
}
int a, b;
cin >> a >> b;
cout << s[a][b] << endl;
return 0;
}②深搜
这道题好像深搜反而比DP快诶……
当然也会有一点点的分治思想(就一点点,不能再多了!)学会了再慢慢理解吧
#include <bits/stdc++.h>
using namespace std;
int sum(int a, int b)
{
if (a==0&&b==1) return 1;
if (a==1&&b==0) return 1;
if (a%2==0&&b%2==0) return sum(a/2, a/2)+sum(b/2, b/2);
if (a%2==1&&b%2==0) return sum(a/2, a/2+1)+sum(b/2, b/2);
if (a%2==0&&b%2==1) return sum(a/2, a/2)+sum(b/2, b/2+1);
if (a%2==1&&b%2==1) return sum(a/2, a/2+1)+sum(b/2, b/2+1);
}
int main()
{
int x, y;
cin >> x >> y;
cout << sum(x, y) << endl;
return 0;
}③数论可能能和数论有些关系吧
根据平方和公式,(a+b)²=a²+2ab+b²。
所以,只要求出a²+2ab+b²,再开根号,就能得到a+b了哦
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
int c=a*a+b*b+2*a*b;
cout << sqrt(c) << endl;
return 0;
}更多搞笑做法,持续更新中0=w=0
感谢读者们的大力支持~
加个关注再走吧QwQ

京公网安备 11010502036488号