找工作——计算机笔试编程题(不定期更新)

2011.9.22

1.根据加密算法和主函数写出解密算法

#include "stdafx.h"
#include  <stdio.h>
#include  <stdlib.h>
#include  <assert.h>
#include  <string.h>
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;


int  encode(const  void*  raw_in,  void*  raw_out,  uint32_t  password,  size_t  len)
{
 const  uint8_t*  in  =  (const  uint8_t*)raw_in;
 uint8_t*  out  =  (uint8_t*)raw_out;

 uint32_t  seed  =  password  ^  0x4077ba7u;
 for  (size_t  i  =  0  ;  i  <  len;  ++i)
 {
  uint8_t  a  =  ((  in[i]  ^  seed  )  >>  3)&31;
  uint8_t  b  =  (  (  ((uint32_t)in[i])  <<  17  )  ^  seed  )  >>  (17-5);
  a  &=  31;
  b  &=  224;
  a  =  31  &  (  a  ^  (b  <<  3));
  out[i]  =  a  |  b;
  seed  =  (seed  *  144123481  ^  seed  ^  in[i]);
 }
 return 0;
}


int  decode(const  void*  raw_in,  void*  raw_out,  uint32_t  password,  size_t  len)
{
 const  uint8_t*  in  =  (const  uint8_t*)raw_in;
 uint8_t*  out  =  (uint8_t*)raw_out;

 uint32_t  seed  =  password  ^  0x4077ba7u;
   for  (size_t  i  =  0  ;  i  <  len;  ++i)
   {
    uint8_t a = in[i] & 31;
    uint8_t b = in[i] & 224;

     a = (( in[i] << 3) ^ seed)&248;
     b = ((((uint32_t)in[i] << 12 ) ^ seed ) >> 17)&7;
     out[i] = a | b;
     seed  =  (seed  *  144123481  ^  seed  ^  out[i]);
  }
 return 0;
}


int  main()
{
 const  uint8_t  buf1[]  =  {0xa8,  0x5d,  0x2d,  0x27,  0xe7,  0x00,  0x83,  0x18,  0x9d,  0x9e,  0xb8,  0xbf,  0x5a,  0x7c,  0x14,  0x39,  0xfb,  0x14,  0xb9,  0x2d,  0x79,  0x92,  0x3f,  0xe0,  0x2f,  0x76,  0x69,  0x23,  0xf6,  0x84,  0xbb,  0x62,  0x51,  0x6d,  0xeb,  0x0d,  0xcb,  0x2d,  0x2b,  0x0d,  };
 uint8_t  buf2[100]  =  {};
 const  uint32_t  password  =  0xcc257936u;
 const  size_t  len  =  sizeof(buf1);
 decode(buf1,  buf2,  password,  len);
 printf("%s\n",  buf2);
}


 

 

2.遍历文件夹下的所有文件,找出唯一内容不重复的那个文件(注:都是二进制图片文件,本程序用到了开源的CRC32库文件lib_crc,下载地址:http://www.lammertbies.nl/comm/software/index.html

#include <stdio.h>
#include <windows.h>
#include "lib_crc.h"
#include<iostream>
#include<String>
#include<map>
#include <iomanip>
using namespace std;

BOOL IsRoot(LPCTSTR lpszPath)
{
 TCHAR szRoot[4];
 wsprintf(szRoot, "%c:\\", lpszPath[0]);
 return (lstrcmp(szRoot, lpszPath) == 0);
}

//递归遍历文件夹
void FindInAll(::LPCTSTR lpszPath, map<string, int>* mTool, map<string, string>* mResult)
{
 TCHAR szFind[MAX_PATH];
 lstrcpy(szFind, lpszPath);
 if (!IsRoot(szFind))
 lstrcat(szFind, "\\");
 lstrcat(szFind, "*.*"); // 找所有文件
 WIN32_FIND_DATA wfd;
 HANDLE hFind = FindFirstFile(szFind, &wfd);
 if (hFind == INVALID_HANDLE_VALUE) // 如果没有找到或查找失败
  return;

 do
 {
  if (wfd.cFileName[0] == '.')
   continue; // 过滤这两个目录
  if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  {
   TCHAR szFile[MAX_PATH];
   if (IsRoot(lpszPath))
    wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
   else
    wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);
   FindInAll(szFile, mTool, mResult); // 如果找到的是目录,则进入此目录进行递归
  }
  else
  {
   TCHAR szFile[MAX_PATH];
   if (IsRoot(lpszPath))
    wsprintf(szFile, "%s%s", lpszPath, wfd.cFileName);
   else
    wsprintf(szFile, "%s\\%s", lpszPath, wfd.cFileName);

   printf("%s\n",szFile);  //输出文件路径
   /**************对文件进行操作***************/
   //一、计算文件CRC32
   FILE *fp;
   if((fp=fopen(szFile,"r")) == NULL)  //打开文件
   {
    printf("cannot open file\n");
    getchar();
    exit(1);
   }
   fseek(fp,0,2);
   int fileLength=ftell(fp);  //取得整个文件的长度
   rewind(fp);

   unsigned long crc_32 = 0xffffffffL;
   for(int i=0; i < fileLength; i++)
   {
    int ch=fgetc( fp );
    crc_32 = update_crc_32(crc_32, (char)ch);
   }
   crc_32    ^= 0xffffffffL;
   char crc_32_char[20];
   sprintf_s(crc_32_char,"%X",crc_32);
   //printf("%s\n",crc_32_char);   //输出计算得的CRC32
   string crc32str(crc_32_char);


   //二、查询mTool表,并修改mResult表
   map<string, int>::iterator iter = mTool->find(crc32str);
   if(iter != mTool->end())
   {
    mResult->erase(crc32str);  //若该文件已出现则在mResult中删除该项
   }
   else
   {
    //若该文件未出现过则分别插入mTool和mResult中
    mTool->insert(pair<string, int>(crc32str, 1));
    mResult->insert(pair<string, string>(crc32str, szFile));
   }
   /**************对文件进行操作***************/
  }
 } while (FindNextFile(hFind, &wfd));
 FindClose(hFind); // 关闭查找句柄
}


//主函数
int main()
{
 map<string, int> mt;
 map<string, string> mr;
 FindInAll("F:\\106070", &mt, &mr);  //递归遍历目标目录并更新mt和mr

 //输出结果
 cout<<"\n共有文件(去重):"<<mt.size()<<"个\n";
 cout<<"\n其中出现次数为1的文件是:\n";
 map<string, string>::iterator it;
 for( it=mr.begin(); it!=mr.end(); ++it)
  cout<<"文件路径:"<<setw(40)<<left<<(*it).second<<"CRC32值:"<<(*it).first<<endl;
 cout<<endl;
 return 0;
}

 -------------------------------------------------------------------------------------------------------------------------

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值