uva371 Ackermann Functions

来源:互联网 发布:安全加密软件 编辑:程序博客网 时间:2024/05/01 22:07

题目: 给你一个公式循环产生数列,指导产生1为止,输出最大的数的位置和数值

注意: 与之前题目的输出是不同的!!!(突然想骂人aaaaaaaa)这次不用考虑输出大小问题

map有自动排序的功能。但因为对map不熟悉(黑脸aaaaaaaaaaa)走了两小时弯路


#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sstream>

#include<cstdio>

#include<iostream>

#include<math.h>

#include <map>

#include <vector>

#include <algorithm>

using namespacestd;

long func(long n){

    int h =1;

    while(1) {

        

        if(n %2 == 1) {

            h++;

            n = 3 * n +1;

        }

        if(n %2 == 0) {

            h++;

            n /= 2;

        }

        if(n ==1) break;

    }

    return h;

}

int main(){

    long a,b;

    map<long,long >num;

    map<long,long >::iterator it;

  //  vector<int> d;

    while((cin>> a >> b)&&(a+b)) {

       // int flag = 1;

        if(a > b){

            long temp = a;

            a = b;

            b = temp;

       //     flag=0;

        }

        for(long i = a; i <= b; i ++) {

            long tmep =func(i);

            it = num.find(tmep); 1️⃣map中find函数找的是num【】括号里的。且find的返回对象是iterator

            if( it != num.end()){

            

            }

            else  num[tmep] = i;

            

        }

        

        it = num.end();

        it--; 2️⃣不知道为什么it只能自增或自减。不能用+1或-1

   // if(flag == 1)

    printf("Between %ld and %ld, %ld generates the longest sequence of %ld values.\n",a,b,it->second,(it->first)-1 );3️⃣直接用num.end()->first会报错

       

    num.clear();

    

    

    }//end-while

    

    

    

    return0;

}




1 0
原创粉丝点击