• 数学题还是计算机题啊


    这道题是从http://blog.csdn.net/fmddlmyy/article/details/7234119#plain看来的。伐木丁丁鸟鸣嘤嘤

     

    伐木丁丁鸟鸣嘤嘤

    1. 缘起

    岳父大人拿来一张去年的报纸,说有一道很难做的益智题,让我看看,就是下图中的题目:

    如果这是一道正常的益智题,岳父大人既然做不出来,我是肯定做不出来的。不过看过题目后,我觉得这个题目就是一个典型的编程作业。把编程作业当作益智题,显然是个恶作剧了。下面简单介绍一下如何编程求解这道题目。

     

    2. 我的解法

    这题可以表述如下(因为第一步肯定是2011+7=2018,最后一步肯定是2017-5=2012):

    已知x0 = 2018, x1=2017,

    f1(x)= (x/2)+7;

    f2(x)= (x+7)/2;

    f3(x)= (x*3)-5;

    f4(x)= (x-5)*3;

    并且fa1fa2…fan(x0)= x1

    a1,a2,…,an属于集合{1,2,3,4}。

    求a1,a2,…,an.
    用程序暴力解法如下:
    // Math2012.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <vector>
    #include <set>
    #include <windows.h>
    using namespace std;
    
    int f1(int even)
    {
    	int ret = even / 2 + 7;
    	return ret;
    }
    int f2(int odd)
    {
    	int ret = (odd + 7) / 2;
    	return ret;
    }
    int f3(int input)
    {
    	int ret = (input - 5) * 3;
    	return ret;
    }
    int f4(int input)
    {
    	int ret = input * 3 - 5;
    	return ret;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int initial = 2018;
    	int destine = 2017;//2017 674 1334 2661 892 299 584 1154 2301 772 1530 515 1016
    	std::set<int> seed_array;
    	std::set<int> seed_array_copy;
    	std::set<int>::iterator it;
    	seed_array.insert(initial);
    	bool success = false;
    	while (true)
    	{
    		Sleep(50);
    		printf("seed_array:
    ");
    		for (it = seed_array.begin(); it != seed_array.end(); ++it)
    		{
    			int temp = *it;
    			printf("%d	", temp);
    			if (temp % 2 == 0)
    			{
    				int ret1 = f1(temp);
    				seed_array_copy.insert(ret1);
    				if (ret1 == destine)
    				{
    					success = true;
    					printf("success.
    ");
    					break;
    				}
    			}
    			else
    			{
    				int ret2 = f2(temp);
    				seed_array_copy.insert(ret2);
    				if (ret2 == destine)
    				{
    					success = true;
    					printf("success.
    ");
    					break;
    				}
    			}
    			int ret3 = f3(temp);
    			seed_array_copy.insert(ret3);
    			if (ret3 == destine)
    			{
    				success = true;
    				printf("success.
    ");
    				break;
    			}
    			int ret4 = f4(temp);
    			seed_array_copy.insert(ret4);
    			if (ret4 == destine)
    			{
    				success = true;
    				printf("success.
    ");
    				break;
    			}
    		}
    		seed_array = seed_array_copy;
    		seed_array_copy = std::set<int>();   
    		printf("
    ");
    		if (success)
    		{
    			break;
    		}
    	}
    
    	return 0;
    }
    

    程序是一目了然的,就是最笨的穷举法。这个程序要执行好几遍,第一遍把destine = 2017, 然后就会输出下一个destine = 674, 以此类推,destine = 2017 674 1334 2661 892 299 584 1154 2301 772 1530 515 1016。见下图:



    3.结束

    有点无聊的题,不知道谁想出来的。

  • 相关阅读:
    [编程题]多多的数字组合
    mac传输文件到服务器
    git 清除缓存、查看add内容
    go build
    vim编辑器
    Git: clone指定分支
    查看端口占用以及kill
    curl小记录
    Python3.9 malloc error: can’t allocate region
    设计模式-策略模式
  • 原文地址:https://www.cnblogs.com/james1207/p/3362210.html
Copyright © 2020-2023  润新知