题目连接

  • 该题是luogu试炼场的2-11:T3

题目大意

  1. 给出n,求1-n的数字中,那些段的连续和 是 n

题目分析


解题思路:队列的介绍

  1. 一般指连续的数字:小火车
  2. 这里车头是y,车尾是x,车头一直往前开,如果s>n,则车尾缩短:

luogu1147:连续自然数和:队列的使用

代码1:

//luogu1147:连续自然数和 

//连续:队列的使用 

#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n,m; 
	scanf("%d",&n); m=n/2+1;
	
	int x=1,y,s=1;// [x,y] 的和是 s 
	
	for(y=2;y<=m+1;y++)
	{
		s+=y;//进队 
		while( s>n && x<y )//出队 
		{
			s-=x;x++; 
		}
		if(s==n) printf("%d %d\n",x,y);
	}
	
	return 0;
}

相关文章:

  • 2022-02-22
  • 2021-10-05
  • 2021-05-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-19
  • 2022-12-23
猜你喜欢
  • 2022-01-01
  • 2021-11-18
  • 2022-01-04
  • 2022-03-10
  • 2021-06-16
  • 2022-12-23
  • 2021-08-03
相关资源
相似解决方案