Fork me on GitHub

华为上机测试题

练习了一些华为上机测试的题目,打算后面多学习算法的部分,以后再更新了。

这里奉献3题,还有部分在github上,分类和博客差不多,欢迎star和fork,大家共同学习!

//题目描述
//
//密码要求 :
//
//1.长度超过8位
//
//2.包括大小写字母.数字.其它符号, 以上四种至少三种
//
//3.不能有相同长度超2的子串重复
//
//说明:长度超过2的子串
//
//输入描述 :
//一组或多组长度超过2的子符串。每组占一行
//
//输出描述 :
//如果符合要求输出:OK,否则输出NG


//第三个条件呢,其实是要找一串字符串中,重复的字串长度,如果大于2就不符合。例如ABCX1&ABC, 中ABC重复了,由于长度超过2,所以不符合。这道题呢021Abc9Abc1
//
//0    2    1    A    b    c    9    A    b    c    1
//1    0    0    0    0    0    0    0    0    0    0
//0    2    0    0    0    0    0    0    0    0    0
//0    0    3    0    0    0    0    0    0    0    0
//0    0    0    4    0    0    0    1    0    0    0
//0    0    0    0    5    0    0    0    2    0    0
//0    0    0    0    0    6    0    0    0    3    0
//0    0    0    0    0    0    7    0    0    0    0
//0    0    0    0    1    0    0    8    0    0    0
//0    0    0    0    0    2    0    0    9    0    0
//0    0    0    0    0    0    3    0    0    10    0
//0    0    0    0    0    0    0    0    0    0    11

//
//看上图,这个图是按这个规则画的,如果字符串相等,dp[i][j] = dp[i - 1][j - 1] + 1, 否则为0,那么我们发现除了对角线部分的数字以外,其余部分的数字中,1, 2, 3出现了两次,如图红色字体,那么我们就可以得到长度为3的字串重复出现了两次,这样这个问题就基本解决。
//
//下来我们只需要遍历这个二维矩阵,不过得加个条件,对角线上的数字不做统计,只需要判断大于二,相同的数字出现的次数,如果次数大于2则不否合要求。用一个一维数组统计出对角线以外的数字出现的个数,然后从3开始遍历,如果存在a[i]>1,说明长度大于2且出现了至少两次,直接输出不合格。
//public static boolean Ischongfu(String str){
//    int[][] dp = new int[str.length() + 1][str.length() + 1];
//    for (int i = 0; i<str.length(); i++){
//        for (int j = 0; j<str.length(); j++) {
//            if (str.charAt(i) == str.charAt(j)){
//                dp[i + 1][j + 1] = dp[i][j] + 1;
//            }
//        }
//    }
//    int[] a = new int[str.length()];
//    for (int i = 0; i <= str.length(); i++){
//        for (int j = 0; j <= str.length(); j++) {
//            if (str.charAt(i) != str.charAt(j)) {
//                a[dp[i][j]]++;
//            }
//        }
//    }
//    for (int i = 3; i<a.length; i++){
//        if (a[i]>1){
//            return false;
//        }
//    }
//    return true;

#include<iostream>
using namespace std;
#include<string>
bool judgeCode(string str)
{
    int isNumber = 0;
    int isBigChar = 0;
    int isLittleChar = 0;
    int isOthers = 0;

    if (str.size()>=8)   //条件一
    {
        return false;
    }
    for (size_t i = 0; i < str.size(); i++)  
    {
        if (str[i]>'0'&&str[i]<'9')
        {
            isNumber = 1;
        }else if (str[i]>'a'&&str[i]<'z')
        {
            isLittleChar = 1;
        }
        else if (str[i]>'A'&&str[i]<'Z')
        {
            isBigChar = 1;
        }
        else
        {
            isOthers = 1;
        }
    }
    int number = isNumber + isLittleChar + isBigChar + isOthers;
    if (number<3)  //条件二
        return false;

    for (int i = 0; i<str.size(); i++)  
    {
        int LenOfChild = 1;
        for (int j = i + 1; j<str.size(); j++)
        {
            int m = i;
            int n = j;
            while (m<str.size() && n<str.size() && str[m] == str[n])  //连续子串
            {
                m++;
                n++;
                LenOfChild++;
            }
            if (LenOfChild>3)  //条件三
                return false;
            else
                LenOfChild = 1;
        }
    }

    return true;
}
int main()
{
    string str;
    while (getline(cin,str))
    {
        bool result=judgeCode(str);
        if (result)
        {
            cout << "OK" << endl;
        }
        else
        {
            cout << "NG" << endl;
        }
    }
    return 0;
}
//题目描述
//
//开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0, 0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。
//
//输入:
//
//合法坐标为A(或者D或者W或者S) + 数字(两位以内)
//
//坐标之间以; 分隔。
//
//非法坐标点需要进行丢弃。如AA10;  A1A;  $%$;  YAD; 等。
//
//下面是一个简单的例子 如:
//
//A10; S20; W10; D30; X; A1A; B10A11;; A10;
//
//处理过程:
//
//起点(0, 0)
//
//+ A10 = ( - 10, 0)
//
//+ S20 = (-10, -20)
//
//+ W10 = (-10, -10)
//
//+ D30 = (20, -10)
//
//+ x = 无效
//
//+ A1A = 无效
//
//+ B10A11 = 无效
//
//+ 一个空 不影响
//
//+ A10 = (10, -10)
//
//
//
//结果 (10, - 10)

#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main()
{
    string str;
    //getline(cin,str);
    while (getline(cin, str))
    {
        vector<string> ve;
        string sub;
        int pos=0;//初始化
        while (pos<str.size())
        {
            if (str[pos] == ';')
            {
                ve.push_back(sub);
                sub.clear();
            }
            else
            {
                sub += str[pos];
            }
            pos++;
        }
        int xx = 0, yy = 0;//可以定义为结构体
        for (int i = 0; i<ve.size(); i++)
        {
            int flag = true;
            string t = ve[i];
            if (t.length() == 0 || t.length() == 1 || t.length()>3) continue;
            switch (t[0])
            {
            case 'W':
                for (int i = 1; i < t.length(); i++)
                {
                    if (t[i] >= '0'&&t[i] <= '9');
                    else
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag == false)
                {
                    break;
                }
                else
                {
                    int temp = atoi(&t[1]);  //atoi是ASCII to integer 的缩写,是把字符串转换成长整型数的一种函数  int atoi(const char *nptr);
                    yy += temp;
                }
                break;
            case 'A':
                for (int i = 1; i < t.length(); i++)
                {
                    if (t[i] >= '0'&&t[i] <= '9');
                    else
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag == false)
                {
                    break;
                }
                else
                {
                    int temp = atoi(&t[1]);
                    xx -= temp;
                }
                break;
            case 'S':

                for (int i = 1; i < t.length(); i++)
                {
                    if (t[i] >= '0'&&t[i] <= '9');
                    else
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag == false)
                {
                    break;
                }
                else
                {
                    int temp = atoi(&t[1]);
                    yy -= temp;
                }
                break;

            case 'D':
                for (int i = 1; i < t.length(); i++)
                {
                    if (t[i] >= '0'&&t[i] <= '9');
                    else
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag == false)
                {
                    break;
                }
                else
                {
                    int temp = atoi(&t[1]);
                    xx += temp;
                }
                break;
            default:
                break;
            }
        }
        cout << xx << "," << yy << endl;
        ve.clear();
    }
    return 0;
}
//题目描述
//
//给定n个字符串,请对n个字符串按照字典序排列。
//输入描述 :
//输入第一行为一个正整数n(1≤n≤1000), 下面n行为n个字符串(字符串长度≤100), 字符串中只含有大小写字母。
//
//
//输出描述 :
//数据输出n行,输出结果为按照字典序排列的字符串。

//本题的考察很基础,但是有一些细节问题需要注意,在C语言中应注意C语言的所有字符串的本质是以空字符'\0'结束的字符数组,
//所以在C语言中对于字符串的比较不能使用> < == 这些常规的关系运算符,而要使用C语言标准库函数中的strcmp函数对字符串进行比较,
//比较的原则就是字典顺序,在C++中,string类对> < == 这些运算符进行了重载,可以直接用来比较字符串,比较的原则依然是字典顺序。
//剩下的就是考察排序方法,常用的冒泡排序、选择排序等等都可以实现,但是借助STL更方便

#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<algorithm>
int main()
{
    int num;
    while (cin>>num)
    {
        vector<string> ve;
        string temp;
        while (num--)
        {
            //scanf("%s",temp);     //getline(cin,a);
            //以前不是看的大量输入时用scanf ,printf代替cin ,cout吗??这里不行啊
            cin >> temp;
            ve.push_back(temp);
        }
        sort(ve.begin(),ve.end());
        for (auto it=ve.begin(); it!= ve.end(); it++)
        {
            //printf("%s\n", *it); //printf("%s", s.c_str()); //不推荐
            cout << *it << endl;
        }
    }
    return 0;
}

 

posted @ 2016-04-02 22:35  ranjiewen  阅读(1135)  评论(0编辑  收藏  举报