在使用for(auto a : c){}和int arr[]遇到的一个问题

在写牛客编程的密码验证合格程序时遇到的。

#include <iostream>
using namespace std;
void varify(string s){
    int arr[4];
    int point = 0;
    int index = 0;
    int repeat = 0;
    if(s.length()<=8){
        cout<<"NG"<<endl;
    }
    else{
        for(auto a:s){
            cout<<s<<endl;
            if(a>='a'&&a<='z'){
                arr[0] = 1;
            }
            else if(a>='A'&&a<='Z'){
                arr[1] = 1;
            }
            else if(a>='0'&&a<='9'){
                arr[2] = 1;
            }
            else{
                arr[3] = 1;
            }
            if(index<=s.length()-2){
                char a1 = a;
                char a2 = char(a+1);
                char a3 = char(a+2);
                
            for(int i = point+1;i<s.length()-2;i++){
                if(a1==s[i]&&a2==s[i+1]&&a3==s[i+2]){
                    repeat++;
                    index=s.length();
                    
                }
                cout<<"1="<<a1<<" 2="<<s[i]<<" idex"<<index<<endl;
            }
            }
            point++;
            index++;
        }
        int count = 0;
        for(int i:arr){
            if(i==1)
            count++;
        }
        if(count>=3&&repeat==0){
            cout<<"OK"<<endl;
        }
        else{
            cout<<"NG"<<endl;
        }

    }
}
int main() {
    string a;
    while (cin >> a) { // 注意 while 处理多个 case
        varify(a);
    }
}

如代码所示,我想直接a+1来访问s字符串其它字符,但是运行时会出现乱七八糟的字符。

修改成fo(int i=0;i<s.length();i++)就不会有这样的问题

还有int arr[4]我没有设定初始值的时候,默认值似乎是1不是0,改成int[4]{0,4}就好了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值