Summer Practice Diary (i)

Source: Internet
Author: User

July 6, 2015

The problem is a math problem, there are two places to pay attention to.

First: The topic asks us to divide a number into successive numbers and requires the number of digits to be the least. By the meaning of the topic can be deduced a formula, the number of n= (A + (a+k-1))/2. Where a represents a continuous number and the smallest of the number, K represents the number of consecutive numbers, the origin of the whole formula is arithmetic progression summation formula derivation.

Second: In the solution, although the formula has been deduced, but in the solution, according to the formula, we take the value of traversing all k to solve a, if the traversal from 2 to N will time out. So it's important to note that you need to open the square root from 2 to 2*n, so you can avoid timeouts.

Code:

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
Main ()
{
int T;
scanf ("%d", &t);
 while (t--)
{
int n,k,flag=0;
scanf ("%d", &n);
 for (int i=2;i<=int (sqrt(n*2)); i++)
{
int temp;
Temp=2*n+i-i*i;
if (temp%i==0&& (temp/i)%2==0&&temp>0)
{
K=i;
flag=1;
break;
}
}
if (flag)
{
printf ("%d =", n);
int a= (2*N+K-K*K)/(2*K);
 for (int i=0;i<k;i++)
{
int ans=a+i;
if (i==k-1)
printf ("%d\n", ans);
Else
printf ("%d +", ans);
}
}
Else
printf ("impossible\n");
}
return 0;
}

Summer Practice Diary (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.