朗吾東 2022-04-12 17:39 采纳率: 71.4%
浏览 30
已结题

PTA上面的,自己找不出问题所在

我自己感觉我的代码没什么问题,但就是答案错误,大家伙帮我看看

img


下面是代码
#include<stdio.h>
#include<string.h>
int main()
{
int N;
scanf("%d",&N);
while(N>0)
{
char ch[81];
gets(ch);
int a=0,b=0,t=0,l=strlen(ch);
N--;
if(l<6)
{
printf("Your password is tai duan le.\n");
continue;
}
for(int i=0;i<l;i++)
{
if((ch[i]>'9'||ch[i]<'0')&&(ch[i]<'a'||ch[i]>'z')&&(ch[i]<'A'||ch[i]>'Z')&&(ch[i]!='.'))
{
t=1;
break;
}
}
if(t==1)
{
printf("Your password is tai luan le.\n");
continue;
}
for(int i=0;i<l;i++)
{
if(ch[i]>='0'&&ch[i]<='9')
{
a=1;
break;
}
}
for(int i=0;i<l;i++)
{
if((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z'))
{
b=1;
break;
}
}
if(a==1&&b==1)
printf("Your password is wan mei.\n");
if(a==1&&b==0)
printf("Your password needs zi mu.\n");
if(a==0&&b==1)
printf("Your password needs shu zi.\n");
}
return 0;
}

  • 写回答

1条回答 默认 最新

  • a5156520 2022-04-12 18:30
    关注

    不知道对不对,我在Dev-C++里面,测试发现在读取整数后就进行了一次密码检查,所以我修改为在读取整数后用一个while循环将scanf读取整数后丢弃的换行符清除,再读取一个密码字符串。修改如下:

    
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        int N;
        scanf("%d",&N);
        while(getchar()!='\n')  //去除读取整数后剩余的换行符,让后面gets读取到一个密码字符串 
            continue; 
        while(N>0)
        {
            char ch[81];
            gets(ch);
            int a=0,b=0,t=0,l=strlen(ch);
            N--;
            
            if(l<6)
            {
                printf("Your password is tai duan le.\n");
                continue;
            }
            for(int i=0;i<l;i++)
            {
                if((ch[i]>'9'||ch[i]<'0')&&(ch[i]<'a'||ch[i]>'z')&&(ch[i]<'A'||ch[i]>'Z')&&(ch[i]!='.'))
                {
                    t=1;
                    break;
                }
            }
            if(t==1)
            {
                printf("Your password is tai luan le.\n");
                continue;
            }
            for(int i=0;i<l;i++)
            {
                if(ch[i]>='0'&&ch[i]<='9')
                {
                    a=1;
                    break;
                }
            }
            for(int i=0;i<l;i++)
            {
                if((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z'))
                {
                    b=1;
                    break;
                }
            }
            if(a==1&&b==1)
                printf("Your password is wan mei.\n");
                
            if(a==1&&b==0)
                printf("Your password needs zi mu.\n");
                
            if(a==0&&b==1)
                printf("Your password needs shu zi.\n");
        }
        
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 4月20日
  • 已采纳回答 4月12日
  • 创建了问题 4月12日

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab