| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付赛孚耐:软件保护加密专家
身份认证令牌USB KEY   
共有 564 人关注过本帖
标题:NKOJ 1004
收藏  订阅  推荐  打印 
neverDie
Rank: 2
等级:注册会员
威望:1
帖子:123
积分:1330
注册:2007-5-5
NKOJ 1004

http://acm.nankai.edu.cn/p1004.html

看了下ac的大部分都是用一个高度总结的数学公式,有没有直接对应题目的算法?
搜索更多相关主题的帖子: NKOJ  数学  acm  nankai  算法  
2007-11-17 18:04
無邪的睡脸
Rank: 3Rank: 3
来自:湖北武汉
等级:中级会员
威望:1
帖子:331
积分:3830
注册:2007-9-11

//南开oj-1004
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int n,m=1,i,j,k,flag,*p;
    scanf("%d",&n);
    p=(int *)calloc(n,sizeof(int));
    while(1)
    {
        flag=0;
        for(i=0;i<n;i++)
            if(p[i]==0)
            {
                p[i]=m;
                flag=1;
                break;
            }
            else
            {
                k=p[i]+m;
                j=(int)sqrt(k);
                if(k==j*j)
                {
                    p[i]=m;
                    flag=1;
                    break;
                }
                else
                    continue;
            }
        if(!flag)
            break;
        m++;
    }
    m--;
    free(p);
    printf("%d\n",m);
    getchar();
    getchar();
    return 0;
}
这题用啥公式啊,我这样做的,AC!

不要仅为成功而努力.要为做一个有价值的人而努力
kobe24j@sina.com

2007-12-20 13:34
leeco
Rank: 4
等级:高级会员
威望:8
帖子:870
积分:9666
注册:2007-5-10

贪心可以过,但我没有看出贪心性质。
2007-12-20 16:26
HJin
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:27
帖子:401
积分:4134
注册:2007-6-9

/**
my code for this problem.

the formula can be found by

first try on a few test cases for n small
then apply mathematical induction
*/

main()
{
    int n;

    while (scanf("%d", &n) != -1)
        printf("%d\n", n*(n+1)/2+(n-1)/2);

}

I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-12-20 23:34
aipb2007
Rank: 12Rank: 12Rank: 12
来自:CQU
等级:贵宾
威望:40
帖子:2881
积分:29414
注册:2007-3-18

搜索+贪心,但是也看不出贪心性质,猜的。

楼上的完全不懂,数学真好。

Fight  to win  or  die...
2007-12-21 20:32
diaoxue
Rank: 2
等级:注册会员
帖子:142
积分:1630
注册:2007-6-1

我也写了个:
#include <iostream>
#include <cmath>
using namespace std;
const int MAX=50;
int a[MAX]={0};
bool Sqn(int n)//判断是不是平方数
{
    if(floor(sqrt(n))*floor(sqrt(n))==n)
        return true;
    return false;
}
int main()
{
//    cout<<Sqn(9)<<endl;
    int N;
    int flag=1;
    while(flag)//输入
    {
        cout<<"Please input the number of stick(N):"<<endl;
        cin>>N;
        if(N>=1 && N<=50)
            flag=0;
        else
            cout<<"The number your input is boundary!"<<endl;
    }
    int cgd=1;//the candiedgound number
    int flg=1;
    while(flg)//put candiedgound
    {
        for(int i=0;i<N;i++)
        {
            if(a[i]==0 || Sqn(a[i]+cgd))
            {
                a[i]=cgd;
                cgd++;
                i=-1;//从零开始找
            }
        }
        cgd--;//减去不满足条件的最后一次加一
        flg=0;
        
    }
    cout<<"The largest number is:";
    cout<<cgd<<endl;
    return 0;
}

上善若水,水善利万物而不争,处众人之所恶
2007-12-22 20:10
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.056511 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved