Day 28 算法笔记之数学问题 5.1简单数学

目录

1.数字黑洞

2.我要通过


1.数字黑洞

#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <sys/time.h>
using namespace std;

int martix[100];

void to_array(int n){
	
	for(int i=3;i>=0;i--){
		martix[i] = n%10;
		n = n/10;
	}
}

int get_value(int a[]){
	int final=0;
	for(int i=0;i<4;i++){
		final = final*10+a[i];
	}
	return final;
}



bool cmp(int a,int b){
	return a>b;
}


int main(){
	
	int n;
	scanf("%d",&n);
	
	int goal=0;
	int x,y;
	while(goal!=6174){
		to_array(n);
		
		bool flag=false;
		int temp = martix[0];
		for(int i=1;i<4;i++){
			if(martix[i]!=temp){
				flag = true;
			}
		}
		
		if(flag==false){
			int value = get_value(martix);
			printf("%04d-%04d=0000\n",value,value);
			return 0;
		}
		
		sort(martix,martix+4);
		x = get_value(martix);
		sort(martix,martix+4,cmp);
		y = get_value(martix);
		goal = y-x;
		n = goal;
		printf("%04d-%04d=%04d\n",y,x,goal);
	}
	
	
	
	
	return 0;
} 

2.我要通过

把数学关系看出来就简单了

首先p和t都只能有一个,而且不能有其他字母,否则一定错

接下来便是A的问题,第三条规则指出,如果已知一个式子一定成立,那么以下操作同时发生就可以生成一个成立的:

1.p和t之间增加一个A

2.t之后增加p之前A的数量

#include <cstdio>
#include <cctype>
#include <cstring>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <sys/time.h>
using namespace std;

int martix[100];

void to_array(int n){
	
	for(int i=3;i>=0;i--){
		martix[i] = n%10;
		n = n/10;
	}
}

int get_value(int a[]){
	int final=0;
	for(int i=0;i<4;i++){
		final = final*10+a[i];
	}
	return final;
}



bool cmp(int a,int b){
	return a>b;
}


int main(){
	
	int t;
	scanf("%d",&t);
	
	while(t--){
		char str[110];
		scanf("%s",str);
		
		int len=strlen(str);
		int num_p=0,num_t=0,other=0;
		int loc_p=-1,loc_t=-1;
		
		for(int i=0;i<len;i++){
			if(str[i]=='P'){
				num_p++;
				loc_p=i;
			}else if(str[i]=='T'){
				num_t++;
				loc_t=i;
			}else if(str[i]!='A'){
				other++;
			}
		}
		
		if((num_p!=1)||(num_t!=1)||(other!=0)||(loc_t-loc_p<=1)){
			printf("NO\n");
			continue; 
		}
		
		int x=loc_p,y=loc_t-loc_p-1,z=len-loc_p-1;
		
		if(z-x*(y-1)==x){
			printf("YES\n");
		}else{
			printf("NO\n");
		}
	}
	
	
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/aixiaoxiao13/article/details/120895636