acm训练 2017 02 21

训练一: 零起点学算法70——Yes,I can!
Description
Boys and girls:
Good evening.
Welcome to take the select contest. I wish you can be an ACMer, and ACM can bring the happiness of success to you. ACM is a little difficult,but if you try,you can!Nothing is impossible.
Input
One line, the ACMer’s name. The name is at most 8 characters.
Output
You should output one line like the output sample.
Output “I am” then the acmer’s name, then “,yes,I can!”
Sample Input
Mary
Sample Output
I am Mary,yes,I can!
测试代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*
Description
Boys and girls:
Good evening.
Welcome to take the select contest. I wish you can be an ACMer, and ACM can bring the happiness of success to you.
ACM is a little difficult,but if you try,you can!Nothing is impossible.
Input
One line, the ACMer's name. The name is at most 8 characters.
Output
You should output one line like the output sample.
Output "I am" then the acmer's name, then ",yes,I can!"
Sample Input
   Mary
Sample Output
   I am Mary,yes,I can!
*/
int main()
{
    char *str = calloc(8, sizeof(char));
    while (gets(str) != NULL)
    {
        int cnt = strlen(str);
        int i;
        printf("I am ");
        for (i = 0; i < cnt; i++)
            printf("%c", *(str + i));
        printf(",yes,I can!\n");
    }
    return 0;
}

运行结果:
这里写图片描述
测试结果: 通过!
简单的题目
练习二: 零起点学算法71——C语言合法标识符
Description
输入一个字符串,判断其是否是C的合法标识符。
Input
输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。
Output
对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出”yes”,否则,输出“no”。
Sample Input
3
12ajf
fi8x_a
ff ai_2
Sample Output
no
yes
no
练习代码一:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*
Description
输入一个字符串,判断其是否是C的合法标识符。
Input
输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。
Output
对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出"yes",否则,输出“no”。
Sample Input
3
12ajf
fi8x_a
ff  ai_2
Sample Output
no
yes
no
*/
int main()
{
    int n;
    scanf("%d", &n);
    while (n--)
    {
        char *str = calloc(50, sizeof(char));
        scanf("%s", str);
        int cnt = strlen(str);
        int i;
        int is = 1;
        if ((int)*str >= 48 && (int)*str <= 57) is = 0;
        for (i = 1; i < cnt; i++)
        {
            if ((int)*(str + i) >= 48 && (int)*(str + i) <= 57 || (int)*(str + i) >= 65 && (int)*(str + i) <= 90 || (int)*(str + i) >= 97 && (int)*(str + i) <= 122 || *(str + i) == '_');
            else is = 0;
        }
        if (is == 1) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

运行结果:
这里写图片描述
第三个例子错误!
分析原因: scanf(“%s”,str); 只将前面的ff存入了字符串中了
试着用gets函数,但是scanf和gets在一起使用出了问题,百度了一下,解决方案如下:
这里写图片描述
这里写图片描述
这里写图片描述
感谢@lil259
用getchar过滤掉回车
测试代码二:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*
Description
输入一个字符串,判断其是否是C的合法标识符。
Input
输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。
Output
对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出"yes",否则,输出“no”。
Sample Input
3
12ajf
fi8x_a
ff  ai_2
Sample Output
no
yes
no
*/
int main()
{
    int n;
    scanf("%d", &n);
    getchar();
    while (n--)
    {
        char *str = calloc(50, sizeof(char));
        gets(str);
        int cnt = strlen(str);
        int i;
        int is = 1;
        if ((int)*str >= 48 && (int)*str <= 57) is = 0;
        for (i = 1; i < cnt; i++)
        {
            if ((int)*(str + i) >= 48 && (int)*(str + i) <= 57 || (int)*(str + i) >= 65 && (int)*(str + i) <= 90 || (int)*(str + i) >= 97 && (int)*(str + i) <= 122 || *(str + i) == '_');
            else is = 0;
        }
        if (is == 1) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

运行结果:
这里写图片描述
测试结果: 通过!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值