自己写代码遇到的一个问题

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
string s,s0;
char ss;
int cnt=0;
/*int shuzi(char c)
{
    if(c=='a'||c=='A') return 1;
    if(c=='b'||c=='B') return 2;
    if(c=='c'||c=='C') return 3;
    if(c=='d'||c=='D') return 4;
    if(c=='e'||c=='E') return 5;
    if(c=='f'||c=='F') return 6;
    if(c=='g'||c=='G') return 7;
    if(c=='h'||c=='H') return 8;
    if(c=='i'||c=='I') return 9;
    if(c=='j'||c=='G') return 10;
    if(c=='k'||c=='K') return 11;
    if(c=='l'||c=='L') return 12;
    if(c=='m'||c=='M') return 13;
    if(c=='n'||c=='N') return 14;
    if(c=='o'||c=='O') return 15;
    if(c=='p'||c=='P') return 16;
    if(c=='q'||c=='Q') return 17;
    if(c=='r'||c=='R') return 18;
    if(c=='s'||c=='S') return 19;
    if(c=='t'||c=='T') return 20;
    if(c=='u'||c=='U') return 21;
    if(c=='v'||c=='V') return 22;
    if(c=='w'||c=='W') return 23;
    if(c=='x'||c=='X') return 24;
    if(c=='y'||c=='Y') return 25;if(c=='z'||c=='Z') return 26;
}*/
void jiami()
{
   /*srand(time(NULL));
    int f1,f2,d1,d2;
    int q=1;
    int a=rand()%26;
    int b=rand()%26;
    f1=(a*b)%2;///0 daxie+++;  1   daxie  ---;
    d1=(a+b)%26;
    f2=(f1*d1)%2;
    d2=(f1+d1)%26;*/
    cin>>s0;
    int n=sizeof(s0);
    /*int c=rand()%26,cc=rand()%2;
    if(cc) {ss='A'+c;cout<<ss;}
    else {ss='a'+c;cout<<ss;}*/
    for(int i=0;i<=n/2;i++)
    {
        /*int y;
        int r=rand()%2;///0 daxie  ;1  xiaoxie;
        if(r)
        {
            if(f1) y=(shuzi(s0[i])+d1)%26;
            else y=(shuzi(s0[i]+26-d1))%26;
            ss='a'+y-1;
            cout<<ss;
        }
        else
        {
            if(f1) y=(shuzi(s0[i]+26-d1))%26;
            else y=(shuzi(s0[i])+d1)%26;
            ss='A'+y-1;
            cout<<ss;
        }*/
        cnt++;

    }
    ///s[q++]='a'+d1-1;
    for(int i=n/2+1;i<=n;i++)
    {
        /*int y;
        int r=rand()%2;///0 daxie  ;1  xiaoxie;
        if(r)
        {
            if(f2) y=(shuzi(s0[i])+d2)%26;
            else y=(shuzi(s0[i]+26-d2))%26;
            ss='a'+y-1;
            cout<<ss;
        }
        else
        {
            if(f2) y=(shuzi(s0[i]+26-d2))%26;
            else y=(shuzi(s0[i])+d2)%26;
            ss='A'+y-1;
            cout<<ss;
        }*/
        cnt++;
    }



}
int main()
{


    jiami();
    cout<<endl<<cnt<<endl;
    return 0;
}
```## 

我自己在写一个加密算法的时候发现for循环会一直循环25次![在这里插入图片描述](https://img-blog.csdnimg.cn/20200228165207598.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDYwNzI0Ng==,size_16,color_FFFFFF,t_70)注释掉一些以后还是如此。



后来才知道string的长度要用str.size()求

```cpp
#include <iostream>
#include <string>
#include <bits/stdc++.h>
using namespace std;

int cnt=0;

int main()
{
    string s0;
    cin>>s0;
    int n=s0.size();
     for(int i=0;i<=n/2;i++)
    {

        cnt++;

    }
    for(int i=n/2+1;i<=n;i++)
    {

        cnt++;
    }
    cout<<endl<<cnt<<endl<<n;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值