一些笔试中会出现的题目

// 1、现在有大量的英语文章、统计出里面每个单词出现的次数,考虑程序运行的效率,给出思路。(不区分大小写,假设只是含有英文字母。)

#include <stdio.h>
#include <string.h>

//能统计的最大单词个数,可以自己改
#define MAX_WORD_COUNT 500

//结构体,保存每个单词及对应的个数
typedef struct WordCount
 {
 char cWord[26];
 int  iCount;
}T_WordCount;

int CalcEachWord(const char *pText);//计算单词个数及输出信息等
void LowerText(char *pText);//把单词变成小写形式
void SwapItem(T_WordCount *ItemA, T_WordCount * ItemB);//交换两个元素
void SortWord(T_WordCount *pWordSet);//排序

int main(int argc, char *argv[])
{
 //测试文本
 char pText[] ="Text HAs HAS ONE h-as MOrE Has MORE ha-S BLANk more blank or more oR blank  Between   wor-ds.";
 printf("The text is :\n");
 printf("----------------------------------\n");
 printf("%s\n", pText);
 printf("----------------------------------\n");
 printf("The top 5 words is :\n");

//这个方法就是统计单词的个数
 CalcEachWord(pText); return 0;
}

int CalcEachWord(const char *pText)
{
 char cTmp[20] = {0};
 int  i   = 0;
 char *pTmp   = cTmp;
 int  iFlag   = 0;

 T_WordCount tWordSet[MAX_WORD_COUNT];
 memset(tWordSet, 0, sizeof(tWordSet)); //这个不是清零吗

 while (*pText != '\0')
 {
  if ((*pText >= 'A' && *pText <= 'Z') || (*pText >= 'a' && *pText <= 'z'))
  {   
   *pTmp = *pText;
   pTmp++;
  }
  else if (*pText == '-')
  {
   ++pText;
   continue;
  }
  else
  {
   if (strlen(cTmp) > 0)
   {
    LowerText(cTmp);
    iFlag = 0;
    for (i = 0; i < MAX_WORD_COUNT; ++i)
    {
     if (strlen(tWordSet[i].cWord) > 0)
     {
      if (strcmp(tWordSet[i].cWord, cTmp) == 0)
      {
       iFlag = 1;
       tWordSet[i].iCount++;
       break;
      }     
     }
     else
     {
      strcpy(tWordSet[i].cWord, cTmp);
      tWordSet[i].iCount = 1;
      iFlag = 1;
     break;
     }
    }
    if (!iFlag)
    {
     printf("No more space to save word.\n");
    }
  }
   memset(cTmp, 0, 20);
   pTmp = cTmp;
  }
  ++pText;
 }

//排序 SortWord(tWordSet);
 for (i = 0; i < 5; ++i)
 {
  if (strlen(tWordSet[i].cWord) > 0)
  {
   printf("%s:%d\n",tWordSet[i].cWord,tWordSet[i].iCount);
  }
 }
 return 0;
}

void LowerText(char *pText)
{
 char *pTmp = pText;
 while (*pTmp != '\0')
 {
  if ((*pTmp >= 'A' && *pTmp <= 'Z'))
  {
   *pTmp += 32 ;
  }
  pTmp++; }
}

void SwapItem(T_WordCount *ItemA, T_WordCount * ItemB)
{
 T_WordCount Tmp;
 memset(&Tmp, 0, sizeof(T_WordCount));
 strcpy(Tmp.cWord, ItemA->cWord);
 Tmp.iCount = ItemA->iCount;
 strcpy(ItemA->cWord, ItemB->cWord); ItemA->iCount = ItemB->iCount;
 strcpy(ItemB->cWord, Tmp.cWord); ItemB->iCount = Tmp.iCount;
}
//冒泡排序算法
void SortWord(T_WordCount *pWordSet){
 int i,j;
 for (j = 0; j < MAX_WORD_COUNT - 1; j++)
 {   
  for (i = 0; i < MAX_WORD_COUNT - 1 - j; i++)
  {     
   if (pWordSet[i].iCount < pWordSet[i+1].iCount)     
   {                             
   SwapItem(&pWordSet[i], &pWordSet[i+1]);
   }     
 }
 }
}

// 这里是调用文件
/*
FILE *fp=NULL;
fp=fopen("d:\\text.txt","r+");
if(NULL == fp)
{
    return -1;
}
char cBuf[1001]={0};
fread(cBuf, 1, 1000,fp);
CalcEachWord(cBuf);

fp=fopen("d:\\text.txt","r+");这句就是打开你要读的文件,d:\\text.txt,换成你自己的文件就可以了。
*/

2、如果是输入的字符串只有包含字母和#号,编写一个函数func,功能是除了中间和尾部的#号外,将字符串中的其他#全部删除。


3、假设在32为机器上,请在2亿个随机的未经排序的32位整形数字中找出中间值。







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值