zoukankan      html  css  js  c++  java
  • 连续自然数和(codevs 1312)

    题目描述 Description

    对于一个自然数M,求出所有的连续的自然数段,使得这些连续自然数段的全部数字和为M.
    eg:1998+1999+2000+2001+2002=10000,所以从1998到2002的一个自然数段为M=10000的一个解。 

    输入描述 Input Description

    一个数M

    输出描述 Output Description

    每行两个数,为连续自然数段的一头一尾,所有输出行的第一个数按照升序排列

    样例输入 Sample Input

    10000

    样例输出 Sample Output

    18 142

    297 328

    388 412

    1998 2002

    //优化真的很难想啊
    #include<cstdio>
    #include<iostream>
    #define M 1000010
    using namespace std;
    int a[M];
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
          a[i]=i+a[i-1];
        int tmp=2;
        for(int i=1;i<=n/2+1;i++)
          for(int j=i+1;j<=n/2+1;j++)
            if(a[j]-a[i-1]==n)
            {
                printf("%d %d
    ",i,j);
                tmp=j;
                break;
            }
            else if(a[j]-a[i-1]>n)break;
        return 0;
    }
    View Code
  • 相关阅读:
    触屏时间控制
    小程序 坐标算距离 (copy)
    微信小程序 对接口常用
    conversion function to_char to_number
    南通
    日期 function
    数字 function
    字符串处理函数
    沪通铁路1
    NVL NVL2 COALESCE NULLIF decode
  • 原文地址:https://www.cnblogs.com/harden/p/5640697.html
Copyright © 2011-2022 走看看