当前位置:网站首页>HJ76 尼科彻斯定理
HJ76 尼科彻斯定理
2022-07-20 13:02:00 【fareast_mzh】
描述
验证尼科彻斯定理,即:任何一个整数m的立方都可以写成m个连续奇数之和。
例如:
1^3=1
2^3=3+5
3^3=7+9+11
4^3=13+15+17+19
输入一个正整数m(m≤100),将m的立方写成m个连续奇数之和的形式输出。
数据范围:1\le m\le 100\1≤m≤100
进阶:时间复杂度:O(m)\O(m) ,空间复杂度:O(1)\O(1)
输入描述:
输入一个int整数
输出描述:
输出分解后的string
示例1
输入:
6
复制输出:
31+33+35+37+39+41
#include <iostream>
#include <vector>
using namespace std;
class HJ76 {
private:
int oddsPrev(int m) {
return m * (m-1) /2; // 前面的奇数个数,倒序相加
}
public:
vector<int> solution(int m) {
int odd = 2 * oddsPrev(m) + 1;
vector<int> vi;
while (m > 0) {
vi.push_back(odd);
odd += 2;
m -= 1;
}
return vi;
}
};
int main(){
int m = 0;
std::cin >> m;
HJ76 q;
vector<int> vi = q.solution(m);
vector<int>::const_iterator it = vi.begin();
cout << *it;
while (++it != vi.end()) {
cout << "+" << *it;
}
return 0;
}
int
m;
while
(cin>>m){
int
s = m*m+1-m;
cout<<s;
for
(
int
i=1;i<m; i++)
cout<<
'+'
<<s+2*i;
cout<<endl;
}
边栏推荐
- 【28. 最大异或对】
- Five coquettish projects were born!
- SQL blind note
- Limit the input type and length in the input box
- How many months did you write your first SCI?
- Google Earth Engine——1980-至今全球压力、温度、风等数据集
- 多省份用电负荷创历史新高 力保迎峰度夏能源供应安全
- Chapter3 : Fighting COVID-19 with Artificial Intelligence
- 4. Storage NFS
- 【27. 表达式求值(中缀表达式)】
猜你喜欢
CANoe下载地址以及CAN Demo 16的下载与激活,并附录所有CANoe软件版本
Recommend 5 powerful tools for data visualization
From giving up to mastering, these 13 recommended system papers must be read! [attached data]
程序员看的JPEG图像压缩介绍(多图慎入)
【28. 最大异或对】
When will the moon appear
【FreeSwitch开发实践】C语言中使用ESL连接FreeSwitch
Four redis cluster schemes you must know and their advantages and disadvantages
CANoe仿真功能之自动化序列(Automation Sequences )
Canoe cannot automatically identify serial port number? Then encapsulate a DLL so that it must work
随机推荐
Google 為造芯再掀“搶人大戰”,英特爾 17 年老將加入
About MySQL Boolean and tinyint (1)
双快门技术
Four redis cluster schemes you must know and their advantages and disadvantages
CANoe的数据回放(Replay Block),还是要结合CAPL脚本才能说的明白
When will the moon appear
物联网标准体系框架
Examples illustrate the division basis of code segment (.Text), data segment (.Data), BSS segment, read-only data segment (.Rodata) and stack
阿里面试Redis常考问题,你略知多少?
除了定时器,真的没法在Simulation Node 类型的CAPL节点中实现延时了吗?
Download address of canoe, download and activation of can demo 16, and appendix of all canoe software versions
【26. 字符串哈希】
Installation and deployment of redis on centos7 [easy to understand]
How to query and modify parameter status values
面试题:八皇后问题(N皇后问题)「建议收藏」
关于MySQL的boolean和tinyint(1)
4. Storage NFS
Web漏洞安全-失效访问权限控制
If you can't understand the character code, hit me! (ASCII,Unicode,Utf-8,GB2312…)
Hand rolling third person character controller - unity makes soul game notes 01