Output對於每組例子,輸出連續的幾個女朋友手中的玫瑰花總和爲M的所有可能,以起始女朋友編號從小到大排序。
Sample Input
20 10 50 30 0 0Sample Output
[1,4] [10,10] [4,8] [6,9] [9,11] [30,30]
首先,判斷題目,如果爲數學問題,觀察能否對公式進行化簡,用一個量便是各個量的關係,化簡後進行循環。
#include<math.h>
#include<stdio.h>
#include<string.h>
int main()
{
int n,m,i,j;
while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0) break;
else
{
for(j=(int)sqrt(2.0*m);j>0;j--)
{
i=(2*m/j+1-j)/2;
if(((i+i+j-1)*j/2)==m) printf("[%d,%d]\n",i,i+j-1);
}
}
printf("\n");
}
return 0;
}