J-What day is that day?(打表找规律)

It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days?

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

There is only one line containing one integer N (1 <= N <= 1000000000).

Output

For each test case, output one string indicating the day of week.

Sample Input
2
1
2
Sample Output
Sunday
Thursday
Hint

A week consists of Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday.

 

题目的大致意思如下:

就是给你11 + 22 + 33 + ... + NN这个式子,并且告诉你现在是星期六,要你判断在11 + 22 + 33 + ... + NN这么多天之后是星期几。

一开始我想,如果直接写的话那么肯定会超时,那么就说明它肯定有规律的咯,那么一开始我是用手工去计算值,但是算了7个之后发现根本无法继续下去,后来我知道可以用打表找周期这种方法来找规律,于是去系统的学习了一下。

#include<stdio.h>
#include<string.h>
int main(){
	char word[100];
	scanf("%s",word);
	int len=strlen(word);
	//i代表的是周期 
	for(int i=1;i<=len;i++) if(len%i==0){	//这里因为我求的是字符串的周期,所以如果i能被len整除的话,那么就说明它是周期了,然后再进行下面的判断; 
		//ok用来标记; 
		int ok=1;
		for(int j=i;j<len;j++)
		//下面的语句才是关键。判断第j个与前面相对应的那几个是否相等; 
			if(word[j]!=word[j%i])   {ok=0; break;}
	}
}


 

于是用上面的方法就可以求出这道题的周期是294,那么就好做了;

#include<stdio.h>
#include<string.h>
#include<string>
using namespace std;
char ss[8][10]={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
int work(int n){
	int sum=1;
	for(int i=1;i<=n;i++){
		sum=sum*n;
		sum=sum%7;
	}
	return sum;
}
int main(){
	int T,n;
	int f[301]={0};
	for(int i=1;i<=294;i++){
		f[i]=f[i-1]+work(i);
		f[i]=f[i]%7;
	}
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		n=n%294;
		//printf("%d\n",f[n]);
		int t=f[n];
		printf("%s\n",ss[t]);
	}
}


很有用的方法,哈哈。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值