zoj 3785 What day is that day?

题意:求从1的1次方加到n的n次方的和对7取余所得结果。

思路:打表找规律。所谓打表就是把部分所求结果保留下来,通过观察已有数据找到规律,或在程序中直接判定出所求规律。对于这种找周期的题,打表最适合不过了!!!

打表程序:

#include<stdio.h>
#include<string.h>
int a[50010],T,n,m,i,j,k,flag;
int work(int n)
{
    int i,sum=1;
    for(i=0;i<n;i++)
    {
        sum*=n;
        sum%=7;
    }
    return sum;
}
int main()
{
    a[1]=1;
    for(i=1;i<5000;i++)
    {
        a[i]=a[i-1]+work(i);
        a[i]%=7;
    }
    for(i=2;i<5000;i++)
    {
        flag=1;
        for(j=1;j<i;j++)
        {
             if(a[j]!=a[i+j]) flag=0;
        }
        if(flag==1)
        {
            printf("circle:%d\n",i);break;
        }

    }
    return 0;
}

提交程序:

#include<stdio.h>
#include<string.h>
int a[500010];
char str[7][10]={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
int work(int n)
{
    int i;
    int sum=1;
     for(i=0;i<n;i++)
     {
        sum*=n;
        sum%=7;
     }
     return sum;
}
int main()
{
    int T,i,j,k,n;
    a[0]=0;
    for(i=1;i<294;i++)
    {
        a[i]=a[i-1]+work(i);
        a[i]%=7;
    }
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        n%=294;
        printf("%s\n",str[a[n]]);
    }
    return 0;
}


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页