C++面试题目(持续更新)

     @DavidHan ,
     http://blog.csdn.net/David_Han008/article/details/77850460

 题目1   
给定一个整数,以下列方式进行打印2*n行,如果n=4
则生成的阵列是:
1
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1

#include <iostream>
 #include <stdio.h>
using namespace std;
int main()
{
    int n = 4;
    for (int i = 1; i < n + 1; i++)
    {
        cout << i;
        for (int j = 1; j < i; j++)
        {
            cout << "*"<<i;
        };
        std::cout<<std::endl;
    }

    for (int i = n; i > 0; i--)
    {
        cout << i;
        for (int j = 1; j < i; j++)
        {
            cout << "*"<<i;
        };
        std::cout << std::endl;
    }
    std::cin.get();
}

 题目2  
对于一个字符串,和字符串中的某一位置,请设计一个算法,将包括i位置在内的左侧部分移动到右边,将右侧部分移动到左边。给定字符串A和它的长度n以及特定位置p,请返回旋转后的结果。测试样例

"ABCDEFGH",8,4
输出结果:
返回:"FGHABCDE"

考点:string用来截取字符串

#include<iostream>
 #include <string>
using namespace std;
class StringRotation {
public:
    string rotateString(string A, int n, int p) {
        // write code here
        string LAST(A.substr(0,p+1));
        string FIRST(A.substr(p+1,n+1));
        string SR;
        SR=FIRST+LAST;
        return SR;
    }
};

python语言实现,利用切片

# -*- coding:utf-8 -*-
class StringRotation:
    def rotateString(self, A, n, p):
        FIRST=A[p+1:]
        LAST=A[0:p+1]
        A=FIRST+LAST
        return A

 题目3  

对于一个矩阵,请设计一个算法,将元素按“之”字形打印。具体见样例。
给定一个整数矩阵mat,以及他的维数nxm,请返回一个数组,其中元素依次为打印的数字。
测试样例:

[[1,2,3],[4,5,6],[7,8,9],[10,11,12]],4,3
输出:
返回:[1,2,3,6,5,4,7,8,9,12,11,10]

解题:

class Printer {
public:
    vector<int> printMatrix(vector<vector<int> > mat, int n, int m) {
        // write code here
        vector<int> pmat;//最终输出
        vector<int> smat;//临时存储
        //判断是偶数还是还是奇数
        smat.swap(mat[0]);
        pmat.insert(pmat.end(),smat.begin(),smat.end());
        for(int row=1;row<n;row++)
            {
            if (row%2!=0)//奇数
            {//smat=mat[row].reserver();
             smat.swap(mat[row]);
             reverse(smat.begin(),smat.end());
             pmat.insert(pmat.end(),smat.begin(),smat.end());
            }
            else
            {
             //smat=mat[row];
             smat.swap(mat[row]);
             pmat.insert(pmat.end(),smat.begin(),smat.end());
            }
        }
        return pmat;
    }
};
总结: 将一个vector拷贝到另外一个vector当中,用swap函数,对一个vector进行逆序输出,用reverse函数,然后将一个vector插入到另一个vector当中,使用insert函数。 使用python
# -*- coding:utf-8 -*-
class Printer:
    def printMatrix(self, mat, n, m):
        # write code her
        smat=[]#定义一个空列表
        pmat=smat#将这个空列表进行赋值
        for row in range(n):#必须range(n),如果没有range就不不对了
            if(row%2!=0):
                smat=mat[row]
                smat=smat[::-1]#对二维数据进行赋值
                pmat.extend(smat)#将一个数组添加到另一个数组当中
            else:
                smat=mat[row]
                pmat.extend(smat)
        return pmat
 #include <iostream>
 #include <string>
using namespace std;
int main()
{
    string a[5][5];
    int i, j;
    //赋值
    for (i = 0; i < 5;++i)
    {
        for (j = 0; j < 5; ++j)
        {
            if (i<j)
            {
                a[i][j] = "口";
            }
            else
            {
                a[i][j] = "*";
            }
        }
    }

    //输出
    for (i = 0; i < 5; ++i)
    {
        for (j = 0; j < 5; ++j)
            cout << a[i][4-j] << "";
        cout << endl;
    }   
    std::cin.get();
}
**题目4**   题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 考点:遍历一个vector中的下面代码当中i是行号,j是列号,行号小于array.size(),列号小于array[i].size().
class Solution {
public:
    bool Find(int target, vector<vector<int> > array) {

        for(int i=0;i<array.size();++i)
            {
            for(int j=0;j<array[i].size();++j)
            {
                if(target==array[i][j])
                    {
                    //说明存在
                    return 1;
                }
            }

        }
        //说明不存在
        return 0;

    }
};
python实现
# -*- coding:utf-8 -*-
class Solution:
    def Find(self, target, array):
        # write code here
        for i in range(len(array)):
            for j in range(len(array[i])):
                if target==array[i][j]:
                    return 1
        return 0
遇到的问题`IndentationError:expected an indented block错误解决`基本上就是因为你的缩进没有弄好 扩展:给 进行赋值,先对最里层的int类型进行赋值(也就是列)然后在对行进行赋值
           vector<vector<int>> test;
    vector<int> v;
    int n,temp;

    cin >> n;
    test.clear();

    //输入
    for (int i = 0; i<n; i++) {
        v.clear(); //每次记得clear:)
        for (int j = 0; j < n; j++)
        {
            cin >> temp;
            v.push_back(temp);
        }
        test.push_back(v);
    }

题目5  
考点:根据不同的结果进行分类
输入父母血型,判断孩子可能的血型
这里写图片描述

 vector<string> chkBlood(string father, string mother) {
        vector<string> vec;
        if("O"==father&&"O"==mother)
            vec.push_back("O");
        else if(("A"==father&&"O"==mother)||("A"==father&&"A"==mother)||("O"==father&&"A"==mother))
            {
            vec.push_back("A");
            vec.push_back("O");
            }
        else if("A"==father&&"B"==mother||"B"==father&&"A"==mother)
            {
            vec.push_back("A");
            vec.push_back("AB");
            vec.push_back("B");
            vec.push_back("O");
            }
        else if(("A"==father&&"AB"==mother)||("B"==father&&"AB"==mother)||("AB"==father&&"AB"==mother)||("AB"==father&&"B"==mother)||("AB"==father&&"A"==mother))
            {
            vec.push_back("A");
            vec.push_back("AB");
            vec.push_back("B");
            }
        else if(("B"==father&&"O"==mother)||("B"==father&&"B"==mother)||("O"==father&&"B"==mother))
            {
            vec.push_back("B");
            vec.push_back("O");
            }
        else
            {
            vec.push_back("A");
            vec.push_back("B");
            }
        return vec;

猜你喜欢

转载自blog.csdn.net/David_Han008/article/details/78073324