P3741 getchar+getline只有6分,但cin却能直接通过
查看原帖
P3741 getchar+getline只有6分,但cin却能直接通过
1054147
kaedeakari楼主2024/7/3 10:47

  如题,代码萌新,学了getline函数就想多用用,发现它跟在cin后面会读空值,百度之后得知需要加一个getchar()。

  然后就发生了这种输入方法只有66分的惨剧(只有#19、#22、#23通过了),但是直接用cin输入又是AC。具体代码如下。

  因为本地和洛谷的IDE上跑样例都是正常的,所以很在意到底是哪里出了差错。如果有大佬愿意赐教,本人不胜感激!!

#include <iostream>
using namespace std;

int main(){
    string text;
    int n;
    
    cin >> n;
    getchar();
    getline(cin,text);
    //↑这样输入只有6分,笑死
    
    //cin >> n >> text;     ←这个就直接AC了
    
    int count=0,countV=0,countK=0;
    bool isChanged = 0;
    for(int i=0;i<n;i++){
        if(text[i]=='V'){
            countK = 0;
            countV ++;
            if(isChanged==0){
                if((countV == 3)||((i==n-1)&&(countV==2))){
                count ++;
                isChanged = 1;
                }
            }
        }else if(text[i]=='K'){
            if(countV)  count++;
            countV = 0;
            countK ++;
            if(isChanged==0){
                if((countK == 3)||((i==1)&&(countK==2))){
                count ++;
                isChanged = 1;
                }
            }
        }
    }
    cout << count;
    return 0;
}
2024/7/3 10:47
加载中...