牛客ACM模式学习记录

提示:牛客OJACM模式练习记录,记录犯错的地方

文章目录

  • 前言
  • HJ1、字符串最后一个单词的长度
  • HJ17、坐标移动
  • HJ21、简单密码
  • HJ22、借汽水瓶
  • HJ23、删除字符串中出现次数最少的字符
  • HJ35、蛇形矩阵
  • 总结


前言

HJ1、字符串最后一个单词的长度

犯错点:题目要求的是输入一行,所以使用getline来获取一行字符串,而不是cin获取单个字符串;因为此cin使用==空白(空格、制表符、换行符)==来确定字符串的结束位置,所以在当前题意下只能得到一个单词

#include 
#include 
using namespace std;
int main(){
    string str;
    getline(cin, str); // 将一行的数据 赋值给str 字符串
    int n = str.size();
    int i = n - 1;
    for(; i >= 0 && str[i] != ' '; i--);
    cout << n - i - 1 << endl;

	/* 其他思路 */
	string s;
	while(cin >> s);
	cout << s.size();
    return 0;
}

HJ17、坐标移动

题目
写了个很繁琐的代码,但能AC;下次看下题解将代码精简

#include 
#include 
#include 
using namespace std;

bool isvalid(string tmp){
    if(tmp.size() > 3)
        return false;
    if(tmp.size() <= 1)
        return false;
    if(tmp.size() == 2){
        if((tmp[0] == 'A' || tmp[0] == 'D' || 
           tmp[0] == 'W' || tmp[0] == 'S'))
            if(isdigit(tmp[1]))
                return true;
    }
    if(tmp.size() == 3){
        if((tmp[0] == 'A' || tmp[0] == 'D' || 
           tmp[0] == 'W' || tmp[0] == 'S'))
            if(isdigit(tmp[1]) && isdigit(tmp[2]))
                return true;
    }
    return false;
}

int main(){
    string str;
    cin >>str;
    string tmp;
    int x = 0, y = 0;
    for(int i = 0; i < str.size(); i++){
//     	cout<< tmp << endl;
        if(str[i] == ';'){
        	if(tmp.size()==0)
                continue;
            if(isvalid(tmp)){
                int num;
                if(tmp.size() == 2)
                    num = tmp[1] - '0';
                else if(tmp.size() == 3){
                    num = stoi(tmp.substr(1, 2));
                    
                }
                
                if(tmp[0] == 'A')
                    x -= num;
                else if(tmp[0] == 'D')
                    x += num;
                else if(tmp[0] == 'W')
                    y += num;
                else if(tmp[0] == 'S')
                    y -= num;
// 				cout << num << "*" <
            }
            tmp.clear();
        }
        else{
            tmp += str[i];
        }
        
    }
    cout << x << ',' << y;
    
    return 0;
}

HJ21、简单密码

模拟 + 哈希

#include 
#include 
#include 
using namespace std;
int main(){
    string str; 
    cin >> str;
    string res;
    for(char c : str){
        if (c >= 'a' && c <= 'z'){
            if(c == 'a' || c == 'b' || c == 'c')
                res += '2';
            else if(c == 'd' || c == 'e' || c == 'f')
                res += '3';
            else if(c == 'g' || c == 'h' || c == 'i')
                res += '4';
            else if(c == 'j' || c == 'k' || c == 'l')
                res += '5';
            else if(c == 'm' || c == 'n' || c == 'o')
                res += '6';
            else if(c == 'p' || c == 'q' || c == 'r' || c == 's')
                res += '7';
            else if(c == 't' || c == 'u' || c == 'v')
                res += '8';
            else if(c == 'w' || c == 'x' || c == 'y' || c == 'z')
                res += '9';
        }
        else if(c >= '0' && c <= '9')
            res += c;
        else if(c >= 'A' && c <= 'Y')
            res += (c + 33);
        else if(c == 'Z') // 回到起点,特处
            res += 'a';
    }
    cout << res << endl;
    
    return 0;
}


#include 
#include 
#include 
using namespace std;
int main(){
    string str; 
    cin >> str;
    map<char, int> imap{
        {'a', 2},{'b', 2},{'c', 2},
        {'d', 3},{'e', 3},{'f', 3},
        {'g', 4},{'h', 4},{'i', 4},
        {'j', 5},{'k', 5},{'l', 5},
        {'m', 6},{'n', 6},{'o', 6},
        {'p', 7},{'q', 7},{'r', 7},{'s', 7},
        {'t', 8},{'u', 8},{'v', 8},
        {'w', 9},{'x', 9},{'y', 9},{'z', 9}
    };
    string res;
    for(char c : str){
        if (c >= 'a' && c <= 'z'){
            res += to_string(imap[c]);
        }
        else if(c >= '0' && c <= '9')
            res += c;
        else if(c >= 'A' && c <= 'Y')
            res += (c + 33);
        else if(c == 'Z')
            res += 'a';
    }
    cout << res << endl;
    return 0;
}



#include 
#include 
#include 
using namespace std;
int main(){
    string str; 
    cin >> str;
    char pwd[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"};
    char ans[] = {"bcdefghijklmnopqrstuvwxyza222333444555666777788899990123456789"};
    for(int i = 0; i < str.size(); i++){
        if (str[i] >= 'a' && str[i] <= 'z'){
            str[i] = ans[str[i] - 'a' + 26];
        }
        else if(str[i] >= 'A' && str[i] <= 'Z'){
         
            str[i] = ans[str[i] - 'A'];
        }
    }
    cout << str << endl;
    return 0;
}

HJ22、借汽水瓶

特殊判断,可以借汽水瓶

#include
using namespace std;
int main(){
    int num;
    while(cin >> num){
        if(num == 0)
            break;
        int cnt = 0;
        while(num > 2){
            cnt += num / 3;
            num = num / 3 + num % 3;
        }
        if(num == 2)
            cout << cnt+1 << endl;
        else
            cout << cnt << endl;
    }
    return 0;
}

HJ23、删除字符串中出现次数最少的字符

#include 
#include 
#include 
using namespace std;
int main(){
    string str;
    cin >> str;
    int dig[26] = {0};
    int imin = 21;
    for(auto c : str){
        dig[c-'a']++;
    }
    for(int cnt : dig)
        if(cnt >= 1)
            imin = min(imin, cnt);
    string res;
    set<char> iset;
    for(int i = 0; i <26; i++)
        if(imin == dig[i]){
            iset.insert(i + 'a');
        }
    for(char c : str){
        if(!iset.count(c))
            res += c;
    }
    cout << res;
    return 0;
}

HJ35、蛇形矩阵

这种模拟的矩阵遍历题目不是很会


总结

待完善

你可能感兴趣的:(算法,算法,c++)