zoukankan      html  css  js  c++  java
  • 杭电2030汉字统计(已解决)

    #include "stdio.h"

    int main(int argc, char* argv[])
    {
        int index,sum;
        char c;
        scanf("%d",&index);
        getchar();
        while (index--)
        {
            sum =0 ;
            while ( (c= getchar())!= '\n')
            {
                if (c < 0 )
                    sum++;
            }
            printf("%d\n",sum/2);
        }
        return 0;
    }

    以上是转的别人的代码!可以AC过去!

    #include <cstdlib>
    #include <iostream>
    #include <string>
    using namespace std;

    int main(int argc, char *argv[])
    {
        int d,len,n;
        char str[10000];
        getchar();
        cin>>n;
        while(n--)
        {
          d=0;
          gets(str);
          len=strlen(str);
          for(int i=0;i<len;i++)
          {
            if(str[i]<0)
              d++;
          }
          cout<<d/2<<endl;
        }
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    上面这段是我自己的代码!为什么会超时呢?通过这几天的做题,我发现自己弄不懂为什么会超时?继续努力,争取解决超时问题,有能力的话,总结一下超时的原因!请大家多多帮忙。谢谢啦~

    另外,还有一事不明白,求解汉子个数时的条件为什么是“str[i]<0”?

    小结:(出自:http://hi.baidu.com/gminking/blog/item/6fcea58f918da9ddfd1f10bd.html

    1—— 一个汉字在字符串中是以两个负的字符形式存储,所以本题只要把字符串中负字符的个数找出来,再

    除以2 就OK了。

    2——汉字机内码在计算机的表达方式的描述是,使用二个字节,每个字节最高位一位为1。
    计算机中, 补码第一位是符号位, 1 表示为 负数, 所以 汉字机内码的每个字节表示的十进制数都是负数

    下面的代码是改正后的,就是有一点区别:

    #include <cstdlib>
    #include <stdio.h>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int d,len,n;
        char str[1000];
        
        cin>>n;
        getchar();
        while(n--)
        {
          d=0;
          gets(str);
          len=strlen(str);
          //cout<<"len="<<len<<endl;
          for(int i=0;i<len;i++)
          {
            if(str[i]<0)
              d++;
          }
          cout<<d/2<<endl;
        }
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    区别在于getchar()的位置,当把getchar放到cin下边时,就AC了,具体原因还是不明白!!!为什么这样写就不会超时呢?继续努力ing~

    以下是我朋友的代码(可以AC):

    #include <cstdlib>
    #include <iostream>
    #include<string.h>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int n,len,count;
        char data[10000];
        while(cin>>n)
        {
        for(int i=0;i<n;i++)
         {count=0;
         if(i==0)
         getchar();
         gets(data); 
         len=strlen(data);
         for(int j=0;j<len;j+=2)
           if(data[j]<0) 
            count++;
           cout<<count<<endl; 
        } 
                 
         } 
        // system("PAUSE");
        return EXIT_SUCCESS;
    }

    供大家参考!

  • 相关阅读:
    用javascript获取屏幕高度和宽度等信息
    Delphi程序启动参数的读取
    在CSS中使用javascript运算表达式
    How to check an Internet connection
    CheckMenuItem Function in Delphi
    在delphi中添加一个菜单项到Windows的系统菜单
    Delphi中直接将DataSet中的数据写入Excel文件
    带有TClientDataSet的delphi应用程序在发布时应注意的问题
    Delphi下一个封装较为完整的DBGrid>Excel类
    how to advertent to connect to internet?
  • 原文地址:https://www.cnblogs.com/gkfeng/p/2615144.html
Copyright © 2011-2022 走看看