C++面试题

来源:互联网 发布:淘宝直播怎么搜索找人 编辑:程序博客网 时间:2024/06/05 19:31

#include "stdafx.h"
#include "stdlib.h"
#include <math.h>

int main(int argc, char* argv[])
{
 int n,middle,i;
 n = atoi(argv[1]);

 if(n <2)
 {
   printf("please input the number larger than 2 ");
   return -1;
 }  

 middle = (int)sqrt(2*n); // the min sum of i numbers is (i+1)*i/2

 for(i=2; i<=middle; i++)
 {      
  if( i % 2 ==1)
  {   
   if( n % i ==0) //  15 % 3
   {       
    int min, max,middle;
    middle=n/i;
    min=middle - i/2;
    max=middle + i/2;
    printf("%d could added by %d numbers for %d to %d /n",n,i,min,max);

   }       
  }else // i is even
  {   
   if( (n-i/2) % i ==0)     
   {       
    int min,max,middle;
    middle=(n -i/2)/i;
    min=middle-(i-1)/2;
    max=middle+(i-1)/2+1;
    printf("%d could added by %d numbers for %d to %d /n",n,i,min,max);
   }       
  }   
 }   

 return 0;