1. 程式人生 > >華中科技大學計算機歷年考研複試上機題

華中科技大學計算機歷年考研複試上機題

這裡不做詳細說明了,可以練練手,程式碼有的寫的並不一定是最優解,僅供參考

矩陣轉置

/***
題目描述
輸入一個N*N的矩陣,將其轉置後輸出。要求:不得使用任何其他陣列(就地逆置)。
輸入描述:
輸入的第一行包括一個整數N,(1<=N<=100),代表矩陣的維數。
接下來的N行每行有N個整數,分別代表矩陣的元素。


輸出描述:
可能有多組測試資料,對於每組資料,將輸入的矩陣轉置後輸出。

示例1
輸入
3
1 2 3
4 5 6
7 8 9

輸出
1 4 7
2 5 8
3 6 9
***/
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
    int ant[105][105];
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                scanf("%d",&ant[i][j]);
            }
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                printf("%d",ant[j][i]);
                if(j+1 == n) printf("\n");
                else printf(" ");
            }
        }
    }
    return 0;
}


統計單詞

/***
題目描述
編一個程式,讀入使用者輸入的,以“.”結尾的一行文字,統計一共有多少個單詞,並分別輸出每個單詞含有多少個字元。 (凡是以一個或多個空格隔開的部分就為一個單詞)
輸入描述:
輸入包括1行字串,以“.”結束,字串中包含多個單詞,單詞之間以一個或多個空格隔開。


輸出描述:
可能有多組測試資料,對於每組資料,
輸出字串中每個單詞包含的字母的個數。
示例1
輸入

hello how are you.
輸出

5 3 3 3
****/

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
int main()
{
    char str[1000007];
    while(gets(str))
    {
        int len = strlen(str);
        int cnt = 0;
        if (str[len-1]!='.') continue;
        for(int i=0;i<len;i++)
        {
            if (str[i]=='.')
            {
                if (cnt > 0)
                    printf("%d",cnt);
                printf("\n");
                break;
            }
            if (str[i]==' ')
            {
                if (cnt > 0)
                    printf("%d ",cnt);
                cnt=0;
            }
            else cnt++;

        }
    }
    return 0;
}


IP地址

/***
題目描述
輸入一個ip地址串,判斷是否合法。
輸入描述:
輸入的第一行包括一個整數n(1<=n<=500),代表下面會出現的IP地址的個數。
接下來的n行每行有一個IP地址,IP地址的形式為a.b.c.d,其中a、b、c、d都是整數。


輸出描述:
可能有多組測試資料,對於每組資料,如果IP地址合法則輸出"Yes!”,否則輸出"No!”。

合法的IP地址為:
a、b、c、d都是0-255的整數。
示例1
輸入

2
255.255.255.255
512.12.2.3
輸出

Yes!
No!
***/

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        char str[25];
        scanf("%s",str);
        //cout<<"str = "<<str<<endl;
        int len = strlen(str);
        //cout<<"len = "<<len<<endl;
        int ans = 0;
        bool bo = true;
        for(int i=0;i<len;i++)
        {
            if(str[i]=='.')
            {
                //cout<<ans<<endl;
                if(ans<0 or ans>255){bo = false; break;}
                ans = 0;
            }
            else if(str[i] >= '0' and str[i] <= '9')
            {
                ans = ans * 10 + str[i] - '0';
            }
        }
        if(bo and (ans<0 or ans>255)) bo = false;
        if (bo) printf("Yes!\n");
        else printf("No!\n");
    }
}

/*
#include <cstdio>

int main()
{
    int ip[4];
    int n;
    while (scanf("%d", &n) != EOF)
    {
        for (int i = 0; i < n; i++)
        {
            scanf("%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]);
            bool flag = true;
            for (int j = 0; j < 4; j++)
            {
                if (ip[j] < 0 || ip[j] > 255)
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
                printf("Yes!\n");
            else
                printf("No!\n");
        }
    }
    return 0;
}
*/


二叉排序樹

/**
題目描述
二叉排序樹,也稱為二叉查詢樹。可以是一顆空樹,也可以是一顆具有如下特性的非空二叉樹:
    1. 若左子樹非空,則左子樹上所有節點關鍵字值均不大於根節點的關鍵字值;
    2. 若右子樹非空,則右子樹上所有節點關鍵字值均不小於根節點的關鍵字值;
    3. 左、右子樹本身也是一顆二叉排序樹。
    現在給你N個關鍵字值各不相同的節點,要求你按順序插入一個初始為空樹的二叉排序樹中,
    每次插入後成功後,求相應的父親節點的關鍵字值,如果沒有父親節點,則輸出-1。

輸入描述:
輸入包含多組測試資料,每組測試資料兩行。
第一行,一個數字N(N<=100),表示待插入的節點數。
第二行,N個互不相同的正整數,表示要順序插入節點的關鍵字值,這些值不超過10^8。


輸出描述:
輸出共N行,每次插入節點後,該節點對應的父親節點的關鍵字值。
示例1
輸入

5
2 5 1 3 4
輸出

-1
2
2
5
3
**/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
using namespace std;

struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
        val(x), left(NULL), right(NULL) {
    }
};
void createTree(TreeNode* &node,int num,int parent)
{
    if(node == NULL)
    {
        printf("%d\n",parent);
        node = new TreeNode(num);
    }
    else
    {
        if(num > node->val)
        {
            createTree(node->left,num,node->val);
        }
        else
        {
            createTree(node->right,num,node->val);
        }
    }
}
void destory(TreeNode* &node)
{
    if(node)
    {
        destory(node->left);
        destory(node->right);
        delete node;
    }

}
int main()
{
    int n,num;
    while(scanf("%d",&n)!=EOF)
    {
        TreeNode *root = NULL;//初始化(否則會變成懸垂指標)
        for(int i=0;i<n;i++)
        {
            scanf("%d",&num);
            //cout<<num<<endl;
            createTree(root,num,-1);
        }
        destory(root);
    }
    return 0;
}


字串連線

/**
題目描述
不借用任何字串庫函式實現無冗餘地接受兩個字串,然後把它們無冗餘的連線起來。
輸入描述:
每一行包括兩個字串,長度不超過100。


輸出描述:
可能有多組測試資料,對於每組資料,
不借用任何字串庫函式實現無冗餘地接受兩個字串,然後把它們無冗餘的連線起來。
輸出連線後的字串。
示例1
輸入

abc def
輸出

abcdef
**/

//無冗餘...沒理解
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
int main()
{
    string str1,str2;
    while(cin>>str1>>str2)
        cout<<str1<<str2<<endl;
    return 0;
}

a+b

題目描述

實現一個加法器,使其能夠輸出a+b的值。

輸入描述:

輸入包括兩個數a和b,其中a和b的位數不超過1000位。

輸出描述:

可能有多組測試資料,對於每組資料,
輸出a+b的值。
示例1

輸入

2 6
10000000000000000000 10000000000000000000000000000000

輸出

8
10000000000010000000000000000000

寫了一個大數模板,大數加法,大數減法,大數乘法和大數除法,這裡是寫好後代入的。雖然看起來有點麻煩,但是模板是可以重複利用的。哈~完善之後再發詳細的。

#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
#define System 1000000    //

class BigNumber{
public:
    string x,y;
    int op; //運算 1:+; -1:-; 2:*; -2:/
    int posx,posy;
    int oop;//符號位 1: + ; -1: - ;
    int opx,opy;// 先取x,y的符號
    int dim; //進位

    BigNumber(string xx,string yy):
        x(xx), y(yy), op(1), posx(0),posy(0),oop(1),opx(1),opy(1),dim(0) {}
    BigNumber(string xx,string yy,int ope):
        x(xx), y(yy), op(ope), posx(0),posy(0),oop(1),opx(1),opy(1),dim(0) {}
    vector<int> res;
    vector<int> remainder;//餘數
    string subX,subY;
    bool SwapXY()
    {
        if(x.size()>0 and y.size()>0)//x,y 非空
        {
            //cout<<"S: operate: ans = "<<x<<" ("<<op<<") "<<y<<" ="<<endl;
            if(x[0]=='-') {opx = -1; posx = 1;}
            if(y[0]=='-') {opy = -1; posy = 1;if(op == 1 || op == -1){ op = -1*op; opy = -1*opy;} }//2 - (-16) == 2 + (+16) || 2 + (-16) == 2 - (+16)

            subX = x.substr(posx);
            subY = y.substr(posy);
            int lenX = x.length()-posx;
            int lenY = y.length()-posy;
            //cout<<"F: subX="<<subX<<" ,lenX="<<lenX<<" ; subY="<<subY<<" ,lenY="<<lenY<<endl;
            //cout<<"F: operate: ans = "<<"("<<opx<<")*"<<subX<<" ("<<op<<") "<<"("<<opy<<")*"<<subY<<" ="<<endl;
            if (op == 1) // +
            {// -x + y || x + y
                if(lenX < lenY || (lenX == lenY && subX < subY))  //  |x| < |y|
                {
                    oop = 1;
                    if (opx < 0) op = -1; //-x + y = y - x
                    subX.swap(subY);
                }
                else if(lenX == lenY && subX == subY)  //  |x| == |y|
                {
                    oop = 1;
                    if (opx < 0) // y - x = 0
                    {
                        res.push_back(0);
                        return false;
                    }
                }
                else    //|x| > |y|
                {
                    if ( opx < 0 ) // -x + y = -(x-y)
                    {
                        oop = -1;
                        op = -1;
                    }
                }

            }
            else if(op == -1) // -
            {// -x - y || x - y

                if(lenX < lenY || (lenX == lenY && subX < subY))  //  |x| < |y|
                {
                    oop = -1;   // x - y = - (y - x) || -x - y = -(y+x)
                    if (opx < 0) op = 1;
                    subX.swap(subY);
                }
                else if(lenX == lenY && subX == subY)  //  |x| == |-y|
                {
                    if (opx > 0)
                    {
                        oop = 1;
                        res.push_back(0);   // x - y = 0
                        return false;
                    }
                    else    // -x -y = -(x+y) ; -5 - 5 = -(5+5) = -10
                    {
                        oop = -1;
                        op = 1;
                    }

                }
                else    //|x| > |y|
                {
                    if(opx < 0) // -x -y = -(x+y) ; -10 - 5 = -(10+5) = -15
                    {
                        oop = -1;
                        op = 1;
                    }

                }
            }
            else if(op == 2 || op == -2)
            {
                if(opx * opy < 0) { oop = -1;} //一正一負
                else { oop = 1;}//兩正數,兩負數

                if(op == -2 && lenX == lenY && subX == subY)  //  |x| == |-y|
                {
                    res.push_back(1);   // x - y = 0
                    return false;
                }
            }
            //cout<<"L: subX="<<subX<<" , subY="<<subY<<" ; X="<<x<<" , Y="<<y<<endl;

            return true;
        }
        else return false;
    }
    void operate()
    {
        if (SwapXY())//|x|>|y|
        {
            //計算...
            //cout<<"operate: ans = ("<<oop<<") ("<<subX<<" ("<<op<<") "<<subY<<") ="<<endl;
            if(op == 1) Plus(subX,subY);
            else if(op == -1) Subtract(subX,subY);
            else if(op == 2 ) Multiply(subX,subY);
            else if(op == -2) Divide(subX,subY);// -5/15= 1/3
        }


    }
    void Plus(string X,string Y)  // +
    {
        //cout<<"Plus: "<<endl;
        string::reverse_iterator ritX = X.rbegin();
        string::reverse_iterator ritY = Y.rbegin();
        for(; ritY < Y.rend(); ritY++,ritX++ )
        {
            int num = *ritX - '0' + *ritY - '0' + dim;
            //cout<<num<<endl;
            dim = num / 10;
            res.push_back(num % 10);
        }
        for(;ritX < X.rend(); ritX++)
        {
            int num = *ritX - '0' + dim;
            dim = num / 10;
            res.push_back(num % 10);
        }
        if(dim) res.push_back(dim);
        while (res.back() == 0)//刪前導0
        {
            res.pop_back();
        }
    }
    void Subtract(string X,string Y) // -
    {
        //cout<<"Subtract: "<<endl;
        string::reverse_iterator ritX = X.rbegin();
        string::reverse_iterator ritY = Y.rbegin();
        for(; ritY < Y.rend(); ritY++,ritX++ )
        {
            int num = (*ritX - '0' - dim ) - (*ritY - '0') ;
            //cout<<num<<endl;
            if(num < 0) {num = (num + 10 ) % 10; dim = 1;}//借位
            else dim = 0;//復位
            res.push_back(num);
        }
        for(;ritX < X.rend(); ritX++)
        {
            int num = (*ritX - '0' - dim ) ;
            //cout<<num<<endl;
            if(num < 0) {num = (num + 10 ) % 10; dim = 1;}//借位
            else dim = 0;//復位
            res.push_back(num);
        }
        if(dim){res.clear();} //最後還有dim 則 X<Y 需要清表
        while (res.back() == 0)//刪前導0
        {
            res.pop_back();
        }

    }
    /**思路:乘法利用加法原則,1.積的位數 = X位數+Y位數 **/
    void Multiply(string X,string Y)
    {
        //cout<<"Multiply: "<<endl;
        string::reverse_iterator ritX = X.rbegin();
        string::reverse_iterator ritY = Y.rbegin();
        //vector<int> arr(X.size()+Y.size()) ;// 位數K = x + y;
        res.resize(X.size()+Y.size());  //重劃定大小
        vector<int>::iterator it;

        for(int i = 0;i < X.size();i++)
        {
            for(int j = 0;j < Y.size();j++)
            {
                res[i+j] += (*(ritX+i) - '0') * (*(ritY+j) - '0');//從左到右 相乘 相加
            }
        }
        for(it = res.begin();it < res.end();++it)
        {
            *it = (*it) + dim;
            dim = (*it) / 10;
            *it = (*it) % 10;
        }
        /*
        int p = 0;
        for(it = arr.begin();it<arr.end();++it)
        {
            cout<<p<<" : "<<*it<<"\t"<<arr[p]<<endl;
            p++;
        }
        */
        if(dim){res.clear();} //最後還有dim 需要清表
        while (res.back() == 0)//刪前導0
        {
            res.pop_back();
        }
    }
    //大數除法與求餘 (整除)  -- 輾轉相除法  X / Y
    void Divide(string X,string Y)//輸出是正序輸出...
    {
        int lenX = X.length();
        int lenY = Y.length();
        string::reverse_iterator ritX = X.rbegin();
        string::reverse_iterator ritY = Y.rbegin();
        if(lenX < lenY || (lenX == lenY && X < Y))  //餘數 :Y
        {
            res.push_back(0);
            for(;ritY < Y.rend();ritY++) remainder.push_back((*ritY - '0'));
        }
        else if(lenX == lenY && X == Y)
        {
            res.push_back(1);
            remainder.push_back(0);
        }
        else    // 987654321 / 15999
        {
            string sub_X = X.substr(0,lenY);
            //cout<<"First: sub_X = "<<sub_X<<" ,Y = "<<Y<<endl;
            string ans("");
            if(sub_X > Y)
            {
                solveXY(sub_X,Y,0,ans);
            }
            else if(sub_X == Y)
            {
                res.push_back(1);
                ans.clear();
            }
            else    //sub_X < Y
            {
                ans = sub_X;
            }
            //cout<<"Second: sub_X = "<<sub_X<<" ,Y = "<<Y<<", ans = "<<ans<<endl;
            for(int i = lenY;i < lenX;i++)
            {
                if(X[i]-'0' > 0)
                    ans.push_back(X[i]);
                int len = ans.length();
                if(len < lenY) { res.push_back(0); continue; }
                else
                {
                    if(len == lenY)
                    {
                        if(ans == Y)//151515600 / 15 = 10101040
                        {
                            res.push_back(1);
                            ans.clear();
                        }
                        else if(ans < Y)
                        {
                            res.push_back(0);
                            continue;
                        }
                        else //ans > Y
                        {
                            sub_X = ans;
                            ans.clear();
                            solveXY(sub_X,Y,0,ans);
                        }
                    }
                    else //len > lenY
                    {
                        sub_X = ans;
                        ans.clear();
                        solveXY(sub_X,Y,0,ans);
                    }
                }
            }
            if(ans == "")
                remainder.push_back(0);
            else //餘數
            {
                for(int i = 0;i<ans.length();i++)
                {
                    if(ans[i] - '0' == 0) continue;
                    remainder.push_back(ans[i] - '0');
                }
            }

        }
    }
    //輾轉相除法
    void solveXY(string X,string &Y,int d,string &ans) //X > Y
    {
        //cout<<"Divide_Subtract: "<<X<<" / "<<Y<<" ; d = "<<d<<", ans = "<<ans<<endl;
        string::reverse_iterator ritX = X.rbegin();
        string::reverse_iterator ritY = Y.rbegin();
        string rem("");
        int dimX = 0;
        if(dimX){rem.clear();} //最後還有dim 則 X<Y 需要清表
        for(; ritY < Y.rend(); ritY++,ritX++ )
        {
            int num = (*ritX - '0' - dimX ) - (*ritY - '0') ;
            //cout<<num<<endl;
            if(num < 0) {num = (num + 10 ) % 10; dimX = 1;}//借位
            else dimX = 0;//復位
            char ch = num + '0';
            rem.push_back(ch);
        }
        for(;ritX < X.rend(); ritX++)
        {
            int num = (*ritX - '0' - dimX ) ;
            //cout<<num<<endl;
            if(num < 0) {num = (num + 10 ) % 10; dimX = 1;}//借位
            else dimX = 0;//復位
            char ch = num + '0';
            rem.push_back(ch);
        }
        // 刪前導0
        //string::reverse_iterator ritM = rem.rbegin();
        int dy = 0;
        int sm = rem.length();
        for(int i=sm-1;i >= 0;i--)
        {
            if(rem[i]-'0' == 0) { dy++; continue; }
            else break;
        }
        rem = rem.substr(0,sm-dy);

        //cout<<"處理餘數 :";
        d++;
        for(int i = rem.length()-1;i >= 0;i--)
        {
            //cout<<rem[i];
            ans.push_back(rem[i]);
        }
        //cout<<endl;
        int len = ans.length();
        int lenY = Y.length();
        if(len < lenY || (len == lenY && ans < Y))// 下一次減數小於被減數
        {
            //商存起來
            res.push_back(d);
            //
            //cout<<"餘數要返回 :"<<ans<<endl;
            return ;
        }
        else if(len == lenY && ans == Y) // 下一次減數和被減數相等
        {
            d++;
            res.push_back(d);
            ans.clear();  //15999897654 / 15999 = 1000056
            return ;
        }
        else//// 下一次減數大於被減數,做下一次減法運算
        {
            X = ans;
            ans.clear();
            solveXY(X,Y,d,ans);
        }
    }
};

int main()
{
    string str1,str2;
    while(cin>>str1>>str2)
    {
        BigNumber bn(str1,str2);
        //cout<<bn.x<<endl<<bn.y<<endl<<bn.op<<endl;
        bn.operate();
        //cout<<"-------------"<<endl<<bn.x<<endl<<bn.y<<endl<<bn.op<<endl;

        //cout<<"result: = ";
        if(bn.oop<0) cout<<"-";

        for (vector<int>::reverse_iterator it = bn.res.rbegin() ; it != bn.res.rend(); ++it)
            cout<< *it;
        cout<<endl;

    }
    return 0;
}

排序

/**
題目描述
    對輸入的n個數進行排序並輸出。
輸入描述:
    輸入的第一行包括一個整數n(1<=n<=100)。
    接下來的一行包括n個整數。


輸出描述:
    可能有多組測試資料,對於每組資料,將排序後的n個整數輸出,每個數後面都有一個空格。
    每組測試資料的結果佔一行。
示例1
輸入

4
1 4 3 2
輸出

1 2 3 4
**/
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include <algorithm>
using namespace std;
#define MAXN 105

int main()
{
    int N;
    while(scanf("%d",&N) != EOF)
    {
        vector<int> arr;
        for(int i=0;i<N;i++)
        {
            int num;
            scanf("%d",&num);
            arr.push_back(num);
        }
        vector<int>::iterator iter;
        sort(arr.begin(),arr.end());
        for(iter = arr.begin();iter < arr.end();iter++)
        {
            cout<<*iter<<" ";
        }
        cout<<endl;
    }
    return 0;
}


有空再補一個快排手寫的。

特殊排序

/**
題目描述
輸入一系列整數,將其中最大的數挑出,並將剩下的數進行排序。
輸入描述:
輸入第一行包括1個整數N,1<=N<=1000,代表輸入資料的個數。
接下來的一行有N個整數。


輸出描述:
可能有多組測試資料,對於每組資料,
第一行輸出一個整數,代表N個整數中的最大值,並將此值從陣列中去除,將剩下的數進行排序。
第二行將排序的結果輸出。
示例1
輸入

4
1 3 4 2
輸出

4
1 2 3
**/

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include <algorithm>
using namespace std;
#define MAXN 105

int main()
{
    int N;
    while(scanf("%d",&N) != EOF)
    {
        vector<int> arr;
        for(int i=0;i<N;i++)
        {
            int num;
            scanf("%d",&num);
            arr.push_back(num);
        }
        if(N == 1)
        {
            cout << arr[0] << endl << -1 << endl;
            continue;
        }
        vector<int>::iterator iter = arr.end();
        sort(arr.begin(),arr.end());
        cout<<*(iter-1)<<endl;
        for(iter = arr.begin();iter < arr.end()-2;iter++)
        {
            cout<<*iter<<" ";
        }
        cout<<*iter<<endl;

    }
    return 0;
}


二叉樹遍歷

/***
題目描述
二叉樹的前序、中序、後序遍歷的定義:
 前序遍歷:對任一子樹,先訪問跟,然後遍歷其左子樹,最後遍歷其右子樹;
 中序遍歷:對任一子樹,先遍歷其左子樹,然後訪問根,最後遍歷其右子樹;
 後序遍歷:對任一子樹,先遍歷其左子樹,然後遍歷其右子樹,最後訪問根。

給定一棵二叉樹的前序遍歷和中序遍歷,求其後序遍歷(提示:給定前序遍歷與中序遍歷能夠唯一確定後序遍歷)。
輸入描述:
兩個字串,其長度n均小於等於26。
第一行為前序遍歷,第二行為中序遍歷。
二叉樹中的結點名稱以大寫字母表示:A,B,C....最多26個結點。


輸出描述:
輸入樣例可能有多組,對於每組測試樣例,
輸出一行,為後序遍歷的字串。
示例1
輸入

ABC
BAC
FDXEAG
XDEFAG
輸出

BCA
XEDGAF
**/

// http://blog.csdn.net/u010579068/article/details/48416491

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

struct TreeNode{
    TreeNode *left;
    TreeNode *right;
    char val;
    TreeNode(char x):
        val(x), left(NULL), right(NULL) {
    }
}*root;
string preTree,midTree;
TreeNode *Create(string::iterator pTree, string::iterator mTree,int len)
{
    TreeNode *node;
    for(int i=0;i<len;i++)
    {                                                                       //前 FDXEAG
        if(*(mTree+i) == *pTree) // 定根                             中 XDEFAG
        {
            node = new TreeNode(*pTree);                                //根    F
            node->left = Create(pTree+1,mTree,i);                     //左 XDE
            node->right = Create(pTree+i+1,mTree+i+1,len-i-1);         //右     AG
            return node;
        }
    }
    return NULL; //退出條件
}

void PrintTree(TreeNode *head)
{
    if(head == NULL) return ;
    PrintTree(head->left);
    PrintTree(head->right);

    if(head == root)//最後一個
        cout<<head->val<<endl;
    else cout<<head->val;
}
void destory(TreeNode* &node)
{
    if(node)
    {
        destory(node->left);
        destory(node->right);
        delete(node);
    }

}
int main()
{

    while(cin>>preTree>>midTree)
    {
        //前序定根,中序定左右
        if(preTree.size() == midTree.size())
        {
            root = Create(preTree.begin(),midTree.begin(),midTree.size());
            PrintTree(root);
            destory(root);
        }
    }
    return 0;
}


奇偶校驗

/***
題目描述
輸入一個字串,然後對每個字元進行奇校驗,最後輸出校驗後的二進位制數(如'3',輸出:10110011)。
輸入描述:
輸入包括一個字串,字串長度不超過100。


輸出描述:
可能有多組測試資料,對於每組資料,
對於字串中的每一個字元,輸出按題目進行奇偶校驗後的數,每個字元校驗的結果佔一行。
示例1
輸入

3
3a
輸出

10110011
10110011
01100001
**/
/**思想就是:轉為2進位制後1的個數為偶數最高位要補1,注意ascii碼最大就是127*/
#include<cstdio>
#include<string>
#include<cstring>
#include<bitset>
#include<iostream>
using namespace std;
int main()
{
    char c;
    bitset<8> bit;
    while(cin>>c)
    {
        if(c == ' ' || c == '\n') continue;
        bit = c;//轉ASCII碼,
        //printf("%d\n",c);
        if((bit.count()&1) == 0) bit.set(7);//偶數位,最高位變1
        cout<<bit<<endl;
    }
    return 0;
}


最大的兩個數

/**
題目描述
    輸入一個四行五列的矩陣,找出每列最大的兩個數。
輸入描述:
    輸入第一行包括一個整數n(1<=n<=1000),接下來有n個四行每行包括五個整數。代表一個四行五列的矩陣,矩陣元素全部是整數。


輸出描述:
    可能有多組測試資料,對於每組資料,按照樣例輸出的格式將每列最大的兩個數輸出,如果最大的兩個數中的一個數在這一列中有多個相同的值,則行值取行值小的那一個。
    輸出時要保留原矩陣的行列順序,即在原矩陣中行值小的,在輸出矩陣中的行值依然小。
示例1
輸入

2
1  2   4  9  8
-1  4  9  8  8
12  9  8  7  0
7   8  9  7  0
1  8   4  9  8
-1  4  9  8  8
12  9  8  7  0
7   8  9  7  0
輸出

12 9 9 9 8
7 8 9 8 8
12 8 9 9 8
7 9 9 8 8
**/
/**解:最關鍵是每列最大兩個數的行順序不變,還有個坑最後換行前有空格**/
#include<cstdio>
#include<stack>
#include<iostream>
using namespace std;
#define row 4
#define column 5
void solve(int &x1,int &x2,int x)
{
    if(x1 > x2)
    {
        if(x >= x1) x2 = x;
        else if(x > x2 && x < x1) x2 = x;
    }
    else if(x1 == x2)
    {
        if(x > x1) x2 = x;
    }
    else    // x1 < x2
    {
        if(x >= x2) {x1 = x2; x2 = x;}
        else if(x > x1 && x < x2) {x1 = x2; x2 = x;}
    }
}
int main()
{
    int n;
    while(cin>>n)
    {
        for(int t=0;t<n;t++)
        {
            int ans[row][column];
            int res[2][column];
            stack<int> S;
            for(int i=0;i<row;i++)
            {
                for(int j=0;j<column;j++)
                {
                    cin>>ans[i][j];
                    if(i<2) res[i][j] = ans[i][j];
                }
            }
            for(int j=0;j<column;j++)
            {
                int r1 = res[0][j];
                int r2 = res[1][j];
                for(int i=2;i<row;i++)
                {
                    solve(r1,r2,ans[i][j]);
                }
                res[0][j] = r1;
                res[1][j] = r2;
            }
            for(int i=0;i<2;i++)
            {
                int j=0;
                for(;j<column;j++)
                {
                    cout<<res[i][j]<<" ";
                }
                cout<<endl;
            }
        }
    }
    return 0;
}


成績排序

/***
題目描述
有N個學生的資料,將學生資料按成績高低排序,如果成績相同則按姓名字元的字母序排序,
如果姓名的字母序也相同則按照學生的年齡排序,並輸出N個學生排序後的資訊。
輸入描述:
測試資料有多組,每組輸入第一行有一個整數N(N<=1000),接下來的N行包括N個學生的資料。
每個學生的資料包括姓名(長度不超過100的字串)、年齡(整形數)、成績(小於等於100的正數)。


輸出描述:
將學生資訊按成績進行排序,成績相同的則按姓名的字母序進行排序。
然後輸出學生資訊,按照如下格式:
姓名 年齡 成績

學生姓名的字母序區分字母的大小寫,如A要比a的字母序靠前(因為A的ASC碼比a的ASC碼要小)。
示例1
輸入

3
abc 20 99
bcd 19 97
bed 20 97
輸出

bcd 19 97
bed 20 97
abc 20 99
**/
/**解:這裡沒有明確最後按年齡的排序。預設從小到大 **/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct Info{
    string name;
    int age;
    int grade;
    Info(string N,int x,int y):
        name(N), age(x),grade(y) {
    }
};
bool comp (Info A,Info B)
{
    if(A.grade == B.grade)
    {
        if(A.name == B.name)
        {
            return A.age < B.age;
        }
        return A.name < B.name;
    }
    return A.grade < B.grade;
}


int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        vector<Info> arr;
        string str;
        int a,g;
        for(int i=0;i<n;i++)
        {
            cin>>str>>a>>g;
            Info in(str,a,g);
            arr.push_back(in);
        }
        sort(arr.begin(),arr.end(),comp);
        for(vector<Info>::iterator iter = arr.begin();iter<arr.end();iter++)
        {
            cout<<(*iter).name<<" "<<(*iter).age<<" "<<(*iter).grade<<endl;
        }
    }
    return 0;
}


遍歷連結串列

/**
題目描述
建立一個升序連結串列並遍歷輸出。
輸入描述:
輸入的每個案例中第一行包括1個整數:n(1<=n<=1000),接下來的一行包括n個整數。


輸出描述:
可能有多組測試資料,對於每組資料,
將n個整數建立升序連結串列,之後遍歷連結串列並輸出。
示例1
輸入

4
3 5 7 9
輸出

3 5 7 9
**/
//感覺這題是要考連結串列的排序嗎?O((n+1)n/2) = O(n^2)

#include<cstdio>
#include<cstring>
#include<iostream>
#include <climits>
using namespace std;

struct LNode{
    LNode *next;
    int data;
    LNode(int x):
        data(x), next(NULL){
    }
};
void Insert_LNode(LNode * &head,int num)
{
    LNode *pre = head;
    LNode *p = pre->next;
    LNode *L = new LNode(num);
    //if(p == NULL){ pre->next = L; return ;}
    while(p)
    {

        if(p->data < num)
        {
            pre = pre -> next;
            p = p -> next;
        }
        else break;
    }
    //cout<<p->data<<endl;
    pre -> next = L;
    pre -> next -> next = p;
}
void Print_LNode(LNode * head)
{
    while(head)
    {
        if(head->next == NULL) cout<<head->data<<endl;
        else cout<<head->data<<" ";
        head = head -> next;
    }
}
void Clear_LNode(LNode* &node)
{
    if(node)
    {
        Clear_LNode(node->next);
        delete(node);
    }

}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        LNode *root = new LNode(INT_MIN);   //#include <climits>
        int num;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&num);
            Insert_LNode(root,num);
        }
        Print_LNode(root->next);
        Clear_LNode(root);
    }
    return 0;
}


守形數

/**
題目描述
守形數是這樣一種整數,它的平方的低位部分等於它本身。
比如25的平方是625,低位部分是25,因此25是一個守形數。 編一個程式,判斷N是否為守形數。
輸入描述:
輸入包括1個整數N,2<=N<100。
輸出描述:
可能有多組測試資料,對於每組資料,
輸出"Yes!”表示N是守形數。
輸出"No!”表示N不是守形數。
示例1
輸入

25
4
輸出

Yes!
No!
**/

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;

int main()
{
    int N;
    while(scanf("%d",&N)!=EOF)
    {
        if(N*N%100 == N || N*N%10 == N) printf("Yes!\n");  //其實只有5,6,25和76
        else printf("No!\n");
    }
    return 0;
}


矩陣最大值

/**
題目描述
編寫一個程式輸入一個mXn的矩陣儲存並輸出,並且求出每行的最大值和每行的總和。
要求把每行總和放入每行最大值的位置,如果有多個最大值,取下標值最小的那一個作為最大值。 最後將結果矩陣輸出。
輸入描述:
輸入的第一行包括兩個整數m和n(1<=m,n<=100),分別代表矩陣的行和列的維數。
接下來的m行每行有n個數,代表矩陣的元素。
輸出描述:
可能有多組測試資料,對於每組資料,輸出按題目要求執行後的矩陣。
示例1
輸入

3 3
1 1 1
1 1 1
1 1 1
3 3
3 2 3
2 3 2
3 2 3
輸出

3 1 1
3 1 1
3 1 1
8 2 3
2 7 2
8 2 3
**/

#include<cstdio>
#include<vector>
#include<climits>
#include<iostream>
using namespace std;
#define MAXN 105

int main()
{
    int arr[MAXN][MAXN];
    int m,n;
    while(scanf("%d%d",&m,&n)!=EOF)
    {

        for(int i=0;i<m;i++)
        {
            int pos = 0, sum = 0;;
            for(int j=0;j<n;j++)
            {
                scanf("%d",&arr[i][j]);
                sum += arr[i][j];
                if(arr[i][pos] < arr[i][j]) {pos = j;}
            }
            arr[i][pos] = sum;
        }

        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n-1;j++)
            {
                printf("%d ",arr[i][j]);
            }
            printf("%d\n",arr[i][n-1]);
        }
    }
    return 0;
}


最小年齡的3個職工

/**
題目描述
職工有職工號,姓名,年齡.輸入n個職工的資訊,找出3個年齡最小的職工打印出來。
輸入描述:
輸入第一行包括1個整數N,1<=N<=30,代表輸入資料的個數。
接下來的N行有N個職工的資訊:
包括職工號(整數), 姓名(字串,長度不超過10), 年齡(1<=age<=100)。
輸出描述:
可能有多組測試資料,對於每組資料,
輸出結果行數為N和3的較小值,分別為年齡最小的職工的資訊。
關鍵字順序:年齡>工號>姓名,從小到大。
示例1
輸入

5
501 Jack 6
102 Nathon 100
599 Lily 79
923 Lucy 15
814 Mickle 65
輸出

501 Jack 6
923 Lucy 15
814 Mickle 65
**/

#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
struct StaffInfo{
    int staffID;
    string name;
    int age;
    StaffInfo(int id,string n,int old):
        staffID(id), name(n), age(old) {
    }
};
bool comp (StaffInfo A,StaffInfo B)
{
    if(A.age == B.age)
    {
        if(A.staffID == B.staffID)
        {
            return A.name < B.name;
        }
        return A.staffID < B.staffID;
    }
    return A.age < B.age;
}
int main()
{
    int N;
    while(scanf("%d",&N)!=EOF)
    {
        int age,id;
        string name;
        vector<StaffInfo> arr;
        for(int i=0;i<N;i++)
        {
            cin>>id>>name>>age;
            StaffInfo info(id,name,age);
            arr.push_back(info);
        }
        sort(arr.begin(),arr.end(),comp);
        for(int i=0;i<N && i<3;i++)
        {
            printf("%d %s %d\n",arr[i].staffID,arr[i].name.c_str(),arr[i].age);
        }
    }
    return 0;
}


對稱矩陣

/**
題目描述
輸入一個N維矩陣,判斷是否對稱。
輸入描述:
輸入第一行包括一個數:N(1<=N<=100),表示矩陣的維數。
接下來的N行,每行包括N個數,表示N*N矩陣的元素。
輸出描述:
可能有多組測試資料,對於每組資料,
輸出"Yes!”表示矩陣為對稱矩陣。
輸出"No!”表示矩陣不是對稱矩陣。
示例1
輸入

4
16 19 16 6
19 16 14 5
16 14 16 3
6 5 3 16
2
1 2
3 4
輸出

Yes!
No!
**/
//對稱矩陣,貌似只要考慮 arr[i][j] == ar[j][i],有時候不能想太多啊....
#include<cstdio>
#include<iostream>
using namespace std;

int main()
{
    int N; //1<=N<=100
    while(scanf("%d",&N)!=EOF)
    {
        int arr[N][N];
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++)
            {
                scanf("%d",&arr[i][j]);
            }
        }
        bool bo = true;
        for(int i=0;i<N;i++)
        {
            if (bo)
            {
                for(int j=i;j<N;j++)
                {
                    if(arr[i][j] != arr[j][i]) {bo = false; break;}
                }
            }
            else break;

        }
        if(bo) printf("Yes!\n");
        else printf("No!\n");
    }
    return 0;
}


A+B

/****
給定兩個整數A和B,其表示形式是:從個位開始,每三位數用逗號","隔開。 現在請計算A+B的結果,並以正常形式輸出。
輸入描述:
輸入包含多組資料資料,每組資料佔一行,由兩個整數A和B組成(-10^9 < A,B < 10^9)。


輸出描述:
請計算A+B的結果,並以正常形式輸出,每組資料佔一行。

輸入例子:
-234,567,890 123,456,789
1,234 2,345,678

輸出例子:
-111111101
2346912
*******/

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<math.h>
using namespace std;
#define LL long long
LL changeLL(char (&x)[20])
{
    int op = 1; //+
    int len = strlen(x);
    LL xi = 0;
    for(int i=0;i<len;i++)
    {
        if (x[i]=='-') {op = 0;continue;}
        if (x[i]==',') continue;
        xi = xi * 10 + x[i]-'0';
    }
    if(op == 0) xi = xi*(-1);
    return xi;
}
int main()
{
    char a[20]={'\0'},b[20]={'\0'};
    while(scanf("%s%s",&a,&b)!=EOF)
    {
        printf("%lld\n",changeLL(a)+changeLL(b));

    }
    return 0;
}



列印日期

/**
題目描述
給出年分m和一年中的第n天,算出第n天是幾月幾號。
輸入描述:
輸入包括兩個整數y(1<=y<=3000),n(1<=n<=366)。
輸出描述:
可能有多組測試資料,對於每組資料,
按 yyyy-mm-dd的格式將輸入中對應的日期打印出來。
示例1
輸入

2000 3
2000 31
2000 40
2000 60
2000 61
2001 60
輸出

2000-01-03
2000-01-31
2000-02-09
2000-02-29
2000-03-01
2001-03-01
**/

#include<cstdio>
#include<iostream>
using namespace std;

int monthDay[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
    int year,day;
    while(scanf("%d%d",&year,&day)!=EOF)
    {
        if(year%4==0&&year%100!=0 || year%400==0) monthDay[2] = 29;
        else monthDay[2] = 28;
        int month = 1;
        for(;month<13;month++)
        {
            if(day <= monthDay[month]) break;
            day -= monthDay[month];
        }
        printf("%04d-%02d-%02d\n",year,month,day);
    }
    return 0;
}


二叉樹排序

/**
題目描述
輸入一系列整數,建立二叉排序數,並進行前序,中序,後序遍歷。
輸入描述:
輸入第一行包括一個整數n(1<=n<=100)。
接下來的一行包括n個整數。
輸出描述:
可能有多組測試資料,對於每組資料,將題目所給資料建立一個二叉排序樹,並對二叉排序樹進行前序、中序和後序遍歷。
每種遍歷結果輸出一行。每行最後一個數據之後有一個空格。

輸入中可能有重複元素,但是輸出的二叉樹遍歷序列中重複元素不用輸出。
示例1
輸入

5
1 6 5 9 8
輸出

1 6 5 9 8
1 5 6 8 9
5 8 9 6 1
**/

// 題意中有重複元素,但不同輸出說明建樹時插在同個位置

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
struct TreeNode{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int v):
        val(v), left(NULL), right(NULL) {
    }
};

void createTree(TreeNode * &p,int num)
{
    if(p==NULL) {p = new TreeNode(num); return;}
    if(p->val > num) createTree(p->left,num);
    else if(p->val < num) createTree(p->right,num);
    else return ;
}
//前序遍歷 :根左右
void PreorderTraversal(TreeNode *p)
{
    if(p==NULL) return;
    printf("%d ",p->val);
    PreorderTraversal(p->left);
    PreorderTraversal(p->right);
}
//中序遍歷 :左根右
void InorderTraversal(TreeNode *p)
{
    if(p==NULL) return;
    InorderTraversal(p->left);
    printf("%d ",p->val);
    InorderTraversal(p->right);
}
//後序遍歷 :左右根
void PostorderTraversal(TreeNode *p)
{
    if(p==NULL) return;
    PostorderTraversal(p->left);
    PostorderTraversal(p->right);
    printf("%d ",p->val);
}
void destoryTree(TreeNode * &p)
{
    if(p == NULL) return;
    destoryTree(p->left);
    destoryTree(p->right);
    delete p;
}
void PrintTree(TreeNode * &p)
{
    PreorderTraversal(p);   //前序遍歷
    cout<<endl;
    InorderTraversal(p);    //中序遍歷
    cout<<endl;
    PostorderTraversal(p);  //後序遍歷
    cout<<endl;
    destoryTree(p);         //回收
}
int main()
{
    int N; //1<=N<=100
    while(scanf("%d",&N)!=EOF)
    {
        TreeNode *root = NULL;
        int num;
        for(int i=0;i<N;i++)
        {
            scanf("%d",&num);
            createTree(root,num);
        }
        PrintTree(root);
    }
    return 0;
}


大整數排序

/**
題目描述
對N個長度最長可達到1000的數進行排序。
輸入描述:
輸入第一行為一個整數N,(1<=N<=100)。
接下來的N行每行有一個數,數的長度範圍為1<=len<=1000。
每個數都是一個正數,並且保證不包含字首零。
輸出描述:
可能有多組測試資料,對於每組資料,將給出的N個數從小到大進行排序,輸出排序後的結果,每個數佔一行。
示例1
輸入

3
11111111111111111111111111111
2222222222222222222222222222222222
33333333
輸出

33333333
11111111111111111111111111111
2222222222222222222222222222222222
**/
//不用處理負數和前導0...
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 105
bool comp(string a,string b)
{
    if(a.size() == b.size()) return a < b;
    return a.size() < b.size();
}
int main()
{
    int N;
    while(cin>>N)
    {
        string str[MAXN];
        for(int i=0;i<N;i++)
        {
            cin>>str[i];
        }
        sort(str,str+N,comp);
        for(int i=0;i<N;i++)
            cout<<str[i]<<endl;
    }
    return 0;
}


N階樓梯上樓問題

/**
題目描述
N階樓梯上樓問題:一次可以走兩階或一階,問有多少種上樓方式。(要求採用非遞迴)
輸入描述:
輸入包括一個整數N,(1<=N<90)。
輸出描述:
可能有多組測試資料,對於每組資料,
輸出當樓梯階數是N時的上樓方式個數。
示例1
輸入

4
輸出

5
**/

//打表
#include<cstdio>
#include<iostream>
using namespace std;

#define MAXN 90
long long arr[MAXN];
void init()
{
    arr[1] = 1;
    arr[2] = 2;
    for(int i=3;i<MAXN;i++)
        arr[i] = arr[i-1] + arr[i-2];
}
int main()
{
    init();
    int N;
    while(scanf("%d",&N)!=EOF)
    {
        if(arr[N]) cout<<arr[N]<<endl;
    }
    return 0;
}


a+b

/**
題目描述
計算a+b的和



每行包行兩個整數a和b

對於每行輸入對應輸出一行a和b的和

輸入

1 5

輸出

6
**/

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<math.h>
using namespace std;
#define LL long long
LL changeLL(char (&x)[20])
{
    int op = 1; //+
    int len = strlen(x);
    LL xi = 0;
    for(int i=0;i<len;i++)
    {
        if (x[i]=='-') {op = 0;continue;}
        if (x[i]==',') continue;
        xi = xi * 10 + x[i]-'0';
    }
    if(op == 0) xi = xi*(-1);
    return xi;
}
int main()
{
    char a[20]={'\0'},b[20]={'\0'};
    while(scanf("%s%s",&a,&b)!=EOF)
    {
        printf("%lld\n",changeLL(a)+changeLL(b));

    }
    return 0;
}


迴文字串

/***
題目描述
給出一個長度不超過1000的字串,判斷它是不是迴文(順讀,逆讀均相同)的。
輸入描述:
輸入包括一行字串,其長度不超過1000。
輸出描述:
可能有多組測試資料,對於每組資料,如果是迴文字串則輸出"Yes!”,否則輸出"No!"。
示例1
輸入

hellolleh
helloworld
輸出

Yes!
No!
**/
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;

int main()
{
    string str;
    while(cin>>str)
    {
        int str_size = str.size();
        if(str_size == 0) continue;
        if(str_size == 1) printf("Yes!\n");
        bool bo = true;
        for(int i=0,j=str_size-1;i<j;i++,j--)
        {
            if (str[i] != str[j]) {bo = false; break;}
        }
        if(bo) printf("Yes!\n");
        else printf("No!\n");
    }
    return 0;
}


找位置

/**
題目描述
對給定的一個字串,找出有重複的字元,並給出其位置,
如:abcaaAB12ab12
輸出:a,1;a,4;a,5;a,10,b,2;b,11,1,8;1,12, 2,9;2,13。
輸入描述:
輸入包括一個由字母和數字組成的字串,其長度不超過100。
輸出描述:
可能有多組測試資料,對於每組資料,
按照樣例輸出的格式將字元出現的位置標出。

1、下標從0開始。
2、相同的字母在一行表示出其出現過的位置。
示例1
輸入

abcaaAB12ab12
輸出

a:0,a:3,a:4,a:9
b:1,b:10
1:7,1:11
2:8,2:12
***/
//hush or 暴力
#include<vector>
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<map>
using namespace std;

int main()
{

    string str;
    while(cin>>str)
    {
        vector< vector<int> > vec;
        vector<char> ch;
        string::iterator its;
        int len = str.size();
        for(int i=0;i<len;i++)
        {
            if(str[i]=='-') continue;
            int rf = str.rfind(str[i]);
            if(rf != i)//存在多個
            {
                //cout<<rf<<"---"<<i<<endl;
                int j=i;
                ch.push_back(str[i]);
                vector<int> tmp;
                while(j<=rf)
                {
                    //cout<<str[j]<<" ~ "<<str[i]<<endl;
                    if(str[j]==str[rf])
                    {
                        tmp.push_back(j);
                        str[j] = '-';
                    }
                    j++;
                }
                vec.push_back(tmp);
            }

        }
        vector<char>::iterator itch;
        vector< vector<int> >::iterator iter;
        for(itch=ch.begin(),iter=vec.begin();itch<ch.end() && iter<vec.end();itch++,iter++)
        {
            vector<int>::iterator in = (*iter).begin();
            cout<<*itch<<":"<<*in;
            for(++in;in<(*iter).end();in++)
            {
                cout<<","<<*itch<<":"<<*in;
            }
            cout<<endl;
        }
    }
    return 0;
}


階乘

/**
題目描述
輸入n, 求y1=1!+3!+...m!(m是小於等於n的最大奇數) y2=2!+4!+...p!(p是小於等於n的最大偶數)。
輸入描述:
每組輸入包括1個整數:n
輸出描述:
可能有多組測試資料,對於每組資料,
輸出題目要求的y1和y2
示例1
輸入

4
輸出

7 26
**/

//題目沒有說N的範圍,簡直了。不過考察的重點應該是那個等式,可以O(n)來做

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int N;  //N<=20
    while(scanf("%d",&N)!=EOF)
    {
        long long y1 = 0,y2 = 0;
        long long factorial = 1;
        for(int i=1;i<=N;i++)
        {
            factorial *= i;
            if(i&1) y1 += factorial;    //奇數
            else { y2 += factorial; }      //偶數
        }
        cout<<y1<<" "<<y2<<endl;
        //printf("%I64d %I64d",y1,y2); // 用%iid why y2 = 0?
    }
    return 0;
}

八進位制
/**
題目描述
輸入一個整數,將其轉換成八進位制數輸出。
輸入描述:
輸入包括一個整數N(0<=N<=100000)。
輸出描述:
可能有多組測試資料,對於每組資料,
輸出N的八進位制表示數。
示例1
輸入

7
8
9
輸出

7
10
11
**/
//題目原來意思可能是要我們模擬進位制轉換吧....
#include<cstdio>
#include<iostream>
using namespace std;

int main()
{
    int N;
    while(scanf("%d",&N)!=EOF)
    {
        printf("%o\n",N);
    }
    return 0;
}

題量還是挺多,考研的題正好練練手~