| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1135 人关注过本帖
标题:NKOJ 1004
只看楼主 加入收藏
neverDie
Rank: 1
等 级:新手上路
威 望:1
帖 子:123
专家分:0
注 册:2007-5-5
收藏
 问题点数:0 回复次数:5 
NKOJ 1004
http://acm.nankai.edu.cn/p1004.html

看了下ac的大部分都是用一个高度总结的数学公式,有没有直接对应题目的算法?
搜索更多相关主题的帖子: NKOJ 数学 acm nankai 算法 
2007-11-17 18:04
無邪的睡脸
Rank: 2
等 级:等待验证会员
威 望:1
帖 子:344
专家分:13
注 册:2007-9-11
收藏
得分:0 
//南开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!
2007-12-20 13:34
leeco
Rank: 4
等 级:贵宾
威 望:10
帖 子:1029
专家分:177
注 册:2007-5-10
收藏
得分:0 
贪心可以过,但我没有看出贪心性质。
2007-12-20 16:26
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
/**
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: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
搜索+贪心,但是也看不出贪心性质,猜的。

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

Fight  to win  or  die...
2007-12-21 20:32
diaoxue
Rank: 1
等 级:新手上路
帖 子:142
专家分:0
注 册:2007-6-1
收藏
得分:0 
我也写了个:
#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
快速回复:NKOJ 1004
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014818 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved