随笔分类 - c++
摘要:一直没有好好的扎扎实实的算法的基础,要找工作了,临时抱下佛脚,顺便把学的东西整理下,以应对比较健忘的大脑。。。废话不说,直接主题,其实整理这个,借鉴了不少这个blog,http://www.cppblog.com/shongbee2/archive/2009/04/25/81058.html在此再次感谢这个博主,但愿有一天,自己也能请博主喝杯咖啡 哈哈~先从最熟悉的冒泡排序开始吧:冒泡排序(BubbleSort)冒泡排序也是我接触到的最早的排序,其基本思想是把数组中每个元素都看成是有质量的气泡,每个气泡的质量就是数组对应位置的元素的大小。排序的过程就是不停的把质量较轻(元素较小)的气泡上浮的过
阅读全文
摘要:Implement an algorithm to find the nth to last element of a singly linked list 分析:想法很简单,但是不是一下子就能想到的: 两个指针p1,p2分别指向头节点,然后让p1先循环n-1次,这样p1与p2 的间隔就是n-1,然后同时增加p1,p2,当p2到达尾节点的时候,p1正好到达倒数地n个节点。Linklist nthToLast(Linklist L,int n)
{ if(L==NULL||n<1) return NULL; Linklist p1=L; Linklist p2=L; for(int ...
阅读全文
摘要:Write code to remove duplicates from an unsorted linked list FOLLOW UPHow would you solve this problem if a temporary buffer is not allowed?如果我们用缓冲区,我们可以在hash表里跟踪每一个元素,同时删除任何重复的。本文主要是记载线性表的一些实现方法。struct LNode
{ ElemType data; struct LNode *next;
};
typedef struct LNode *Linklist;线性表的定义;void CreatL..
阅读全文
摘要:Write an algorithm such that if an element in an MxN matrix is 0, its entire row andcolumn is set to 0写一个算法使得把一个M*N的矩阵中0元素所在位置的行列设置为零At first glance, this problem seems easy: just iterate through the matrix and every time wesee a 0, set that row and column to 0 There’s one problem with that soluti..
阅读全文
摘要:Given an image represented by an NxN matrix, where each pixel in the imageis 4bytes, write a method to rotate the image by 90 degrees Can you do this in place?描述:给定一个N*N的图像,每个位置的像素是4byte,写一个方法用来在原来的空间内旋转图像90度。思路:我们可以按层来旋转。void matrixRotation(int [][]a,int n)
{ for(int layer=0;layer<n;layer++) {
.
阅读全文
摘要:DescriptionA palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome.As an exa
阅读全文
摘要:DescriptionA cyclic number is an integer n digits in length which, when multiplied by any integer from 1 to n, yields a"cycle"of the digits of the original number. That is, if you consider the number after the last digit to "wrap around"back to the first digit, the sequence of di
阅读全文
摘要:骑士旅游(Knight tour)在十八世纪初倍受数学家与拼图迷的注意,它什么时候被提出已不可考,骑士的走法为西洋棋的走法,骑士可以由任一个位置出发,它要如何走完所有的位置? 骑士的走法,基本上可以使用递归来解决,但是纯綷的递归在维度大时相当没有效率,一个聪明的解法由J.C. Warnsdorff在1823年提出,简单的说,先将最难的位置走完,接下来的路就宽广了,骑士所要走的下一步,「为下一步再选择时,所能走的步数最少的一步。」,使用这个方法,在不使用递归的情况下,可以有较高的机率找出走法(找不到走法的机会也是有的)。 在一个n m 格子的棋盘上,有一只国际象棋的骑士在棋盘的左下角,骑士只..
阅读全文
摘要:河内之塔(TowersofHanoi)是法国人M.Claus(Lucas)于1883年从泰国带至法国的,河内为越战时北越的首都,即现在的胡志明市;1883年法国数学家EdouardLucas曾提及这个故事,据说创世纪时Benares有一座波罗教塔,是由三支钻石棒(Pag)所支撑,开始时神在第一根棒上放置64个由上至下依由小至大排列的金盘(Disc),并命令僧侣将所有的金盘从第一根石棒移至第三根石棒,且搬运过程中遵守大盘子在小盘子之下的原则,若每日仅搬一个盘子,则当盘子全数搬运完毕之时,此塔将毁损,而也就是世界末日来临之时。解法如果柱子标为ABC,要由A搬至C,在只有一个盘子时,就将它直接搬至C
阅读全文
摘要:感觉自己用C++越来越生疏了,所以在网上找到了一些题,然后每天做一个练习,不为什么,从基础做起,权当作自己练习的见证了,今天就从最简单的冒泡排序开始~~问题描述:问题的提出:某大学开田径运动会,现有12名选手参加100米比赛,对应的运动员号及成绩如表所示,请按照成绩排名并输出,要求每一行输出名次、运动员号及成绩。要求用冒泡法排序。运动员号成绩(秒)运动员号成绩(秒)00113.603114.900214.803612.601012.003713.401112.710212.502315.632515.302513.443812.7#include <iostream>
#inclu
阅读全文

浙公网安备 33010602011771号