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;
    }
    
  • 相关阅读:
    服务器架构前面加了防火墙,Nginx如何获取客户端真实ip???
    Prometheus学习笔记(5)Grafana可视化展示
    Prometheus学习笔记(4)什么是pushgateway???
    Prometheus学习笔记(3)什么是node_exporter???
    Prometheus学习笔记(2)Prometheus部署
    Prometheus学习笔记(1)Prometheus架构简介
    Centos 7 中的ulimit -n 65535 对进程的文件句柄限制不生效??
    Jenkins实用发布与回滚PHP项目生产实践
    Ansible入门笔记(3)之Playbook
    Ansible入门笔记(2)之常用模块
  • 原文地址:https://www.cnblogs.com/quark/p/2564023.html
Copyright © 2011-2022 走看看