c语言入门必备算法 C语言与算法基础是什么?

[更新]
·
·
分类:互联网
2017 阅读

c语言入门必备算法

C语言与算法基础是什么?

C语言与算法基础是什么?

基础上是数据结构。数据结构主要研究数据存储和检索需要的结构组织方式以及相关的数据查询和操作方法,这个过程中涉及到许多经典的算法,可以看成复杂算法的基础,比如各种排序算法,深度优先和广度优先搜索,最短路径,动态规划算法等,希望以上回答可以帮助到您

c语言中一个正确的算法必须有?

C语音中的一个正确的算法必须要用输出

c语言压缩算法?

方法1:最简单就是将所有字符加起来,代码如下:
  unsigned long HashString(const char *pString, unsigned long tableSize)
  {
  unsigned long hashValue 0
  while(*pString)
  hashValue *pString
  return hashValue % tableSize
  }
  分析:如果字符串的长度有限,而散列表比较大的话,浪费比较大。例如,如果字符串最长为16字节,那么用到的仅仅是散列表的前16*1272032。假如散列表含2729项,那么2032以后的项都用不到。
  方法2:将上次计算出来的hash值左移5位(乘以32),再和当前关键字相加,能得到较好的均匀分布的效果。
  unsigned long HashString(const char *pString,unsigned long tableSize)
  {
  unsigned long hashValue 0
  while (*pString)
  hashValue (hashValue ltlt 5) *pString
  return hashValue % tableSize
  }
  分析:这种方法需要遍历整个字符串,如果字符串比较大,效率比较低。
  方法3:利用哈夫曼算法,假设只有0-9这十个字符组成的字符串,我们借助哈夫曼算法,直接来看实例:
  #define Size 10
  int freq[Size]
  string code[Size]
  string word
  struct Node
  {
  int id
  int freq
  Node *left
  Node *right
  Node(int freq_in):id(-1), freq(freq_in)
  {
  left right NULL
  }
  }
  struct NodeLess
  {
  bool operator()(const Node *a, const Node *b) const
  {
  return a-gtfreq lt b-gtfreq
  }
  }
  void init()
  {
  for(int i 0 i lt Size i)
  freq[i] 0
  for(int i 0 i lt () i)
   freq[word[i]]
  }
  void dfs(Node *root, string res)
  {
  if(root-gtid gt 0)
  code[root-gtid] res
  else
  {
  if(NULL ! root-gtleft)
  dfs(root-gtleft, res #340#34)
  if(NULL ! root-gtright)
  dfs(root-gtright, res #341#34)
  }
  }
  void deleteNodes(Node *root)
  {
  if(NULL root)
  return
  if(NULL root-gtleft ampamp NULL root-gtright)
  delete root
  else
  {
  deleteNodes(root-gtleft)
  deleteNodes(root-gtright)
  delete root
  }
  }
  void BuildTree()
  {
  priority_queueltNode*, vectorltNode*gt, NodeLessgt nodes
  for(int i 0 i lt Size i)
  {
  //0 freq[i] 的情况未处理
  Node *newNode new Node(freq[i])
  newNode-gtid i
  nodes.push(newNode)
  }
  while(() gt 1)
  {
  Node *left ()
  nodes.pop()
  Node *right ()
  nodes.pop()
  Node *newNode new Node(left-gtfreq right-gtfreq)
  newNode-gtleft left
  newNode-gtright right
  nodes.push(newNode)
  }
  Node *root ()
  dfs(root, string(#34#34))
  deleteNodes(root)
  }