zoukankan      html  css  js  c++  java
  • [Project Euler] Problem 51

    Problem Description

    By replacing the 1st digit of *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.

    By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.

    Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.

    C++

    This problem seems a bit hard to me at the first look. However, through several steps of analysis, I simplify this problem to some extent .

    #pragma once
    
    const int MAX_NUM = 1000000;
    const int TARGET_COUNT = 8;
    
    int* g_primeArray = NULL;
    int g_primeLength = 0;
    
    // pre decalaration
    bool CheckThisPrime(int prime, char sameChar, int sameCount);
    
    void Initialize()
    {
    	g_primeLength = MAX_NUM / 5;
    	g_primeArray = new int[g_primeLength];
    	MakePrimes(g_primeArray, g_primeLength, MAX_NUM);
    }
    // By analysis, we can infer that the result should has at least three digits with value 0 or 1 or 2, which are not including the lowest digit of the number.
    void Problem_51()
    {
    	Initialize();
    
    	char digitArray[10] = { 0 };
    	int digitCount = 0;
    	for(int i =0; i<g_primeLength; i++)
    	{
    		int curPrime = g_primeArray[i];
    		if(curPrime < 1000)
    			continue;
    
    		sprintf(digitArray, "%d", curPrime);
    		digitCount = strlen(digitArray);
    
    		int zeroCount = 0;
    		int oneCount = 0;
    		int twoCount = 0;
    		for(int j =0; j<digitCount - 1; j++)
    		{
    			if(digitArray[j] == '0')
    				zeroCount++;
    			if(digitArray[j] == '1')
    				oneCount++;
    			if(digitArray[j] == '2')
    				twoCount++;
    		}
    
    		if(zeroCount < 3 && oneCount < 3 && twoCount < 3)
    			continue;
    
    		printf("%d\n", curPrime);
    
    		// int temp = 0;
    		// digitArray[0] = '0';
    		// sscanf(digitArray, "%d", &temp);
    		// printf("%d\n", temp);
    		
    		if(zeroCount >= 3)
    		{
    			if(CheckThisPrime(curPrime, '0', zeroCount))
    			{
    				printf("result = %d\n", curPrime);
    				return;
    			}
    		}
    		
    		if(oneCount >=3)
    		{
    			if(CheckThisPrime(curPrime,'1', oneCount))
    			{
    				printf("result = %d\n", curPrime);
    				return;
    			}
    		}
    
    		if(twoCount >=3)
    		{
    			if(CheckThisPrime(curPrime, '2', twoCount))
    			{
    				printf("result = %d\n", curPrime);
    				return;
    			}
    		}
    
    	}
    }
    
    bool CheckThisPrime(int prime, char sameChar, int sameCount)
    {
    	bool ret = false;
    	char digitArray[10] = { 0 };
    	int digitCount = 0;
    	
    	sprintf(digitArray, "%d", prime);
    	digitCount = strlen(digitArray);
    
    	int* digitIndexArray = new int[sameCount];
    	int index=0;
    	for(int j =0; j<digitCount - 1; j++)
    	{
    		if(digitArray[j] == sameChar)
    			digitIndexArray[index++] = j;
    	}
    
    	// determine which three digits should be modified
    	
    	bool* useArr = new bool[sameCount];
    	useArr[0] = true;
    	useArr[1] = true;
    	useArr[2] = true;
    
    	while(true)
    	{
    		char digitArrayBak[10] = { 0 };
    		memcpy(digitArrayBak, digitArray, 10 * sizeof(char));
    		
    		int primeCount = 1;
    		int notPrimeCount = 0;
    		int notPrimeMax = 2 - (sameChar - '0');
    		for(char c=(char)(sameChar + 1); c<='9' ; c++)
    		{
    			for(int i = 0; i<sameCount; i++)
    			{
    				if(useArr[i])
    				{
    					digitArrayBak[digitIndexArray[i]] = c;
    				}
    			}
    			int temp = 0;
    			sscanf(digitArrayBak, "%d", &temp);
    			if(IsPrime(temp))
    			{
    				primeCount++;
    			}
    			else
    			{
    				notPrimeCount++;
    			}
    
    			if(notPrimeCount > notPrimeMax)
    			{
    				break;
    			}
    
    			if(primeCount == TARGET_COUNT)
    			{
    				ret = true;
    				goto LRESULT;
    			}
    		}
    
    		int reachEndCount = 0;
    		for(int i = sameCount -1; i>=0; i--)
    		{
    			if(useArr[i])
    			{
    				reachEndCount++;
    			}
    			else
    			{
    				break;
    			}
    		}
    
    		if(reachEndCount == 3)
    			break;
    
    		for(int i = sameCount -1 - reachEndCount; i>=0; i--)
    		{
    			if(useArr[i])
    			{
    				useArr[i] = false;
    				useArr[i+1] = true;
    				break;
    			}
    		}
    	}	
    	
    LRESULT:
    	delete[] digitIndexArray;
    	delete[] useArr;
    	return ret;
    }
    
  • 相关阅读:
    项目中 2个或者多个EF模型 表名称相同会导致生成的实体类 覆盖的解决方法
    Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法;关于如何查看 EntityValidationErrors 详细信息的解决方法
    深夜杳杳
    我在学习
    IIS下新建虚拟目录
    Win10资源管理器异常
    Docker笔记三:Docker数据卷和DockerFile
    Docker笔记二:Docker拷贝,提交,进入容器和阿里云加速
    ElasticSearch7.x系列三:Logstash的使用
    ElasticSearch7.x系列二:Kibana的使用和C#的Nest客户端
  • 原文地址:https://www.cnblogs.com/quark/p/2564023.html
Copyright © 2011-2022 走看看