zoukankan      html  css  js  c++  java
  • 暑期实践日志(一)

    2015年7月6日

     

    题解这道题目是一个数学题,其中有两个需要注意的地方。

    第一:题目要求我们求将一个数分解成连续数字的和,而且要求数字个数最少。由题目意思可以推导出一个公式,所给的数N=(a+(a+k-1))/2。其中的a表示连续数字和中最小的那个数,k表示的是连续数字的个数,整个公式的由来是等差数列求和公式推导的。

    第二:在求解时,虽然已经推导出了公式,但是在求解时,根据公式,我们采取遍历所有的k的值来求解a,如果从2到n遍历的话会超时。所以这里要注意,要从2到2*n开根号,这样可以避免超时。

    代码:

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    int 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
    ",ans);
                    else
                        printf("%d + ",ans);
                }
            }
            else
                printf("IMPOSSIBLE
    ");
        }
    return 0;
    }
  • 相关阅读:
    jquery 实现 html5 placeholder 兼容password密码框
    php返回json的结果
    使用PHP读取远程文件
    Sharepoint 自定义字段
    Sharepoint 中新增 aspx页面,并在页面中新增web part
    【转】Sharepoint 2010 配置我的站点及BLOG
    JS 实现 Div 向上浮动
    UserProfile同步配置
    【转】Import User Profile Photos from Active Directory into SharePoint 2010
    Sharepoint 2010 SP1升级后 FIMSynchronizationService 服务无法开启
  • 原文地址:https://www.cnblogs.com/hbutACMER/p/4631647.html
Copyright © 2011-2022 走看看