欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

华为2016研发工程师编程题

程序员文章站 2024-03-12 15:11:50
...

华为2016研发工程师编程题

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

int getIndex(int arr[], int n){
    int residue = n;
    int i = 0, cur = 0;
    while(residue > 1){
        if(arr[i] == 0){
            cur++;
        }
        if(cur == 3){
            arr[i] = 1;
            residue--;
            cur = 0;
        }
        i++;
        if(i >=n ){
            i = 0;
        }
    }
    while (arr[i] != 0)
    {
        i++;
        if(i >=n ){
            i = 0;
        }
    }
    
    return i;
}

int main() {
    int n;
    while(~scanf("%d", &n)){
        if(n > 1000) n = 1000;
        int arr[1001] = {0};
        int res = getIndex(arr, n);
        printf("%d\n", res);
    }
    return 0;
}

华为2016研发工程师编程题

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

int main() {
    string str;
    while(cin >> str){
        vector<char> res;
        for (int i = 0; i< str.length(); i++){
            if(find(res.begin(), res.end(), str[i]) == res.end()){
                res.push_back(str[i]);
            }
        }
        for(int j = 0; j< res.size(); j++){
            printf("%c", res[j]);
        }
        printf("\n");
    }
}

华为2016研发工程师编程题

#include <cstdio>
#include <iostream>

using namespace std;

int numData[9][9] = {0};

bool dfs(){
    bool flag = true;
    for (int i = 0; i< 9; i++){
        for (int j = 0; j< 9; j++){
            if(numData[i][j] == 0){
                flag = false;
                int remain[10] = {0};
                int  k = 0;
                for (k = 0; k< 9; k++){
                    if(numData[i][k] != 0){
                        remain[numData[i][k]] = 1;
                    }
                }
                for (k = 0; k< 9; k++){
                    if(numData[k][j] != 0){
                        remain[numData[k][j]] = 1;
                    }
                }
                for (int m = 0; m < 3; m++) {
                    for (int n = 0; n < 3; n++) {
                        if(numData[i/3*3+m][j/3*3+n] != 0){
                            remain[numData[i/3*3+m][j/3*3+n]] = 1;
                        }
                    }
                }
                
                bool ff = true;
                for (k = 1; k< 10; k++){
                    if(remain[k] == 0){
                        ff = false;
                        numData[i][j] = k;
                        bool o = dfs();
                        if(o) return true;
                    }
                }
                if(ff) return false;
                numData[i][j] = 0;
                return false;
            }
        }
    }
    if(flag){
        for (int i = 0; i< 9; i++){
            for (int j = 0; j< 9; j++){
                printf("%d ", numData[i][j]);
            }
            printf("\n");
        }
        return true;
    }
    return false;
}

int main() {
    for (int i = 0; i< 9; i++){
        for (int j = 0; j< 9; j++){
            cin >> numData[i][j];
        }
    }
    dfs();
    return 0;
}

最后一题并没有跑出全部的测试用例:
华为2016研发工程师编程题

相关标签: 算法题