Google Code Jam之Alien Numbers之我的解答

本文介绍了一种将外星数字系统中的数值从一种形式转换到另一种形式的方法。通过使用特定的算法,可以处理不同基数的数字系统之间的转换,并提供了一个实际的C++实现示例。
由于时间的限制,程序有些地方的容错性不够,以//!! 标出。
运行成功,经Google Code Jam鉴定为正确。
题目为:
 
Alien Numbers
Problem

The decimal numeral system is composed of ten digits, which we represent as "0123456789" (the digits in a system are written from lowest to highest). Imagine you have discovered an alien numeral system composed of some number of digits, which may or may not be the same as those used in decimal. For example, if the alien numeral system were represented as "oF8", then the numbers one through ten would be (F, 8, Fo, FF, F8, 8o, 8F, 88, Foo, FoF). We would like to be able to work with numbers in arbitrary alien systems. More generally, we want to be able to convert an arbitrary number that's written in one alien system into a second alien system.

Input

The first line of input gives the number of cases, N . N test cases follow. Each case is a line formatted as

alien_number source_language target_language

Each language will be represented by a list of its digits, ordered from lowest to highest value. No digit will be repeated in any representation, all digits in the alien number will be present in the source language, and the first digit of the alien number will not be the lowest valued digit of the source language (in other words, the alien numbers have no leading zeroes). Each digit will either be a number 0-9, an uppercase or lowercase letter, or one of the following symbols !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

Output

For each test case, output one line containing "Case #x : " followed by the alien number translated from the source language to the target language.

Limits

1 ≤ N ≤ 100.

Small dataset

1 ≤ num digits in alien_number ≤ 4, 2 ≤ num digits in source_language ≤ 10, 2 ≤ num digits in target_language ≤ 10.

Large dataset

1 ≤ alien_number (in decimal) ≤ 1000000000, 2 ≤ num digits in source_language ≤ 94, 2 ≤ num digits in target_language ≤ 94.

Sample

Input Output

4 9 0123456789 oF8 Foo oF8 0123456789 13 0123456789abcdef 01 CODE O!CDE? A?JM!.

Case #1: Foo Case #2: 9 Case #3: 10011 Case #4: JAM!
我的解答的源代码:

#include <iostream>

#include <fstream>

#include <iterator>

 

using namespace std;

 

//A helper function to improve performance of power, basically copied from <stl_numeric.h> of SGI STL

inline long power(long x, long n)

{

    if(n == 0) return 1;

    else

    {

        while((n & 1) == 0)

        {

            n >>= 1;

            x *= x;

        }

 

        long y = x;

        n >>=1;

        while(n != 0)

        {

            x *= x;

            if((n & 1) != 0)

                y *= x;

            n >>=1;

        }

        return y;

    }

}

 

int main(int argc, char* argv[])

{

    //Only for memerizing usage for myself

    if(argc != 3)

    {

        cout << "Usage: nstrans INPUT_FILE OUTPUT_FILE" << endl;

        return 0;

    }

 

    ifstream fin(argv[1]); //!!

    ofstream fout(argv[2]); //!!

 

    //How many cases in total?

    int case_max;

    fin >> case_max;

    fin.ignore(); // ''

 

    string source_number;

    string source_language;

    string target_language;

    string target_number;

 

    int source_digits; //How many digits in source_number?

    int source_base; //How many digits in source_language?

    int target_base; //How many digits in target_language?

 

    long num; //alien number in demical, since it's less than 1000000000, it's in long's range----assert(sizeof(long) >= 4)

 

    char tmp; //for storing a temp char

 

    for(int cur_case = 1; cur_case <= case_max; cur_case++)

    {

        //some cleanup

        num = 0;

        source_number = source_language = target_language = target_number = "";

 

        //tear the case apart

        while((tmp = fin.get()) != ' ') source_number += tmp; //!!

        while((tmp = fin.get()) != ' ') source_language += tmp; //!!

        while((tmp = fin.get()) != '') target_language += tmp; //!!

 

        //

        source_digits = source_number.length();

        source_base = source_language.length();

        target_base = target_language.length();

 

        int e; //exponent

 

        //source number to decimal value

        for(e = 0; e < source_digits; e++)

        {

            num += source_language.find((source_number[source_digits-1 - e])) * power(source_base, e);

        }

 

        #ifdef MY_DEBUG

            cout << source_number << ' ' << num <<' ';

        #endif

 

        //decimal value to target number(reversal order)

        e = 1;

        while(num > 0)

        {

            target_number += target_language[num % target_base];

            num /= target_base;

        }

 

        fout << "Case #" << cur_case << ": ";

 

        //reverse the target number and output

        ostream_iterator<char> foutit(fout);

        copy(target_number.rbegin(), target_number.rend(), foutit);

 

        fout << endl;

 

        #ifdef MY_DEBUG

        ostream_iterator<char> outit(cout);

        copy(target_number.rbegin(), target_number.rend(), outit);

        cout << endl;

        #endif

    }

 

    return 0;

}
 
【四轴飞行器】非线性三自由度四轴飞行器模拟器研究(Matlab代码实现)内容概要:本文围绕非线性三自由度四轴飞行器模拟器的研究展开,重点介绍了基于Matlab的建模与仿真方法。通过对四轴飞行器的动力学特性进行分析,构建了非线性状态空间模型,并实现了姿态与位置的动态模拟。研究涵盖了飞行器运动方程的建立、控制系统设计及数值仿真验证等环节,突出非线性系统的精确建模与仿真优势,有助于深入理解飞行器在复杂工况下的行为特征。此外,文中还提到了多种配套技术如PID控制、状态估计与路径规划等,展示了Matlab在航空航天仿真中的综合应用能力。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的高校学生、科研人员及从事无人机系统开发的工程技术人员,尤其适合研究生及以上层次的研究者。; 使用场景及目标:①用于四轴飞行器控制系统的设计与验证,支持算法快速原型开发;②作为教学工具帮助理解非线性动力学系统建模与仿真过程;③支撑科研项目中对飞行器姿态控制、轨迹跟踪等问题的深入研究; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注动力学建模与控制模块的实现细节,同时可延伸学习文档中提及的PID控制、状态估计等相关技术内容,以全面提升系统仿真与分析能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值