程序员面试100题之二十六,和为n连续正数序列

// 100_26.cpp : Defines the entry point for the console application.

//



#include "stdafx.h"



void find(int n)

{

	int small = 1;

	int big = 2;

	int middle = (n+1)/2;

	int sum = small + big;



	while(small < middle)

	{

		if(sum == n)

			printf("%d %d\n", small,big);

		if(sum > n)

		{

			sum -= small;

			small++;

		}

		else

		{

			big++;

			sum += big;

		}

	}



}



int _tmain(int argc, _TCHAR* argv[])

{

	find(15);

	return 0;

}



你可能感兴趣的:(程序员)