幸运数字

幸运数字

Alice认为4和7是幸运数字,并且如果一个数字是几个幸运数字的和,那么这个数字也是幸运数字,例如

  • 14=7+7
  • 18=7+7+4
  • 11=7+4
  • 7=7
    现在给你Q个数字,请你分别判断每个数字是否为幸运数字

我的代码

#include<bits/stdc++.h>
using namespace std;
bool xnyp(long long x){
	if(x<4)
		return false;
	if(x==7 || x==4)
		return true; 
	else if(xnyp(x-7) || xnyp(x-4))
		return true;
	else return false;  
}
int main(void){
	int q;
	long long x;
	cin>>q;
	for(int i=0;i<q;i++){
		cin>>x;
		if(xnyp(x))
			cout<<"Yes"<<endl;
		else 
			cout<<"No"<<endl;  
	}
	return 0;
}

问题就在于大数怎么办?这种东西竟然是找规律
这个规律有两层。
一层是这样
x是幸运数,有一个数字7加出来的,那么这个数字7可以换成4+4,那么x+1也是幸运数。
如果有7个4呢,那么就有4个7,可以替换4次,那么x,x+1,x+2,x+3,x+4都是幸运数,那么28是幸运数字,29 30 31 32 也都是,而32里面也有7个4啊,类推,33 34 34 36 都是幸运数。
那么28以后必然是幸运数字。
接下来是室友想出来的,继续缩小范围的方法。
幸运树必然x==3m+4n的样式。
那么如果m>=n,必然是幸运数字。
如果m<n呢?(为了寻找不幸运的数字)
取,n 为1,2,3,然后,1,m取0,1 3,m取0,1,2,那么找到的17是最大的不幸运数字。
取n等于4,那么m可以0,1,2,3
16 19 22 25,发现全是幸运数字了。

代码

#include<bits/stdc++.h>
using namespace std;
int a[]={1,2,3,5,6,9,10,13,17};

int main(void){
	int q;
	long long x;
	bool flag=true;
	cin>>q;
	for(int i=0;i<q;i++){
		cin>>x;
		flag = true;
		for(int j=0;j<9;j++){
			if(x==a[j]){
				flag = false;
				break;
			}
		}
		if(flag)
			cout<<"Yes"<<endl;
		else 
			cout<<"No"<<endl;  
	}
	return 0;
}
posted @ 2019-03-13 09:03  lingr7  阅读(698)  评论(0编辑  收藏  举报