沐里纷纷


私信TA

用户名:Epoch

访问量:62777

签 名:

我不会算法

等  级
排  名 37
经  验 12814
参赛次数 1
文章发表 172
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

不会算法

解题思路:

使用等差数列求和公式足,使复杂度为O(n),以承受10^4的数据的打击。

注意事项:

参考代码:

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#define EPS 0.000001

using namespace std;

map<int, int> result;

void solve (int n) 
{
 double a = 1;
 while (a <= n/2)
 {
  double b = (sqrt(1+4*(a*a-a+2*n))-1)/2;
  if (b - (int)b < EPS)
   result[(int)a] = (int)b;
  a += 1;
 }
}

int main(void)
{
 int n = 0;
 cin >> n;
 solve(n);
 for (map<int, int>::iterator it = result.begin(); it != result.end(); it++)
  cout << it->first << " " << it->second << endl;
 return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区