LeeBlog

导航

HDU 2030 汉字统计

    前几天在学校水题时就在看到了这个水题,开始觉得有点高深( 特别是最下面有个提示,从汉字几内码考虑,表示没有机内码这个概念 ),所以一直没水,今天再看到这个题,不就时ASCII码吗?再偷瞄了DISCUSS一眼,发现我的思路没错故果断水了此题,还有一个汉字相当于两个英文字母,所以在遍历时遇到汉字要加2

代码
#include <stdio.h>
#include
<string.h>
int main ( )
{
char cha[100000];
int n;
while ( scanf ( "%d%*c" , &n ) == 1 )//有gets()的地方别把%*c(吃回车)给漏了
{
while ( n-- )
{
gets ( cha );//文章中有空格,必须要用gets()
int length = strlen ( cha ),count = 0;
int i = 0;
for ( ; i < length ; ++i )
if ( cha[i] < 0 || cha[i] > 127 )
{
count
++;
i
++;//这里不能加2,因为每次循环本身就要加1
}
printf (
"%d\n",count );
}
}
return 0;
}

 

posted on 2011-01-30 16:03  LeeBlog  阅读(440)  评论(0编辑  收藏  举报