CODEVS白银组(下)

 有一个二叉树最大宽度和深度的,实在蛋疼无比,无能为力就不放上来了。有些写的时候很复杂,最后懒得优化了,就这样放上来。

四、进制转换
1、十进制转换m进制

#include
#include
#include
using namespace std;

int main()
{
    int dec,scale;
    cin>>dec>>scale;
    int k=0;
    int remain=0;
    string res ="";
    while(dec>=scale)
    {
        remain = dec % scale;
        if(remain<10)
        {
            switch(remain)
            {
                case 0 : res+="0";break; 
                case 1 : res+="1";break;
                case 2 : res+="2";break;
                case 3 : res+="3";break;
                case 4 : res+="4";break;
                case 5 : res+="5";break;
                case 6 : res+="6";break;
                case 7 : res+="7";break;
                case 8 : res+="8";break;
                case 9 : res+="9";break;
            }
            dec = (dec-remain)/scale;
            k++;
        }
        else
        {
            switch( remain )
            {
                case 10 : res += "A"; break;
                case 11 : res += "B"; break;
                case 12 : res += "C"; break;
                case 13 : res += "D"; break;
                case 14 : res += "E"; break;
                case 15 : res += "F"; break;
            }
            dec = (dec-remain)/scale;
            k++;
        }
    }
    remain = dec % scale;
    if(remain < 10)
    {
        switch(remain)
        {
            case 1 : res+="1";break;
            case 2 : res+="2";break;
            case 3 : res+="3";break;
            case 4 : res+="4";break;
            case 5 : res+="5";break;
            case 6 : res+="6";break;
            case 7 : res+="7";break;
            case 8 : res+="8";break;
            case 9 : res+="9";break;
        }
    }
    else
    {
        switch( remain )
        {
            case 10 : res += "A"; break;
            case 11 : res += "B"; break;
            case 12 : res += "C"; break;
            case 13 : res += "D"; break;
            case 14 : res += "E"; break;
            case 15 : res += "F"; break;
        }
    }

    //结果字符串翻转 
    int len = res.length();

    for (int i = 0; i2; i++)
    {
        //前后交换
        char temp = res[i];
        res[i] = res[len-i-1];
        res[len-i-1] = temp;
    }
    //输出交换后的字符串
    cout<return 0;       
} 

2、m进制转换十进制

#include
#include
using namespace std;

int main()
{
    string ori;
    int scale;
    double res=0.0;
    cin>>ori>>scale;
    int len = ori.length();
    for(int i=0;iswitch(ori[i])
        {
            case '0': res += 0*pow((double)scale,(double)len-i-1);break;
            case '1': res += 1*pow((double)scale,(double)len-i-1);break;
            case '2': res += 2*pow((double)scale,(double)len-i-1);break;
            case '3': res += 3*pow((double)scale,(double)len-i-1);break;
            case '4': res += 4*pow((double)scale,(double)len-i-1);break;
            case '5': res += 5*pow((double)scale,(double)len-i-1);break;
            case '6': res += 6*pow((double)scale,(double)len-i-1);break;
            case '7': res += 7*pow((double)scale,(double)len-i-1);break;
            case '8': res += 8*pow((double)scale,(double)len-i-1);break;
            case '9': res += 9*pow((double)scale,(double)len-i-1);break;
            case 'A': res += 10*pow((double)scale,(double)len-i-1);break;
            case 'B': res += 11*pow((double)scale,(double)len-i-1);break;
            case 'C': res += 12*pow((double)scale,(double)len-i-1);break;
            case 'D': res += 13*pow((double)scale,(double)len-i-1);break;
            case 'E': res += 14*pow((double)scale,(double)len-i-1);break;
            case 'F': res += 15*pow((double)scale,(double)len-i-1);break;
        }

    }
    cout<return 0;
} 

五、递推
1、数的计算

#include
using namespace std;

//一个简单的递归,传入res的地址 
void dfs(int n,int &res)
{
    res+=n/2;
    for(int i=1;i<=n/2;i++)
        dfs(i,res);
}

int main()
{
    int m;
    cin>>m;
    int res = 1;
    dfs(m,res);
    cout<return 0;
}

2、Fibonacci数列3

#include
using namespace std;

int Fib(int x)
{
    if(x == 1)
        return 1;
    else if(x == 2)
        return 1;
    else
        return Fib(x-1)+Fib(x-2);
}

int main()
{
    int m;
    cin>>m;
    cout<return 0;
}

六、递归
1、二叉树的最大宽度和高度
【放弃了
2、递归第一次

#include
using namespace std;

int func(int x)
{
    if(x>=0)
        return 5;
    else
        return func(x+1)+func(x+2)+1;
}

int main()
{
    int y;
    cin>>y;
    cout<

3、3n+1问题

#include
using namespace std;

int main()
{
    int n;
    cin>>n;
    for(int i=0;iint k,n=0;
        cin>>k;
        while(k!=1)
        {
            if(k%2 == 1)
                k=3*k+1;
            else if(k%2 == 0)
                k=k/2;
            n++;
        }
        cout<

4、二叉树的序遍历

#include
using namespace std;

int a[100][2];

//前序遍历 
void MLR(int n)
{
    if(n==0)
        return;
    cout<" ";
    MLR(a[n][0]);
    MLR(a[n][1]);
} 
//中序遍历 
void LMR(int n)
{
    if(n==0)
        return;
    LMR(a[n][0]);   
    cout<" ";
    LMR(a[n][1]);
}
//后序遍历 
void LRM(int n)
{
    if(n==0)
        return;
    LRM(a[n][0]);
    LRM(a[n][1]);   
    cout<" ";
}

int main()
{
    int n;
    cin>>n;
    for(int i=0;icin>>a[i+1][0]>>a[i+1][1];
    }
    MLR(1);
    cout<1);
    cout<1);
    cout<return 0;
}

5、汉诺塔问题

#include
using namespace std;

int sum=0;

//计数专用 
int hanoi_1(int n)
{
    if(n==1)
        return ++sum;
     else
     {
        hanoi_1(n-1);
        sum++;
        hanoi_1(n-1);
    }
    return sum;
}

//模拟运行 
void hanoi_2(int n,char a,char b,char c)
{
    if(n==1)
        cout<<1<<" from "<" to "<else
    {
        hanoi_2(n-1,a,c,b);
        cout<" from "<" to "<1,b,a,c);
    }
}

int main()
{
    int n;
    cin>>n;
    sum = hanoi_1(n);
    cout<'A','B','C');
    return 0;
}

你可能感兴趣的:(CODEVS白银组(下))