完成该计划 [英] Finalize the program

查看:91
本文介绍了完成该计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入此测试是真的

10#底部的位数

4 2 2 2 2 2#此行中的第一位数度 - 1

1 2 2

1 3 2

1 2 3

3 2 2 2 2
2 2 2 2

1 3 3

3 3 3 3

2 4 3 3

2 2 3 4



输出

2 4 3 6 7 5 9 10 1 8



这里的任务是提高功率,然后比较他们的数字,按照数量增加的顺序得出索引

指数从最后开始

2 2 3 4

被提高为3 ^ 4 = 81

现在2 ^ 81 = 2417851639229258349412352



如果您提交此类号码,则工作不正确

输入

3

4 5 2 49 4 5

4 3 2 7 2 11

4 2 2 49 32 2



输出

3 2 1应该是

我的结论3 1 2








  #include   <   fstream  >  
#include < algorithm >

#include < stdio.h >
#include < math.h >
使用 namespace std;

class tower_t {
public
< span class =code-keyword> int num;
int height;
double val [ 11 ]; // содержимое
double 缓存[ 11 ]; // кэшдляускорениярасчета

// Конструктор
tower_t(){
for int i = 0 ; i< 11 ; i ++){
val [i] = 1 ;
cache [i] = 0 ;
}
height = 0 ;
}

// Тройнойлогарифмверхних3-хуровней
double head( int 级别){
if (cache [level] == 0 )cache [level] =(log2(log2(val [level]))+ log2( val [level + 1 ])* val [level + 2 ]);
return cache [level];
}

// Вычислениеверхушкидотехпокавлазитвdouble
void normalize(){
while (height> 1 &&(log2(val [height - 2 ])* val [height - 1 ])< 50 ){
val [height - 2 ] = pow(val [height - 2 ],val [height - 1 ]) ;
val [height - 1 ] = 1 ;
height--;
}
}

// Выводдляотладки
void print(){
#ifdef _DEBUG
printf ( %2d:{,num);
for int i = 0 ; i< height; i ++){
if (i> 0 ) printf( );
if (val [i]< 1000000000 ){
printf(< span class =code-string> %0.0f,val [i]);
} else {
printf( %0.3e,val [i]);
}
}
printf( } \ n );
#endif
}
};

// сравнениедвух
bool compare(tower_t& t1,tower_t& t2){
// 更多信息
int level =((t1.height> t2.height)?t1.height:t2。高度) - 3 ;
if (级别< 0 )level = 0 ;
if (t1.height == t2.height){ // еслиоднойвысоты,сравниваемпоэтажно
for int i = t1.height - 1 ; i> = 0 ; i--){
if (abs(t1.val [i] - t2.val [i])>(t1.val [i] * 1e- 14 )){
if (i< level){ // верхисовпалинижеlevel
return t1.val [i]< ; t2.val [I];
}
break ;
}
}
}
return t1.head(level)< t2.head(电平);
}

int main()
{
// Считываниезадания
ifstream in input.txt);
int cnt;
>> CNT;
tower_t * towers = new tower_t [cnt];
for int i = 0 ; i< cnt; i ++){
int len;
>> LEN;
塔[i] .num = i + 1 ;
bool write = true ;
for int j = 0 ; j< = len; j ++){
int val;
>> VAL;
if (val< = 1 )write = ; // еслиуровень< =1товышенечитать
if (write){
towers [i] .val [j] = val;
塔[i] .height = j + 1 ;
}
}
塔[i] .print();
towers [i] .normalize();
}
// Сортировка
sort(塔楼,塔楼+ cnt,比较);
// Выводрезультата
ofstream out( output.txt);
for int i = 0 ; i< cnt; i ++){
out<<塔[i] .num<< ;
towers [i] .print();
}
out<< ENDL;
out.close();
删除 []塔;
return 0 ;
}





我的尝试:



数论 - 电力塔比较的复杂性等级 - 数学堆栈交换 [ ^ ]

解决方案

与您的Python问题相同: https://www.codeproject.com/Questions/1246813/Is-there-any-algorithm-for-comparing-large-numbers [ ^ ]。

input with this test is true
10 # the number of digits in the bottom
4 2 2 2 2 2 # first digit number of degrees in this row -1
1 2 2
1 3 2
1 2 3
3 2 2 2 2
2 2 2 2
1 3 3
3 3 3 3 3
2 4 3 3
2 2 3 4

output
2 4 3 6 7 5 9 10 1 8

Here the task is to raise to a power and then compare their numbers to derive the index in the order of increasing numbers
exponentiation is from the end
2 2 3 4
is raised as 3 ^ 4 = 81
Now 2 ^ 81 = 2417851639229258349412352

if you submit such numbers, it works incorrectly
input
3
4 5 2 49 4 5
4 3 2 7 2 11
4 2 2 49 32 2

output
3 2 1 should be
my conclusion 3 1 2




#include <fstream>
#include <algorithm>
 
#include <stdio.h>
#include <math.h>
using namespace std;
 
class tower_t {
public:
    int num; 
    int height; 
    double val[11]; // содержимое
    double cache[11]; // кэш для ускорения расчета
 
    // Конструктор
    tower_t() {
        for (int i = 0; i < 11; i++) {
            val[i] = 1;
            cache[i] = 0;
        }
        height = 0;
    }
 
    // Тройной логарифм верхних 3-х уровней
     double head(int level) {
        if(cache[level] == 0) cache[level] = (log2(log2(val[level])) + log2(val[level + 1]) * val[level + 2]);
        return cache[level];
    }
 
    // Вычисление верхушки до тех пока влазит в double
    void normalize() {
        while(height > 1 && (log2(val[height - 2]) * val[height - 1]) < 50) {
            val[height - 2] = pow(val[height - 2], val[height - 1]);
            val[height - 1] = 1;
            height--;
        }
    }
 
    // Вывод для отладки
    void print() {
#ifdef _DEBUG
        printf("%2d: {", num);
        for (int i = 0; i < height; i++) {
            if (i > 0) printf(", ");
            if(val[i] < 1000000000) {
                printf("%0.0f", val[i]);
            } else {
                printf("%0.3e", val[i]);
            }
        }
        printf("}\n");
#endif
    }
};
 
// сравнение двух 
bool compare(tower_t& t1, tower_t& t2) {
    // этаж с которого сравнивать три последних уровня
    int level = ((t1.height > t2.height) ? t1.height : t2.height) - 3;
    if (level < 0) level = 0;
    if(t1.height == t2.height) { // если  одной высоты, сравниваем поэтажно
        for (int i = t1.height - 1; i >= 0; i--) {
            if (abs(t1.val[i] - t2.val[i]) > (t1.val[i] * 1e-14)) {
                if (i < level) { // верхи  совпали ниже level
                    return t1.val[i] < t2.val[i];
                }
                break;
            }
        }
    }
    return t1.head(level) < t2.head(level);
}
 
int main()
{
    // Считывание задания
    ifstream in ("input.txt");
    int cnt;
    in >> cnt;
    tower_t* towers = new tower_t[cnt];
    for (int i = 0; i < cnt; i++) {
        int len;
        in >> len;
        towers[i].num = i + 1;
        bool write = true;
        for (int j = 0; j <= len; j++) {
            int val;
            in >> val;
            if (val <= 1) write = false; // если уровень <= 1 то выше не читать
            if(write) {
                towers[i].val[j] = val;
                towers[i].height = j + 1;
            }
        }
        towers[i].print();
        towers[i].normalize();
    }
    // Сортировка
    sort(towers, towers + cnt, compare);
    // Вывод результата
    ofstream out("output.txt");
    for (int i = 0; i < cnt; i++) {
        out << towers[i].num << " ";
        towers[i].print();
    }
    out << endl;
    out.close();
    delete[] towers;
    return 0;
}



What I have tried:

number theory - Complexity class of comparison of power towers - Mathematics Stack Exchange[^]

解决方案

Same as your Python question: https://www.codeproject.com/Questions/1246813/Is-there-any-algorithm-for-comparing-large-numbers[^].


这篇关于完成该计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆