UVa 11254 - Consecutive Integers Solution

#include <iostream>
#include <cmath>
using namespace std;
int n,st,en;
int main(){
    ios::sync_with_stdio(0);
    while(1){
        cin>>n;
        if(n<1)
            return 0;
        st=n,en=0;
        for(int k=sqrt(2*n);k>0;k--){
            int m=(2*n-k-k*k)/(2*(k+1));
            if((k+1)*(2*m+k)==2*n){
                st=m,en=k;
                break;
            }
        }
        if(st==0)
            st=1,en--;
        cout<<n<<" = ";
        cout<<st<<" + ... + "<<st+en<<endl;
    }
}

Comments