1. 程式人生 > 其它 >華為機試題 簡單錯誤記錄

華為機試題 簡單錯誤記錄

簡介

噁心
4、迴圈記錄時,只以第一次出現的順序為準,後面重複的不會更新它的出現時間,仍以第一次為準.
浪費爺爺的時間.
明明只有8個空間, 卻按照從頭開始查詢.

code

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
struct Error{
    string in;
    int line;
    int time;
};
string decomp(string filepath){
    int end_pos = filepath.size()-1;
    for(; end_pos >= 0; end_pos--){
        if(filepath[end_pos] == '\\') break;
    }
    string file = filepath.substr(end_pos + 1, filepath.size() - end_pos - 1);
    if(file.size() > 16) file = file.substr(file.size()-16, 16);
    return file;
}
void dec(string in, string &out, int &line) {
    for(int i=0; i<in.size(); i++) {
        if(in[i] == ' ') {
            for(int j=i-1; j>=0; j--){
                if(in[j] == '\\') break;
                if(out.size() >= 16) break;
                out += in[j];
            }
            reverse(out.begin(), out.end());
            stringstream ss;
            string num = in.substr(i+1);
            ss << num;
            ss >> line;
            return;
        }
    }
    return;
}
int main() {
    string in;
    vector<Error> v(8, {"", -1, 1});
    string filepath; int length;
    int index = 0;
    while(cin >> filepath >> length) {
        string file = decomp(filepath);
        string s = file;
        int line = length;
        bool check = false;
        for(int i=0; i<8; i++){
            if(v[i].in == s && v[i].line == line){
                v[i].time++;
                check = true;
                break;
            }
        }
        if(check) {
            continue;
        }else{
            v[index].in = s;
            v[index].line = line;
            v[index].time = 1;
            index++;
            index = index % 8;
        }
    }
    for(int i = index; i < 8; i++){
        if(v[i].line != -1)
            cout << v[i].in << " " << v[i].line << " " << v[i].time << std::endl;
    }
    for(int i = 0; i < index; i++){
        cout << v[i].in << " " << v[i].line << " " << v[i].time << std::endl;
    }
    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <set>
using namespace std;
struct Error{
    string in;
    int line;
    int time;
};
string decomp(string filepath){
    int end_pos = filepath.size()-1;
    for(; end_pos >= 0; end_pos--){
        if(filepath[end_pos] == '\\') break;
    }
    string file = filepath.substr(end_pos + 1, filepath.size() - end_pos - 1);
    if(file.size() > 16) file = file.substr(file.size()-16, 16);
    return file;
}
void dec(string in, string &out, int &line) {
    for(int i=0; i<in.size(); i++) {
        if(in[i] == ' ') {
            for(int j=i-1; j>=0; j--){
                if(in[j] == '\\') break;
                if(out.size() >= 16) break;
                out += in[j];
            }
            reverse(out.begin(), out.end());
            stringstream ss;
            string num = in.substr(i+1);
            ss << num;
            ss >> line;
            return;
        }
    }
    return;
}
int main() {
    string in;
    vector<Error> v(8, {"", -1, 1});
    string filepath; int length;
    set<string> dis;
    int index = 0;
    while(cin >> filepath >> length) {
        string file = decomp(filepath);
        string s = file;
        int line = length;
        bool check = false;
        
        for(int i=0; i<8; i++){
            if(v[i].in == s && v[i].line == line){
                v[i].time++;
                check = true;
                break;
            }
        }
        if(check) {
            continue;
        }else{
            if(dis.find(file + " " + to_string(line)) != dis.end()) {
                continue;
            }
            v[index].in = s;
            v[index].line = line;
            v[index].time = 1;
            index++;
            index = index % 8;
        }
        dis.insert(file + " " + to_string(line));
    }
    for(int i = index; i < 8; i++){
        if(v[i].line != -1)
            cout << v[i].in << " " << v[i].line << " " << v[i].time << std::endl;
    }
    for(int i = 0; i < index; i++){
        cout << v[i].in << " " << v[i].line << " " << v[i].time << std::endl;
    }
    return 0;
}