zoukankan      html  css  js  c++  java
  • 51Nod-1015 水仙花数【进制+查表搜索】

    1015 水仙花数

    基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
    水仙花数是指一个 n 位数 ( n >= 3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153)
    给出一个整数M,求 >= M的最小的水仙花数。
    Input
    一个整数M(10 <= M <= 1000)
    Output
    输出>= M的最小的水仙花数
    Input示例
    99
    Output示例
    153

    问题链接1015 水仙花数

    问题分析

    程序中所定义的水仙花数有哪些是不知道的,所以有必要先编写一个程序看看。

    辅助程序都写出来了,那就干脆直接生成真正程序用的语句(AC的程序的第5行)吧。

    实际上,计算查找水仙花数的程序,随着位数的增加,需要的运行时间越来越多,简直是无法忍受的。好在这个程序不用提交,自己用。

    由于这个辅助程序时间复杂大,所以不能基于这个算法来编写提交的程序,不然会LTE。只能借用一下这个程序!

    有了辅助程序,后面的事情就简单了。

    程序说明:(略)

    题记:编写一个辅助程序是一个好主意,常常需要的!

    参考链接:(略)



    生成水仙花数数组定义的辅助C++语言程序:

    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    const int N = 3;
    const int MAXN = 6;
    const int BASE = 10;
    
    int main()
    {
        cout << "int dn[]={";
    
        int count=0;
        for(int i=N; i<=MAXN; i++) {
            int start = 1, end;
            for(int j=1; j<i; j++)
                start *= BASE;
            end = start * BASE;
    
            for(int j=start; j<end; j++) {
                int v=j, dn=0;
                while(v) {
                    dn += pow(v % 10, i);
                    v /= BASE;
                }
                if(dn == j) {
                    if(++count != 1)
                        cout << ", ";
                    cout << j;
                }
            }
        }
    
        cout << "};" << endl;
    
        return 0;
    }



    AC的C++程序如下:

    #include <iostream>
    
    using namespace std;
    
    int dn[]={153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834};
    
    int main()
    {
        int m, i;
    
        cin >> m;
    
        for(i=0; ; i++)
            if(dn[i] >= m) {
                cout << dn[i] << endl;
                break;
            }
    
        return 0;
    }






  • 相关阅读:
    java课后思考问题(二)
    论文-MS-CNN
    论文-ION--Inside-Outside Net: Detecting Objects in Context with Skip
    51Nod--1285-山峰和分段
    论文-Learning Features and Parts for Fine-Grained Recognition
    论文-Multi-view Convolutional Neural Networks for 3D Shape Recognition
    论文-SPP_net
    Leetcode 448. Find All Numbers Disappeared in an Array
    FlowNet
    LeetCode Weekly Contest 21
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7563722.html
Copyright © 2011-2022 走看看