Y - The sum problem

Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M.
Input
Input contains multiple test cases. each case contains two integers N, M( 1 <= N, M <= 1000000000).input ends with N = M = 0. 
Output
For each test case, print all the possible sub-sequence that its sum is M.The format is show in the sample below.print a blank line after each test case. 
Sample Input
20 10
50 30
0 0
Sample Output
[1,4]
[10,10]

[4,8]
[6,9]
[9,11]

[30,30]

题解:题目要求在1至m的所有数中,找到所有的连续的子序列,其和都为n;

一开始的想法是从每个点开始遍历查找以这个点为左端点的满足要求的序列是否存在

	int i,j,sum = 0,k;
/*	for(i = 1; i <= m; i++)//从1开始查找子序列 
	{
		sum = 0;
		for(j = i; j <= n; j++)//查找以i为开始的子序列是否满足条件 
		{
			sum = sum + j;
			if(sum == n)
			{	
				printf("[%d,%d]\n",i,j);
			
			}
			if(sum > n)break;
		}	
		
	}
	*/
结果代码超时,这时便需要换个思路,不是从左端点入手,而是从子序列长度入手,这样可以大大降低消耗时间,如果左端点为i,长度为k,那么公式为:((2*i)+k-1)*k = 2*n;

对k进行初始化的时候,是把i设初值为1,因为这样序列长度才会最长,当然不一定存在这样的序列,这就需要验证,验证的方法是通过整数除法所得到的值会舍去小数部分,这样无法整除的数便会被筛选出来,

代码如下:

#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int xulie(int m,int n)
{
	int i,j,sum = 0,k;

	k = sqrt(n * 2);
	k++;
	
	while(--k)
	{
		i = n/k - (k - 1)/2;
		if((2 * i + k - 1 ) * k == 2 * n)
		{
			printf("[%d,%d]\n",i,i + k - 1);
		}
	 } 
	return 0;
}

int main()  
{  
    int m,n;
	scanf("%d%d",&m,&n);
	while(m != 0||n != 0) 
	{
		xulie(m,n);
		printf("\n");
		scanf("%d%d",&m,&n);
	 } 
 }   
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值