判断字符串里有几个汉字 hdu 2030

最开始也在网上搜了一下,发现好多讲了内部编码,用>>什么的,我自己用>>还没搞懂。
但是现在知道了一个比较简单的思路…那就是 首先,汉字的ascll码存储时占2个字节,而英文字符占1个, 汉字的ascll码是由两个负数组成的
也就是说,你只要把循环变量每次加2去遍历数组,找到某个数组元素的ascll值为负,代表这里有一个汉字。
以下为acm 2030 代码

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
    int n,i,j,count;
    char a[100]scanf("%d",&n);
    getchar();
    for(i=0;i<n;i++){
        gets(a);
        count=0;
        int len=strlen(a);
        for(j=0;j<len;j+=2){
            if(a[j]<0) count++;
        }
        printf("%d\n",count);
        memset(a,0,sizeof(a));
    }
 return 0;
}

当然,如果有兴趣的话去了解一下具体原理更好

posted @ 2018-12-21 00:54  in_the_wind  阅读(190)  评论(0编辑  收藏  举报