博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2140 Herd Sums
阅读量:2441 次
发布时间:2019-05-10

本文共 1443 字,大约阅读时间需要 4 分钟。

Herd Sums
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16120   Accepted: 9541

Description

The cows in farmer John's herd are numbered and branded with consecutive integers from 1 to N (1 <= N <= 10,000,000). When the cows come to the barn for milking, they always come in sequential order from 1 to N. 
Farmer John, who majored in mathematics in college and loves numbers, often looks for patterns. He has noticed that when he has exactly 15 cows in his herd, there are precisely four ways that the numbers on any set of one or more consecutive cows can add up to 15 (the same as the total number of cows). They are: 15, 7+8, 4+5+6, and 1+2+3+4+5. 
When the number of cows in the herd is 10, the number of ways he can sum consecutive cows and get 10 drops to 2: namely 1+2+3+4 and 10. 
Write a program that will compute the number of ways farmer John can sum the numbers on consecutive cows to equal N. Do not use precomputation to solve this problem. 

Input

* Line 1: A single integer: N 

Output

* Line 1: A single integer that is the number of ways consecutive cow brands can sum to N. 

Sample Input

15

Sample Output

4

题意:求一个数n可以有几种有连续的数字相加得到

解题思路:i*a+i*(i-1)/2=n,转换为a=(n-i*(i-1)/2)/i   把i从1到n遍历一遍,如果(n-i*(i-1)/2)可以整除i那么i符合

#include 
using namespace std;int main(){ int n; while (cin>>n){ int count=0; for (int i=1;i<=n;i++){ int ans=n-i*(i-1)/2; if (ans<=0) continue; if (ans%i==0){ count++; } } cout<
<

转载地址:http://eybqb.baihongyu.com/

你可能感兴趣的文章
如何在VS Code中删除空行
查看>>
JavaScript中的链接方法调用
查看>>
postgresql使用_如何使用Sequelize与PostgreSQL交互
查看>>
盖茨比乔布斯_盖茨比,如何更改图标
查看>>
npm本地包安装_如何在本地测试NPM软件包
查看>>
如何处理承诺拒绝
查看>>
dom5秒计时截断_使用DOM时计时的重要性
查看>>
安装后使用Docker的第一步
查看>>
c结构体定义_C结构
查看>>
javascript 逗号_JavaScript中逗号的奇怪用法
查看>>
软件开发 自由职业_如何以开发人员身份开始自由职业
查看>>
node js 图像压缩_如何使用Node.js下载图像
查看>>
文件命名为node.js_如何在Node.js中批量重命名文件
查看>>
如何反转JavaScript数组
查看>>
如何安装svelte_如何在Svelte中动态应用CSS
查看>>
c#枚举类型_C枚举类型
查看>>
如何在数据库中存储用户密码_如何在数据库中存储密码
查看>>
gatsby_使用Gatsby加载外部JS文件
查看>>
窗口怎么退出扩展窗口_如何创建退出意图弹出窗口
查看>>
docker删除映像_如何将更改提交到Docker映像
查看>>