??xml version="1.0" encoding="utf-8" standalone="yes"?>一级做a爱片久久毛片,久久性精品,26uuu久久五月天http://m.shnenglu.com/Joe/archive/2011/07/19/151405.htmlsimplyzhaosimplyzhaoTue, 19 Jul 2011 11:57:00 GMThttp://m.shnenglu.com/Joe/archive/2011/07/19/151405.htmlhttp://m.shnenglu.com/Joe/comments/151405.htmlhttp://m.shnenglu.com/Joe/archive/2011/07/19/151405.html#Feedback0http://m.shnenglu.com/Joe/comments/commentRss/151405.htmlhttp://m.shnenglu.com/Joe/services/trackbacks/151405.html问题:

Given a random number generator which can generate the number in range (1,5) uniformly. How can you use it to build a random number generator which can generate the number in range (1,7) uniformly?


(l定一个随机数生成器,q个生成器能均匀生成1?(1,5)的随机数Q如何用这个生成器生成均匀分布??(1,7)的数?)

解法一:
拒绝采样定理
单的? ?1-5 的随机数发生器用两次, 拼成一?q制的数, 是1-25. 这 1-25 q_分配?5U情冉|到7U情况上, 问题p决了. 因ؓ21?的倍数, 我们可以每三个映到一? ?-3 映射?, …, 19-21 映射?. 可见, q些情况之间的概率是一L. 那么, 要是拼成的数字正好是 22-25 q四个呢? 有两U方? W一U是丢弃q个数字, 从头再来, 直到拼成的数字在1-21之间. 因ؓq个是个概率法, 不能保证每次都能落在1-21, 所以采L密度不高. q有一U方? 是说, 假如落到?22-25, 那这ơ的采样l果q上次? 可以证明, q看上去两个互相矛盾的算? l果都能均等的得到等概率的分? (前者叫?Reject Sampling, 后者叫?Metropolis Algorithm, 都是数学物理模拟里面常用的方?

解法?
二进?br />1-2映射?Q?跌Q?-5映射?
生成三位的二q制卛_


simplyzhao 2011-07-19 19:57 发表评论
]]>
从n个items中随机选择m个的问题http://m.shnenglu.com/Joe/archive/2011/07/18/151277.htmlsimplyzhaosimplyzhaoMon, 18 Jul 2011 01:32:00 GMThttp://m.shnenglu.com/Joe/archive/2011/07/18/151277.htmlhttp://m.shnenglu.com/Joe/comments/151277.htmlhttp://m.shnenglu.com/Joe/archive/2011/07/18/151277.html#Feedback0http://m.shnenglu.com/Joe/comments/commentRss/151277.htmlhttp://m.shnenglu.com/Joe/services/trackbacks/151277.html问题来源: ~程珠玑

解法一:
遍历qn个itemsQy妙地利用概率来筛?
void
generate_random_m_from_n(
int n, int m)
{
    
int i, remaining, select = m;
    srand(time(NULL));
    
for(i=0; i<n; i++) {
        remaining 
= n - i;
        
if(rand()%remaining < select) {
            printf(
"%d\t", i);
            
--select;
        }
    }
    printf(
"\n");
}

解法?
shuffleQ即随机z牌E序Q然后选择前m个items卛_
代码参? http://blog.fuqcool.com/2011/04/17/algorithm-shuffle.html

z牌法的一U实?/a>

作者:fuqcool 发布旉Q?011-04-17 23:16:02 分类Q?algorithms

最q自己在做一个小的程序,需要把一个集合里面的元素全部随机地打散。自己想了一个方法,复杂度是nQ觉得不太快。后来参照了一下python关于shuffle的算法,发现我的Ҏ跟它的是一LQ连python的代码都q么写,可能已经没有办法再快了吧Q?/p>

下面来介绍z牌法Q用C语言描述?/p>

法的前提是有一个生随机数的函?/p>

// Generates a random integer between beg and end.
int GetRandomNumber(int beg, int end);

q有一个交换函数?/p>

// Swap a and b.
void Swap(int a, int b);

上面两个函数我就不写出实CQ因文章的重点在于法的讨论?/p>

假设我们有一堆扑克牌Q怎么才能把这副牌完全打ؕ呢?计算机当然不能像人手那样z牌。但是它可以产生随机敎ͼ随机C一副牌中抽Z张牌是可以的。既然这样那好办了Q我们不停地从牌堆中随机抽取一张扑克牌Q然后把q些牌堆hQ直到原来的牌堆只剩下一张牌的时候ؓ止。这样不完成了z牌的动作了吗?/p>

下面是C代码Q?/p>

int Shuffle(int[] a, int len)
{
    for (int i = len - 1; i > 0; i--)
    {
        // Select an element from index 0 to i randomly;
        int index = GetRandomNumber(0, i);
        // exchange a[i] with a[index]
        Swap(a[index], a[i]);
    }
}

Z也脓出python的random单元关于shuffle的实玎ͼ

def shuffle(self, x, random=None, int=int):
    """x, random=random.random -> shuffle list x in place; return None.

    Optional arg random is a 0-argument function returning a random
    float in [0.0, 1.0); by default, the standard random.random.
    """

    if random is None:
        random = self.random
    for i in reversed(xrange(1, len(x))):
        # pick an element in x[:i+1] with which to exchange x[i]
        j = int(random() * (i+1))
        x[i], x[j] = x[j], x[i]



simplyzhao 2011-07-18 09:32 发表评论
]]>
Google面试?/title><link>http://m.shnenglu.com/Joe/archive/2011/07/14/150924.html</link><dc:creator>simplyzhao</dc:creator><author>simplyzhao</author><pubDate>Thu, 14 Jul 2011 02:13:00 GMT</pubDate><guid>http://m.shnenglu.com/Joe/archive/2011/07/14/150924.html</guid><wfw:comment>http://m.shnenglu.com/Joe/comments/150924.html</wfw:comment><comments>http://m.shnenglu.com/Joe/archive/2011/07/14/150924.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Joe/comments/commentRss/150924.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Joe/services/trackbacks/150924.html</trackback:ping><description><![CDATA[<div>来源:<br /><a >http://coolshell.cn/articles/3345.html</a><br /><br /> <div><strong>Software Engineer</strong></div> <div> <div> <ul><li>Why are manhole covers round? Q陈皓:Z么下水井盖是圆的Q这是有NU答案的Q上Wiki看看吧)</li><li>What is the difference between a mutex and a semaphore? Which one would you use to protect access to an increment operation?</li><li>A man pushed his car to a hotel and lost his fortune. What happened? Q陈皓:脑筋急{弯?他在玩大富翁游戏Q!Q)</li><li>Explain the significance of “dead beef”.Q陈皓:要是你看到的?6q制 DEAD BEEFQ你会觉得这是什么?IPv6的地址Q)</li><li>Write a C program which measures the the speed of a context switch on a UNIX/Linux system.</li><li>Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7.Q陈皓:上StackOverflow看看吧,l典的问题)</li><li>Describe the algorithm for a depth-first graph traversal.</li><li>Design a class library for writing card games. Q陈皓:用一pd的类来设计一个扑克游戏,设计题)</li><li>You need to check that your friend, Bob, has your correct phone number, but you cannot ask him directly. You must write a the question on a card which and give it to Eve who will take the card to Bob and return the answer to you. What must you write on the card, besides the question, to ensure Bob can encode the message so that Eve cannot read your phone number?Q陈皓:协议+数字加密Q我试想了一个,U条上可以这样写Q?#8220;BobQ请把我的手机号以MD5法加密后的字符Ԍ比对下面的字W串——XXXXXXQ它们是一L吗?”Q?/li><li>How are cookies passed in the HTTP protocol?</li><li>Design the SQL database tables for a car rental database.</li><li>Write a regular expression which matches a email address. Q陈皓:上StackOverflow查相当的问题吧。)</li><li>Write a function f(a, b) which takes two character string arguments and returns a string containing only the characters found in both strings in the order of a. Write a version which is order N-squared and one which is order N.Q陈皓:法题,不难Q不说了。一个O(n^2)和一个O(n)的算法复杂度Q?/li><li>You are given a the source to a application which is crashing when run. After running it 10 times in a debugger, you find it never crashes in the same place. The application is single threaded, and uses only the C standard library. What programming errors could be causing this crash? How would you test each one? Q陈皓:和随机数有关p?或是旉Q)</li><li>Explain how congestion control works in the TCP protocol.</li><li>In Java, what is the difference between final, finally, and finalize?</li><li>What is multithreaded programming? What is a deadlock?</li><li>Write a function (with helper functions if needed) called to Excel that takes an excel column value (A,B,C,D…AA,AB,AC,… AAA..) and returns a corresponding integer value (A=1,B=2,… AA=26..).</li><li>You have a stream of infinite queries (ie: real time Google search queries that people are entering). Describe how you would go about finding a good estimate of 1000 samples from this never ending set of data and then write code for it.</li><li>Tree search algorithms. Write BFS and DFS code, explain run time and space requirements. Modify the code to handle trees with weighted edges and loops with BFS and DFS, make the code print out path to goal state.</li><li>You are given a list of numbers. When you reach the end of the list you will come back to the beginning of the list (a circular list). Write the most efficient algorithm to find the minimum # in this list. Find any given # in the list. The numbers in the list are always increasing but you don’t know where the circular list begins, ie: 38, 40, 55, 89, 6, 13, 20, 23, 36. Q陈皓:循环排序数组的二分查N题)</li><li>Describe the data structure that is used to manage memory. (stack)</li><li>What’s the difference between local and global variables?</li><li>If you have 1 million integers, how would you sort them efficiently? (modify a specific sorting algorithm to solve this)</li><li>In Java, what is the difference between static, final, and const. (if you don’t know Java they will ask something similar for C or C++).</li><li>Talk about your class projects or work projects (pick something easy)… then describe how you could make them more efficient (in terms of algorithms).</li><li>Suppose you have an NxN matrix of positive and negative integers. Write some code that finds the sub-matrix with the maximum sum of its elements.Q陈皓:以前见过一l数l的q个问题Q现在是二维的。感觉应该是把二l的W一行的最大和的区间算出来Q然后再在这个基之上q行二维的分析。思\应该是这个,不过具体的算法还需要想一惻I</li><li>Write some code to reverse a string.</li><li>Implement division (without using the divide operator, obviously).Q陈皓:想一x除法的q程。)</li><li>Write some code to find all permutations of the letters in a particular string.</li><li>What method would you use to look up a word in a dictionary? Q陈皓:使用排序Q哈希,树等法和数据结构)</li><li>Imagine you have a closet full of shirts. It’s very hard to find a shirt. So what can you do to organize your shirts for easy retrieval?</li><li>You have eight balls all of the same size. 7 of them weigh the same, and one of them weighs slightly more. How can you fine the ball that is heavier by using a balance and only two weighings?</li><li>What is the C-language command for opening a connection with a foreign host over the internet?</li><li>Design and describe a system/application that will most efficiently produce a report of the top 1 million Google search requests. These are the particulars: 1) You are given 12 servers to work with. They are all dual-processor machines with 4Gb of RAM, 4x400GB hard drives and networked together.(Basically, nothing more than high-end PC’s) 2) The log data has already been cleaned for you. It consists of 100 Billion log lines, broken down into 12 320 GB files of 40-byte search terms per line. 3) You can use only custom written applications or available free open-source software.</li><li>There is an array A[N] of N numbers. You have to compose an array Output[N] such that Output[i] will be equal to multiplication of all the elements of A[N] except A[i]. For example Output[0] will be multiplication of A[1] to A[N-1] and Output[1] will be multiplication of A[0] and from A[2] to A[N-1]. Solve it without division operator and in O(n).Q陈皓:注意其不能用除法。算法思\是这LQ把output[i]=a[i]左边的乘U?x a[i]双的乘U,所以,我们可以分两个@环,W一ơ先把A[i]左边的乘U放在Output[i]中,W二ơ把A[i]双的乘U算出来。我们先看第一ơ的循环Q用P代篏U的方式Q代码如下:for(r=1; i=0; i<n-1; i++){ Output[i]=r; r*=a[i]; }Q看明白了吧。第二次的@环我׃说了Q方法一L。)</li><li>There is a linked list of numbers of length N. N is very large and you don’t know N. You have to write a function that will return k random numbers from the list. Numbers should be completely random. Hint: 1. Use random function rand() (returns a number between 0 and 1) and irand() (return either 0 or 1) 2. It should be done in O(n).Q陈皓:本题其实不难。在遍历链表的同时一边生成随机数Q一边记录最大的K个随机数和其链接地址。)</li><li>Find or determine non existence of a number in a sorted list of N numbers where the numbers range over M, M>> N and N large enough to span multiple disks. Algorithm to beat O(log n) bonus points for constant time algorithm.Q陈皓:使用bitmapQ如果一个长整Ş?4位,那么我们可以使用M/64个bitmapQ?/li><li>You are given a game of Tic Tac Toe. You have to write a function in which you pass the whole game and name of a player. The function will return whether the player has won the game or not. First you to decide which data structure you will use for the game. You need to tell the algorithm first and then need to write the code. Note: Some position may be blank in the game?So your data structure should consider this condition also.</li><li>You are given an array [a1 To an] and we have to construct another array [b1 To bn] where bi = a1*a2*…*an/ai. you are allowed to use only constant space and the time complexity is O(n). No divisions are allowed.Q陈皓:前面说过了)</li><li>How do you put a Binary Search Tree in an array in a efficient manner. Hint :: If the node is stored at the ith position and its children are at 2i and 2i+1(I mean level order wise)Its not the most efficient way.Q陈皓:按顺序遍历树Q?/li><li>How do you find out the fifth maximum element in an Binary Search Tree in efficient manner. Note: You should not use use any extra space. i.e sorting Binary Search Tree and storing the results in an array and listing out the fifth element.</li><li>Given a Data Structure having first n integers and next n chars. A = i1 i2 i3 … iN c1 c2 c3 … cN.Write an in-place algorithm to rearrange the elements of the array ass A = i1 c1 i2 c2 … in cnQ陈皓:q个法其实是从中间开始交换元素,代码Qfor(i=n-1; i>1; i++) {  for(j=i; j<2*n-i; j+=2) { swap(a[j], a[j+1]); } }Q不好意思写在同一行上了。)</li><li>Given two sequences of items, find the items whose absolute number increases or decreases the most when comparing one sequence with the other by reading the sequence only once.</li><li>Given That One of the strings is very very long , and the other one could be of various sizes. Windowing will result in O(N+M) solution but could it be better? May be NlogM or even better?</li><li>How many lines can be drawn in a 2D plane such that they are equidistant from 3 non-collinear points?</li><li>Let’s say you have to construct Google maps from scratch and guide a person standing on Gateway of India (Mumbai) to India Gate(Delhi). How do you do the same?</li><li>Given that you have one string of length N and M small strings of length L. How do you efficiently find the occurrence of each small string in the larger one?</li><li>Given a binary tree, programmatically you need to prove it is a binary search tree.</li><li>You are given a small sorted list of numbers, and a very very long sorted list of numbers – so long that it had to be put on a disk in different blocks. How would you find those short list numbers in the bigger one?</li><li>Suppose you have given N companies, and we want to eventually merge them into one big company. How many ways are theres to merge?</li><li>Given a file of 4 billion 32-bit integers, how to find one that appears at least twice? Q陈皓:我能惛_的是拆分成若q个数l,排序Q然后一点点归ƈhQ?/li><li>Write a program for displaying the ten most frequent words in a file such that your program should be efficient in all complexity measures.Q陈皓:你可能需要看看这文?a target="_blank"><span style="text-decoration: underline">Finding Frequent Items in Data Streams</span></a>Q?/li><li>Design a stack. We want to push, pop, and also, retrieve the minimum element in constant time.</li><li>Given a set of coin denominators, find the minimum number of coins to give a certain amount of change.Q陈皓:你应该查看一下这文章:<a target="_blank"><span style="text-decoration: underline">Coin Change Problem</span></a>Q?/li><li>Given an array, i) find the longest continuous increasing subsequence. ii) find the longest increasing subsequence.Q陈皓:q个题不难,O(n)法是边遍历边记录当前最大的q箋的长度。)</li><li>Suppose we have N companies, and we want to eventually merge them into one big company. How many ways are there to merge?</li><li>Write a function to find the middle node of a single link list. Q陈皓:我能惛_的算法是——讄两个指针p1和p2Q每一ơ,p1C步,p2C步,q样Q当p1走到最后时Qp2在中间Q?/li><li>Given two binary trees, write a compare function to check if they are equal or not. Being equal means that they have the same value and same structure.Q陈皓:q个很简单,使用递归法。)</li><li>Implement put/get methods of a fixed size cache with LRU replacement algorithm.</li><li>You are given with three sorted arrays ( in ascending order), you are required to find a triplet ( one element from each array) such that distance is minimum. Distance is defined like this : If a[i], b[j] and c[k] are three elements then distance=max(abs(a[i]-b[j]),abs(a[i]-c[k]),abs(b[j]-c[k]))” Please give a solution in O(n) time complexityQ陈皓:三个指针Qa, b, c分别指向三个数组_假设Qa[0]<b[0]<c[0]Q推qa直到a[i]>b[0]Q计?abs(a[i-1] – c[0])Q把l果保存在min中。现在情况变成找 a[i], b[0],c[0]Q重复上q过E,如果有一个新的值比min要小Q那取代现有的min。)</li><li>How does C++ deal with constructors and deconstructors of a class and its child class?</li><li>Write a function that flips the bits inside a byte (either in C++ or Java). Write an algorithm that take a list of n words, and an integer m, and retrieves the mth most frequent word in that list.</li><li>What’s 2 to the power of 64?</li><li>Given that you have one string of length N and M small strings of length L. How do you efficiently find the occurrence of each small string in the larger one? Q陈皓:我能惛_的是——把那M个小字串排个序,然后遍历大字Ԍq在那M个字串中以二分取中的方式查找。)</li><li>How do you find out the fifth maximum element in an Binary Search Tree in efficient manner.</li><li>Suppose we have N companies, and we want to eventually merge them into one big company. How many ways are there to merge?</li><li>There is linked list of millions of node and you do not know the length of it. Write a function which will return a random number from the list.</li><li>You need to check that your friend, Bob, has your correct phone number, but you cannot ask him directly. You must write a the question on a card which and give it to Eve who will take the card to Bob and return the answer to you. What must you write on the card, besides the question, to ensure Bob can encode the message so that Eve cannot read your phone number?</li><li>How long it would take to sort 1 trillion numbers? Come up with a good estimate.</li><li>Order the functions in order of their asymptotic performance: 1) 2^n 2) n^100 3) n! 4) n^n</li><li>There are some data represented by(x,y,z). Now we want to find the Kth least data. We say (x1, y1, z1) > (x2, y2, z2) when value(x1, y1, z1) > value(x2, y2, z2) where value(x,y,z) = (2^x)*(3^y)*(5^z). Now we can not get it by calculating value(x,y,z) or through other indirect calculations as lg(value(x,y,z)). How to solve it?</li><li>How many degrees are there in the angle between the hour and minute hands of a clock when the time is a quarter past three?</li><li>Given an array whose elements are sorted, return the index of a the first occurrence of a specific integer. Do this in sub-linear time. I.e. do not just go through each element searching for that element.</li><li>Given two linked lists, return the intersection of the two lists: i.e. return a list containing only the elements that occur in both of the input lists. Q陈皓:把第一个链表存入hash表,然后遍历W二个链表。不知道q没有更好的Ҏ。)</li><li>What’s the difference between a hashtable and a hashmap?</li><li>If a person dials a sequence of numbers on the telephone, what possible words/strings can be formed from the letters associated with those numbers?Q陈皓:q个问题和美国的电话有关p,大家可以试着想一下我们发短信的手机,按数字键出字母,一个组合的数学问题。)</li><li>How would you reverse the image on an n by n matrix where each pixel is represented by a bit?</li><li>Create a fast cached storage mechanism that, given a limitation on the amount of cache memory, will ensure that only the least recently used items are discarded when the cache memory is reached when inserting a new item. It supports 2 functions: String get(T t) and void put(String k, T t).</li><li>Create a cost model that allows Google to make purchasing decisions on to compare the cost of purchasing more RAM memory for their servers vs. buying more disk space.</li><li>Design an algorithm to play a game of Frogger and then code the solution. The object of the game is to direct a frog to avoid cars while crossing a busy road. You may represent a road lane via an array. Generalize the solution for an N-lane road.</li><li>What sort would you use if you had a large data set on disk and a small amount of ram to work with?</li><li>What sort would you use if you required tight max time bounds and wanted highly regular performance.</li><li>How would you store 1 million phone numbers?Q陈皓:试想电话是有区段的,可以把区D늻一保存QFlyweight设计模式Q?/li><li>Design a 2D dungeon crawling game. It must allow for various items in the maze – walls, objects, and computer-controlled characters. (The focus was on the class structures, and how to optimize the experience for the user as s/he travels through the dungeon.)</li><li>What is the size of the C structure below on a 32-bit system? On a 64-bit? Q陈皓:注意~译器的寚wQ?</li></ul> <p style="padding-left: 90px">struct foo {</p> <div style="padding-left: 90px">char a;</div> <div style="padding-left: 90px">char* b;</div> <div style="padding-left: 90px">};</div></div></div></div><img src ="http://m.shnenglu.com/Joe/aggbug/150924.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Joe/" target="_blank">simplyzhao</a> 2011-07-14 10:13 <a href="http://m.shnenglu.com/Joe/archive/2011/07/14/150924.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>扑克牌的子http://m.shnenglu.com/Joe/archive/2011/07/12/150787.htmlsimplyzhaosimplyzhaoTue, 12 Jul 2011 12:33:00 GMThttp://m.shnenglu.com/Joe/archive/2011/07/12/150787.htmlhttp://m.shnenglu.com/Joe/comments/150787.htmlhttp://m.shnenglu.com/Joe/archive/2011/07/12/150787.html#Feedback0http://m.shnenglu.com/Joe/comments/commentRss/150787.htmlhttp://m.shnenglu.com/Joe/services/trackbacks/150787.html题目来源:
http://zhedahht.blog.163.com/blog/static/25411174200951262930831/

 题目Q从扑克牌中随机?/span>5张牌Q判断是不是一个顺子,卌5张牌是不是连l的?/span>2-10为数字本w,A?/span>1Q?/span>J?/span>11Q?/span>Q?/span>12Q?/span>K?/span>13Q而大王可以看成L数字?/span>

思\一:
我们需要把扑克牌的背景抽象成计机语言。不难想象,我们可以?/span>5张牌看成?/span>5个数字组成的数组。大王是特D的数字Q我们不妨把它们都当?/span>0Q这样和其他扑克牌代表的数字׃重复了?/span>

接下来我们来分析怎样判断5个数字是不是q箋的。最直观的是Q我们把数组排序。但值得注意的是Q由?/span>0可以当成L数字Q我们可以用0去补满数l中的空~。也是排序之后的数l不是连l的Q即盔R的两个数字相隔若q个数字Q但如果我们有够的0可以补满q两个数字的I缺Q这个数l实际上q是q箋的。D个例子,数组排序之后?/span>{0Q?/span>1Q?/span>3Q?/span>4Q?/span>5}。在1?/span>3之间I缺了一?/span>2Q刚好我们有一?/span>0Q也是我们可以它当?/span>2d补这个空~?/span>

于是我们需要做三g事情Q把数组排序Q统计数l中0的个敎ͼl计排序之后的数l相L字之间的I缺L。如果空~的L于或者等?/span>0的个敎ͼ那么q个数组是q箋的;反之则不q箋。最后,我们q需要注意的是,如果数组中的?/span>0数字重复出现Q则该数l不是连l的。换成扑克牌的描q方式,是如果一副牌里含有对子,则不可能是顺子?br />
更好的思\?
1Q确?张牌中除?Q其余数字没有重复的Q可以用表统计的ҎQ?
2) 满q样的逻辑Q(maxQmin分别代表5张牌中的?以外的最大值最|
       如果没有0Q则max-min=4Q则为顺子,否则不是
       如果有一?Q则max-min=4或?Q则为顺子,否则不是
       如果有两?Q则max-min=4或?或?Q则为顺子,否则不是

最大值和最值在1Q中可以获得,q样׃用排序了




simplyzhao 2011-07-12 20:33 发表评论
]]>
C++基础?/title><link>http://m.shnenglu.com/Joe/archive/2011/06/22/149205.html</link><dc:creator>simplyzhao</dc:creator><author>simplyzhao</author><pubDate>Wed, 22 Jun 2011 11:41:00 GMT</pubDate><guid>http://m.shnenglu.com/Joe/archive/2011/06/22/149205.html</guid><wfw:comment>http://m.shnenglu.com/Joe/comments/149205.html</wfw:comment><comments>http://m.shnenglu.com/Joe/archive/2011/06/22/149205.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Joe/comments/commentRss/149205.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Joe/services/trackbacks/149205.html</trackback:ping><description><![CDATA[<div>题目来源:<br /><a >http://zhedahht.blog.163.com/blog/static/25411174201102642136998/<br /><br /></a> <p style="margin: 0in 0in 10pt"><strong><span style="font-family: '宋体', 'serif'">题目Q六Q:</span></strong><span style="font-family: '宋体', 'serif'">q行下列</span>C++<span style="font-family: '宋体', 'serif'">代码Q输Z么?</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? color: blue; font-size: 9pt">struct</span><span style="font-family: 新宋? font-size: 9pt"> Point3D</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">{</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        <span style="color: blue">int</span> x;</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        <span style="color: blue">int</span> y;</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        <span style="color: blue">int</span> z;</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">};</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"> </p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? color: blue; font-size: 9pt">int</span><span style="font-family: 新宋? font-size: 9pt"> _tmain(<span style="color: blue">int</span> argc, _TCHAR* argv[])</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">{</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        Point3D* pPoint = NULL;</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        <span style="color: blue">int</span> offset = (<span style="color: blue">int</span>)(&(pPoint)->z);</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"> </p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        printf(<span style="color: #a31515">"%d"</span>, offset);</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        <span style="color: blue">return</span> 0;</span></p> <p style="margin: 0in 0in 10pt"><span style="line-height: 115%; font-family: 新宋? font-size: 9pt">}</span></p> <p style="margin: 0in 0in 10pt"><strong><span style="font-family: '宋体', 'serif'">{案Q?/span></strong><span style="font-family: '宋体', 'serif'">输出</span>8<span style="font-family: '宋体', 'serif'">。由于在</span>pPoint->z<span style="font-family: '宋体', 'serif'">的前面加上了取地址W号Q运行到此时的时候,会在</span>pPoint<span style="font-family: '宋体', 'serif'">的指针地址上加</span>z<span style="font-family: '宋体', 'serif'">在类?/span>Point3D<span style="font-family: '宋体', 'serif'">中的偏移?/span>8<span style="font-family: '宋体', 'serif'">。由?/span>pPoint<span style="font-family: '宋体', 'serif'">的地址?/span>0<span style="font-family: '宋体', 'serif'">Q因此最l?/span>offset<span style="font-family: '宋体', 'serif'">的值是</span>8<span style="font-family: '宋体', 'serif'">?/span></p> <p style="text-indent: 0.5in; margin: 0in 0in 10pt">&(pPoint->z)<span style="font-family: '宋体', 'serif'">的语意是?/span>pPoint<span style="font-family: '宋体', 'serif'">中变?/span>z<span style="font-family: '宋体', 'serif'">的地址Q?/span>pPoint<span style="font-family: '宋体', 'serif'">的地址</span>0<span style="font-family: '宋体', 'serif'">?/span>z<span style="font-family: '宋体', 'serif'">的偏U量</span>8<span style="font-family: '宋体', 'serif'">Q,q不需要访?/span>pPoint<span style="font-family: '宋体', 'serif'">指向的内存。只要不讉K非法的内存,E序׃会出错?/span></p> <p style="margin: 0in 0in 10pt"><strong><span style="font-family: '宋体', 'serif'">题目Q七Q:</span></strong><span style="font-family: '宋体', 'serif'">q行下列</span>C++<span style="font-family: '宋体', 'serif'">代码Q输Z么?</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? color: blue; font-size: 9pt">class</span><span style="font-family: 新宋? font-size: 9pt"> A</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">{</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? color: blue; font-size: 9pt">public</span><span style="font-family: 新宋? font-size: 9pt">:</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        A()</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        {</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">                Print();</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        }</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        <span style="color: blue">virtual</span> <span style="color: blue">void</span> Print()</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        {</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">                printf(<span style="color: #a31515">"A is constructed.\n"</span>);</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        }</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">};</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"> </p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? color: blue; font-size: 9pt">class</span><span style="font-family: 新宋? font-size: 9pt"> B: <span style="color: blue">public</span> A</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">{</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? color: blue; font-size: 9pt">public</span><span style="font-family: 新宋? font-size: 9pt">:</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        B()</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        {</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">                Print();</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        }</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"> </p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        <span style="color: blue">virtual</span> <span style="color: blue">void</span> Print()</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        {</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">                printf(<span style="color: #a31515">"B is constructed.\n"</span>);</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        }</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">};</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"> </p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? color: blue; font-size: 9pt">int</span><span style="font-family: 新宋? font-size: 9pt"> _tmain(<span style="color: blue">int</span> argc, _TCHAR* argv[])</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">{</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        A* pA = <span style="color: blue">new</span> B();</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        <span style="color: blue">delete</span> pA;</span></p> <p style="line-height: normal; margin: 0in 0in 0pt"> </p> <p style="line-height: normal; margin: 0in 0in 0pt"><span style="font-family: 新宋? font-size: 9pt">        <span style="color: blue">return</span> 0;</span></p> <p style="margin: 0in 0in 10pt"><span style="line-height: 115%; font-family: 新宋? font-size: 9pt">}</span></p> <p style="margin: 0in 0in 10pt"><strong><span style="font-family: '宋体', 'serif'">{案Q?/span></strong><span style="font-family: '宋体', 'serif'">先后打印Z?/span>:A is constructed. B is constructed. <span style="font-family: '宋体', 'serif'">调用</span>B<span style="font-family: '宋体', 'serif'">的构造函数时Q先会调?/span>B<span style="font-family: '宋体', 'serif'">的基cd</span>A<span style="font-family: '宋体', 'serif'">的构造函数。然后在</span>A<span style="font-family: '宋体', 'serif'">的构造函数里调用</span>Print<span style="font-family: '宋体', 'serif'">。由于此时实例的cd</span>B<span style="font-family: '宋体', 'serif'">的部分还没有构造好Q本质上它只?/span>A<span style="font-family: '宋体', 'serif'">的一个实例,他的虚函数表指针指向的是cd</span>A<span style="font-family: '宋体', 'serif'">的虚函数表。因此此时调用的</span>Print<span style="font-family: '宋体', 'serif'">?/span>A::Print<span style="font-family: '宋体', 'serif'">Q而不?/span>B::Print<span style="font-family: '宋体', 'serif'">。接着调用cd</span>B<span style="font-family: '宋体', 'serif'">的构造函敎ͼq调?/span>Print<span style="font-family: '宋体', 'serif'">。此时已l开始构?/span>B<span style="font-family: '宋体', 'serif'">Q因此此时调用的</span>Print<span style="font-family: '宋体', 'serif'">?/span>B::Print<span style="font-family: '宋体', 'serif'">?/span></p> <p style="text-indent: 0.5in; margin: 0in 0in 10pt"><span style="font-family: '宋体', 'serif'">同样是调用虚拟函?/span>Print<span style="font-family: '宋体', 'serif'">Q我们发现在cd</span>A<span style="font-family: '宋体', 'serif'">的构造函CQ调用的?/span>A::Print<span style="font-family: '宋体', 'serif'">Q在</span>B<span style="font-family: '宋体', 'serif'">的构造函CQ调用的?/span>B::Print<span style="font-family: '宋体', 'serif'">。因此虚函数在构造函CQ已l失M虚函数的动态绑定特性?/span></p><br /><br /></div><img src ="http://m.shnenglu.com/Joe/aggbug/149205.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Joe/" target="_blank">simplyzhao</a> 2011-06-22 19:41 <a href="http://m.shnenglu.com/Joe/archive/2011/06/22/149205.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>时针打印矩?/title><link>http://m.shnenglu.com/Joe/archive/2011/06/17/148879.html</link><dc:creator>simplyzhao</dc:creator><author>simplyzhao</author><pubDate>Fri, 17 Jun 2011 12:00:00 GMT</pubDate><guid>http://m.shnenglu.com/Joe/archive/2011/06/17/148879.html</guid><wfw:comment>http://m.shnenglu.com/Joe/comments/148879.html</wfw:comment><comments>http://m.shnenglu.com/Joe/archive/2011/06/17/148879.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Joe/comments/commentRss/148879.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Joe/services/trackbacks/148879.html</trackback:ping><description><![CDATA[<div>题目来源:<br /><a >http://zhedahht.blog.163.com/blog/static/254111742010111112236313/</a><br /><br />模拟? <div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">#include</span><span style="color: #000000"><</span><span style="color: #000000">stdio.h</span><span style="color: #000000">></span><span style="color: #000000"><br /></span><span style="color: #0000ff">#define</span><span style="color: #000000"> MAX_LEN 101</span><span style="color: #000000"><br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000"><br />print_circle(</span><span style="color: #0000ff">int</span><span style="color: #000000"> (</span><span style="color: #000000">*</span><span style="color: #000000">mtrx)[MAX_LEN], </span><span style="color: #0000ff">int</span><span style="color: #000000"> leftup_x, </span><span style="color: #0000ff">int</span><span style="color: #000000"> leftup_y, </span><span style="color: #0000ff">int</span><span style="color: #000000"> rightdown_x, </span><span style="color: #0000ff">int</span><span style="color: #000000"> rightdown_y)<br />{<br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> i, j;<br />    </span><span style="color: #0000ff">if</span><span style="color: #000000">(leftup_x </span><span style="color: #000000">==</span><span style="color: #000000"> rightdown_x) {<br />        </span><span style="color: #0000ff">for</span><span style="color: #000000">(j</span><span style="color: #000000">=</span><span style="color: #000000">leftup_y; j</span><span style="color: #000000"><=</span><span style="color: #000000">rightdown_y; j</span><span style="color: #000000">++</span><span style="color: #000000">)<br />            printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\t</span><span style="color: #000000">"</span><span style="color: #000000">, mtrx[leftup_x][j]);<br />        </span><span style="color: #0000ff">return</span><span style="color: #000000">;<br />    }<br />    </span><span style="color: #0000ff">if</span><span style="color: #000000">(leftup_y </span><span style="color: #000000">==</span><span style="color: #000000"> rightdown_y) {<br />        </span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">leftup_x; i</span><span style="color: #000000"><=</span><span style="color: #000000">rightdown_x; i</span><span style="color: #000000">++</span><span style="color: #000000">)<br />            printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\t</span><span style="color: #000000">"</span><span style="color: #000000">, mtrx[i][leftup_y]);<br />        </span><span style="color: #0000ff">return</span><span style="color: #000000">;<br />    }<br /><br />    </span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">leftup_y; i</span><span style="color: #000000"><</span><span style="color: #000000">rightdown_y; i</span><span style="color: #000000">++</span><span style="color: #000000">)<br />        printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\t</span><span style="color: #000000">"</span><span style="color: #000000">, mtrx[leftup_x][i]);<br />    </span><span style="color: #0000ff">for</span><span style="color: #000000">(j</span><span style="color: #000000">=</span><span style="color: #000000">leftup_x; j</span><span style="color: #000000"><</span><span style="color: #000000">rightdown_x; j</span><span style="color: #000000">++</span><span style="color: #000000">)<br />        printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\t</span><span style="color: #000000">"</span><span style="color: #000000">, mtrx[j][rightdown_y]);<br />    </span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">rightdown_y; i</span><span style="color: #000000">></span><span style="color: #000000">leftup_y; i</span><span style="color: #000000">--</span><span style="color: #000000">)<br />        printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\t</span><span style="color: #000000">"</span><span style="color: #000000">, mtrx[rightdown_x][i]);<br />    </span><span style="color: #0000ff">for</span><span style="color: #000000">(j</span><span style="color: #000000">=</span><span style="color: #000000">rightdown_x; j</span><span style="color: #000000">></span><span style="color: #000000">leftup_x; j</span><span style="color: #000000">--</span><span style="color: #000000">)<br />        printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\t</span><span style="color: #000000">"</span><span style="color: #000000">, mtrx[j][leftup_y]);<br />}<br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000"><br />solve(</span><span style="color: #0000ff">int</span><span style="color: #000000"> (</span><span style="color: #000000">*</span><span style="color: #000000">mtrx)[MAX_LEN], </span><span style="color: #0000ff">int</span><span style="color: #000000"> width, </span><span style="color: #0000ff">int</span><span style="color: #000000"> length)<br />{<br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> lu_x, lu_y, rd_x, rd_y;<br />    lu_x </span><span style="color: #000000">=</span><span style="color: #000000"> lu_y </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br />    rd_x </span><span style="color: #000000">=</span><span style="color: #000000"> width</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;<br />    rd_y </span><span style="color: #000000">=</span><span style="color: #000000"> length</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;<br />    </span><span style="color: #0000ff">while</span><span style="color: #000000">(</span><span style="color: #000000">1</span><span style="color: #000000">) {<br />        </span><span style="color: #0000ff">if</span><span style="color: #000000">(lu_x</span><span style="color: #000000">></span><span style="color: #000000">rd_x </span><span style="color: #000000">||</span><span style="color: #000000"> lu_y</span><span style="color: #000000">></span><span style="color: #000000">rd_y)<br />            </span><span style="color: #0000ff">break</span><span style="color: #000000">;<br />        print_circle(mtrx, lu_x, lu_y, rd_x, rd_y);<br />        </span><span style="color: #000000">++</span><span style="color: #000000">lu_x;<br />        </span><span style="color: #000000">++</span><span style="color: #000000">lu_y;<br />        </span><span style="color: #000000">--</span><span style="color: #000000">rd_x;<br />        </span><span style="color: #000000">--</span><span style="color: #000000">rd_y;<br />    }<br />}<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000"><br />main(</span><span style="color: #0000ff">int</span><span style="color: #000000"> argc, </span><span style="color: #0000ff">char</span><span style="color: #000000"> </span><span style="color: #000000">**</span><span style="color: #000000">argv)<br />{<br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> i, j, length, width, matrix[MAX_LEN][MAX_LEN];<br />    scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d %d</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">&</span><span style="color: #000000">width, </span><span style="color: #000000">&</span><span style="color: #000000">length);<br />    </span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">; i</span><span style="color: #000000"><</span><span style="color: #000000">width; i</span><span style="color: #000000">++</span><span style="color: #000000">)<br />        </span><span style="color: #0000ff">for</span><span style="color: #000000">(j</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">; j</span><span style="color: #000000"><</span><span style="color: #000000">length; j</span><span style="color: #000000">++</span><span style="color: #000000">)<br />            scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">, matrix[i]</span><span style="color: #000000">+</span><span style="color: #000000">j);<br /><br />    solve(matrix, width, length);<br />    </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br />}</span></div><br /><br /></div><img src ="http://m.shnenglu.com/Joe/aggbug/148879.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Joe/" target="_blank">simplyzhao</a> 2011-06-17 20:00 <a href="http://m.shnenglu.com/Joe/archive/2011/06/17/148879.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>不用Q、-、×、h字运符做加?/title><link>http://m.shnenglu.com/Joe/archive/2011/06/17/148874.html</link><dc:creator>simplyzhao</dc:creator><author>simplyzhao</author><pubDate>Fri, 17 Jun 2011 09:30:00 GMT</pubDate><guid>http://m.shnenglu.com/Joe/archive/2011/06/17/148874.html</guid><wfw:comment>http://m.shnenglu.com/Joe/comments/148874.html</wfw:comment><comments>http://m.shnenglu.com/Joe/archive/2011/06/17/148874.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Joe/comments/commentRss/148874.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Joe/services/trackbacks/148874.html</trackback:ping><description><![CDATA[<div>题目来源:<br /><a >http://zhedahht.blog.163.com/blog/static/254111742011125100605/</a> <p style="margin: 0in 0in 10pt"><strong><span style="font-family: 宋体">题目Q写一个函敎ͼ求两个整数的之和Q要求在函数体内不得使用Q、-?#215;?#247;?/span></strong></p> <p style="margin: 0in 0in 10pt"><span style="font-family: 宋体">分析Q这又是一道考察发散思维的很有意思的题目。当我们习以为常的东西被限制使用的时候,如何H破常规L考,是解决q个问题的关键所在?/span></p> <p style="text-indent: 0.5in; margin: 0in 0in 10pt"><span style="font-family: 宋体">看到的这个题目,我的W一反应是傻gQ四则运都不能用,那还能用什么啊Q可是问题L要解决的Q只能打开思\L考各U可能性。首先我们可以分析h们是如何做十q制的加法的Q比如是如何得出</span>5+17=22<span style="font-family: 宋体">q个l果的。实际上Q我们可以分成三步的Q第一步只做各位相加不q位Q此时相加的l果?/span>12<span style="font-family: 宋体">Q个位数</span>5<span style="font-family: 宋体">?/span>7<span style="font-family: 宋体">相加不要q位?/span>2<span style="font-family: 宋体">Q十位数</span>0<span style="font-family: 宋体">?/span>1<span style="font-family: 宋体">相加l果?/span>1<span style="font-family: 宋体">Q;W二步做q位Q?/span>5+7<span style="font-family: 宋体">中有q位Q进位的值是</span>10<span style="font-family: 宋体">Q第三步把前面两个结果加hQ?/span>12+10<span style="font-family: 宋体">的结果是</span>22<span style="font-family: 宋体">Q刚?/span>5+17=22<span style="font-family: 宋体">?/span></p> <p style="text-indent: 0.5in; margin: 0in 0in 10pt"><span style="font-family: 宋体">前面我们在惻I求两C和四则运都不能用,那还能用什么啊Q对呀Q还能用什么呢Q对数字做运,除了四则q算之外Q也只剩下位运了。位q算是针对二q制的,我们也就以二q制再来分析一下前面的三步走策略对二进制是不是也管用?/span></p> <p style="text-indent: 0.5in; margin: 0in 0in 10pt">5<span style="font-family: 宋体">的二q制?/span>101<span style="font-family: 宋体">Q?/span>17<span style="font-family: 宋体">的二q制</span>10001<span style="font-family: 宋体">。还是试着把计分成三步:W一步各位相加但不计q位Q得到的l果?/span>10100<span style="font-family: 宋体">Q最后一位两个数都是</span>1<span style="font-family: 宋体">Q相加的l果是二q制?/span>10<span style="font-family: 宋体">。这一步不计进位,因此l果仍然?/span>0<span style="font-family: 宋体">Q;W二步记下进位。在q个例子中只在最后一位相加时产生一个进位,l果是二q制?/span>10<span style="font-family: 宋体">Q第三步把前两步的结果相加,得到的结果是</span>10110<span style="font-family: 宋体">Q正好是</span>22<span style="font-family: 宋体">。由此可见三步走的策略对二进制也是管用的?/span></p> <p style="text-indent: 0.5in; margin: 0in 0in 10pt"><span style="font-family: 宋体">接下来我们试着把二q制上的加法用位q算来替代。第一步不考虑q位Q对每一位相加?/span>0<span style="font-family: 宋体">?/span>0<span style="font-family: 宋体">?/span> 1<span style="font-family: 宋体">?/span>1<span style="font-family: 宋体">的结果都</span>0<span style="font-family: 宋体">Q?/span>0<span style="font-family: 宋体">?/span>1<span style="font-family: 宋体">?/span>1<span style="font-family: 宋体">?/span>0<span style="font-family: 宋体">的结果都?/span>1<span style="font-family: 宋体">。我们可以注意到Q这和异或的l果是一L。对异或而言Q?/span>0<span style="font-family: 宋体">?/span>0<span style="font-family: 宋体">?/span>1<span style="font-family: 宋体">?/span>1<span style="font-family: 宋体">异或的结果是</span>0<span style="font-family: 宋体">Q?/span>0<span style="font-family: 宋体">?/span>1<span style="font-family: 宋体">?/span>1<span style="font-family: 宋体">?/span>0<span style="font-family: 宋体">的异或结果是</span>1<span style="font-family: 宋体">。接着考虑W二步进位,?/span>0<span style="font-family: 宋体">?/span>0<span style="font-family: 宋体">?/span>0<span style="font-family: 宋体">?/span>1<span style="font-family: 宋体">?/span>1<span style="font-family: 宋体">?/span>0<span style="font-family: 宋体">而言Q都不会产生q位Q只?/span>1<span style="font-family: 宋体">?/span>1<span style="font-family: 宋体">Ӟ会向前生一个进位。此时我们可以想象成是两个数先做位与q算Q然后再向左Ud一位。只有两个数都是</span>1<span style="font-family: 宋体">的时候,位与得到的结果是</span>1<span style="font-family: 宋体">Q其余都?/span>0<span style="font-family: 宋体">。第三步把前两个步骤的结果相加。如果我们定义一个函?/span>AddWithoutArithmetic<span style="font-family: 宋体">Q第三步q当于输入前两步骤的结果来递归调用自己?/p> <div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">#include</span><span style="color: #000000"><</span><span style="color: #000000">stdio.h</span><span style="color: #000000">></span><span style="color: #000000"><br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000"><br />tricky_add(</span><span style="color: #0000ff">int</span><span style="color: #000000"> arg1, </span><span style="color: #0000ff">int</span><span style="color: #000000"> arg2)<br />{<br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> a, b;<br />    a </span><span style="color: #000000">=</span><span style="color: #000000"> arg1 </span><span style="color: #000000">^</span><span style="color: #000000"> arg2; </span><span style="color: #008000">/*</span><span style="color: #008000"> this is the result of arg1+arg2 without carry </span><span style="color: #008000">*/</span><span style="color: #000000"><br />    b </span><span style="color: #000000">=</span><span style="color: #000000"> arg1 </span><span style="color: #000000">&</span><span style="color: #000000"> arg2;<br />    b </span><span style="color: #000000"><<=</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">;<br /><br />    </span><span style="color: #0000ff">if</span><span style="color: #000000">(b </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">)<br />        </span><span style="color: #0000ff">return</span><span style="color: #000000"> a;<br />    </span><span style="color: #0000ff">else</span><span style="color: #000000"><br />        </span><span style="color: #0000ff">return</span><span style="color: #000000"> tricky_add(a, b);<br />}<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000"><br />main(</span><span style="color: #0000ff">int</span><span style="color: #000000"> argc, </span><span style="color: #0000ff">char</span><span style="color: #000000"> </span><span style="color: #000000">**</span><span style="color: #000000">argv)<br />{<br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> x, y;<br />    </span><span style="color: #0000ff">while</span><span style="color: #000000">(scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d %d</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">&</span><span style="color: #000000">x, </span><span style="color: #000000">&</span><span style="color: #000000">y) </span><span style="color: #000000">!=</span><span style="color: #000000"> EOF) {<br />        printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">, tricky_add(x, y));<br />    }<br /><br />    </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br />}</span></div> <p style="text-indent: 0.5in; margin: 0in 0in 10pt"></span></p><br /><br /></div><img src ="http://m.shnenglu.com/Joe/aggbug/148874.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Joe/" target="_blank">simplyzhao</a> 2011-06-17 17:30 <a href="http://m.shnenglu.com/Joe/archive/2011/06/17/148874.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>和ؓnq箋正数序列http://m.shnenglu.com/Joe/archive/2011/06/16/148804.htmlsimplyzhaosimplyzhaoThu, 16 Jun 2011 11:33:00 GMThttp://m.shnenglu.com/Joe/archive/2011/06/16/148804.htmlhttp://m.shnenglu.com/Joe/comments/148804.htmlhttp://m.shnenglu.com/Joe/archive/2011/06/16/148804.html#Feedback0http://m.shnenglu.com/Joe/comments/commentRss/148804.htmlhttp://m.shnenglu.com/Joe/services/trackbacks/148804.html题目来源:
http://zhedahht.blog.163.com/blog/static/25411174200732711051101/

题目Q输入一个正?/span>nQ输出所有和?/span>nq箋正数序列?/span>

例如输入15Q由?/span>1+2+3+4+5=4+5+6=7+8=15Q所以输?/span>3个连l序?/span>1-5?/span>4-6?/span>7-8?/span>

分析Q这是网易的一道面试题?/span>

q道题和本面试题pd的第10?/span>有些cM。我们用两个?/span>small?/span>big分别表示序列的最值和最大倹{首先把small初始化ؓ1Q?/span>big初始化ؓ2。如果从small?/span>big的序列的和大?/span>n的话Q我们向右移?/span>smallQ相当于从序列中L较小的数字。如果从small?/span>big的序列的和小?/span>n的话Q我们向右移?/span>bigQ相当于向序列中dbig的下一个数字。一直到small{于(1+n)/2Q因为序列至要有两个数字?/span>

Zq个思\Q我们可以写出如下代码:

void PrintContinuousSequence(int small, int big);

/////////////////////////////////////////////////////////////////////////
// Find continuous sequence, whose sum is n
/////////////////////////////////////////////////////////////////////////
void FindContinuousSequence(int n)
{
      if(n < 3)
            return;

      int small = 1; 
      int big = 2;
      int middle = (1 + n) / 2;
      int sum = small + big;

      while(small < middle)
      {
            // we are lucky and find the sequence
            if(sum == n)
                  PrintContinuousSequence(small, big);

            // if the current sum is greater than n, 
            // move small forward
            while(sum > n)
            {
                  sum -= small;
                  small ++;

                  // we are lucky and find the sequence
                  if(sum == n)
                        PrintContinuousSequence(small, big);
            }

            // move big forward
            big ++;
            sum += big;
      }
}

/////////////////////////////////////////////////////////////////////////
// Print continuous sequence between small and big
/////////////////////////////////////////////////////////////////////////
void PrintContinuousSequence(int small, int big)
{
      for(int i = small; i <= big; ++ i)
            printf("%d ", i);

      printf("\n");
}





simplyzhao 2011-06-16 19:33 发表评论
]]>
跛_阉?/title><link>http://m.shnenglu.com/Joe/archive/2011/06/12/148543.html</link><dc:creator>simplyzhao</dc:creator><author>simplyzhao</author><pubDate>Sun, 12 Jun 2011 10:19:00 GMT</pubDate><guid>http://m.shnenglu.com/Joe/archive/2011/06/12/148543.html</guid><wfw:comment>http://m.shnenglu.com/Joe/comments/148543.html</wfw:comment><comments>http://m.shnenglu.com/Joe/archive/2011/06/12/148543.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Joe/comments/commentRss/148543.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Joe/services/trackbacks/148543.html</trackback:ping><description><![CDATA[<div>题目来源:<br /><a >http://zhedahht.blog.163.com/blog/static/25411174200731844235261/</a> <p><span style="font-family: SimSun">题目Q一个台阶d?/span>n<span style="font-family: SimSun">U,如果一ơ可以蟩</span>1<span style="font-family: SimSun">U,也可以蟩</span>2<span style="font-family: SimSun">U。求d有多总蟩法,q分析算法的旉复杂度?/span></p> <p><span style="font-family: SimSun">分析Q这道题最q经常出玎ͼ包括</span>MicroStrategy<span style="font-family: SimSun">{比较重视算法的公司都曾先后选用q个q道题作为面试题或者笔试题?/span></p> <p><span style="font-family: SimSun">首先我们考虑最单的情况。如果只?/span>1<span style="font-family: SimSun">U台Ӟ那显然只有一U蟩法。如果有</span>2<span style="font-family: SimSun">U台Ӟ那就有两U蟩的方法了Q一U是分两ơ蟩Q每ơ蟩</span>1<span style="font-family: SimSun">U;另外一U就是一ơ蟩</span>2<span style="font-family: SimSun">U?/span></p> <p><span style="font-family: SimSun">现在我们再来讨论一般情c我们把</span>n<span style="font-family: SimSun">U台阶时的蟩法看成是</span>n<span style="font-family: SimSun">的函敎ͼCؓ</span>f(n)<span style="font-family: SimSun">。当</span>n>2<span style="font-family: SimSun">ӞW一ơ蟩的时候就有两U不同的选择Q一是第一ơ只?/span>1<span style="font-family: SimSun">U,此时x数目{于后面剩下?/span>n-1<span style="font-family: SimSun">U台阶的x数目Q即?/span>f(n-1)<span style="font-family: SimSun">Q另外一U选择是第一ơ蟩</span>2<span style="font-family: SimSun">U,此时x数目{于后面剩下?/span>n-2<span style="font-family: SimSun">U台阶的x数目Q即?/span>f(n-2)<span style="font-family: SimSun">。因?/span>n<span style="font-family: SimSun">U台阶时的不同蟩法的L</span>f(n)=f(n-1)+(f-2)<span style="font-family: SimSun">?/span></p> <p><span style="font-family: SimSun">我们把上面的分析用一个公式ȝ如下Q?/span></p> <p>        /  1                          n=1<br />f(n)=      2                          n=2<br />        \  f(n-1)+(f-2)               n>2</p> <p><span style="font-family: SimSun">分析到这里,怿很多人都能看是我们熟悉?/span>Fibonacci<span style="font-family: SimSun">序列?/span></p><br /><br /></div><img src ="http://m.shnenglu.com/Joe/aggbug/148543.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Joe/" target="_blank">simplyzhao</a> 2011-06-12 18:19 <a href="http://m.shnenglu.com/Joe/archive/2011/06/12/148543.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>反{链表 循环与递归http://m.shnenglu.com/Joe/archive/2011/06/12/148533.htmlsimplyzhaosimplyzhaoSun, 12 Jun 2011 08:15:00 GMThttp://m.shnenglu.com/Joe/archive/2011/06/12/148533.htmlhttp://m.shnenglu.com/Joe/comments/148533.htmlhttp://m.shnenglu.com/Joe/archive/2011/06/12/148533.html#Feedback0http://m.shnenglu.com/Joe/comments/commentRss/148533.htmlhttp://m.shnenglu.com/Joe/services/trackbacks/148533.htmlhttp://zhedahht.blog.163.com/blog/static/2541117420073471124487/

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

struct Node {
    
char value;
    
struct Node *next;
};

struct Node *
list_reverse(
struct Node *head)
{
    
struct Node *tmp, *cur, *pre = NULL;
    cur 
= head;
    
while(cur) {
        tmp 
= cur->next;
        cur
->next = pre;
        pre 
= cur;
        cur 
= tmp;
    }
    
return pre;
}

struct Node *
list_reverse_recursive(
struct Node *head)
{
    
struct Node *rv;
    
if(head && head->next) {
        rv 
= list_reverse_recursive(head->next);
        head
->next->next = head;
        head
->next = NULL;
        
return rv;
    } 
else 
        
return head;
}

void
test_print(
struct Node *head)
{
    
while(head) {
        printf(
"%c\t", head->value);
        head 
= head->next;
    }
    printf(
"\n");
}

int
main(
int argc, char **argv)
{
    
struct Node d = {'d', NULL};
    
struct Node c = {'c'&d};
    
struct Node b = {'b'&c};
    
struct Node a = {'a'&b};

    test_print(
&a);

    
struct Node *rev_first = list_reverse(&a);

    test_print(rev_first);

    
struct Node *rev_second = list_reverse_recursive(rev_first);

    test_print(rev_second);

    
return 0;
}




simplyzhao 2011-06-12 16:15 发表评论
]]>
把字W串转换成整?/title><link>http://m.shnenglu.com/Joe/archive/2011/06/10/148457.html</link><dc:creator>simplyzhao</dc:creator><author>simplyzhao</author><pubDate>Fri, 10 Jun 2011 11:46:00 GMT</pubDate><guid>http://m.shnenglu.com/Joe/archive/2011/06/10/148457.html</guid><wfw:comment>http://m.shnenglu.com/Joe/comments/148457.html</wfw:comment><comments>http://m.shnenglu.com/Joe/archive/2011/06/10/148457.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Joe/comments/commentRss/148457.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Joe/services/trackbacks/148457.html</trackback:ping><description><![CDATA[<div>题目来源:<br /><a >http://blog.163.com/prevBlogPerma.do?host=zhedahht&srl=25411174200731139971&mode=prev<br /><br /></a> <div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">#include</span><span style="color: #000000"><</span><span style="color: #000000">stdio.h</span><span style="color: #000000">></span><span style="color: #000000"><br />#include</span><span style="color: #000000"><</span><span style="color: #000000">stdlib.h</span><span style="color: #000000">></span><span style="color: #000000"><br /><br />#include</span><span style="color: #000000"><</span><span style="color: #0000ff">string</span><span style="color: #000000">.h</span><span style="color: #000000">></span><span style="color: #000000"><br />#include</span><span style="color: #000000"><</span><span style="color: #000000">limits.h</span><span style="color: #000000">></span><span style="color: #000000"><br /></span><span style="color: #008000">/*</span><span style="color: #008000"><br /> * #define INT_MAX 2147483647   <br /> * #define INT_MIN (-INT_MAX-1) <br /> </span><span style="color: #008000">*/</span><span style="color: #000000"><br /><br /></span><span style="color: #0000ff">enum</span><span style="color: #000000"> Status {<br />    Success,<br />    Fail<br />};<br /></span><span style="color: #0000ff">enum</span><span style="color: #000000"> Status ret;<br /></span><span style="color: #0000ff">int</span><span style="color: #000000"> negative;<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000"><br />Str2Int(</span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">char</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #000000">input)<br />{<br />    </span><span style="color: #0000ff">long</span><span style="color: #000000"> </span><span style="color: #0000ff">long</span><span style="color: #000000"> num </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br />    negative </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br />    ret </span><span style="color: #000000">=</span><span style="color: #000000"> Fail;<br />    </span><span style="color: #0000ff">if</span><span style="color: #000000">(input </span><span style="color: #000000">==</span><span style="color: #000000"> NULL)<br />        </span><span style="color: #0000ff">return</span><span style="color: #000000"> num;<br /><br />    </span><span style="color: #0000ff">const</span><span style="color: #000000"> </span><span style="color: #0000ff">char</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #000000">ptr </span><span style="color: #000000">=</span><span style="color: #000000"> input;<br />    </span><span style="color: #0000ff">if</span><span style="color: #000000">(</span><span style="color: #000000">*</span><span style="color: #000000">ptr</span><span style="color: #000000">==</span><span style="color: #000000">'</span><span style="color: #000000">+</span><span style="color: #000000">'</span><span style="color: #000000"> </span><span style="color: #000000">||</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #000000">ptr</span><span style="color: #000000">==</span><span style="color: #000000">'</span><span style="color: #000000">-</span><span style="color: #000000">'</span><span style="color: #000000">) {<br />        </span><span style="color: #0000ff">if</span><span style="color: #000000">(</span><span style="color: #000000">*</span><span style="color: #000000">ptr </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #000000">'</span><span style="color: #000000">-</span><span style="color: #000000">'</span><span style="color: #000000">)<br />            negative </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">;<br />        </span><span style="color: #000000">++</span><span style="color: #000000">ptr;<br />    }<br />    </span><span style="color: #0000ff">while</span><span style="color: #000000">(</span><span style="color: #000000">*</span><span style="color: #000000">ptr) {<br />        </span><span style="color: #0000ff">if</span><span style="color: #000000">(</span><span style="color: #000000">!</span><span style="color: #000000">(</span><span style="color: #000000">*</span><span style="color: #000000">ptr</span><span style="color: #000000">>=</span><span style="color: #000000">'</span><span style="color: #000000">0</span><span style="color: #000000">'</span><span style="color: #000000"> </span><span style="color: #000000">&&</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #000000">ptr</span><span style="color: #000000"><=</span><span style="color: #000000">'</span><span style="color: #000000">9</span><span style="color: #000000">'</span><span style="color: #000000">)) <br />            </span><span style="color: #0000ff">return</span><span style="color: #000000"> num;<br /><br />        </span><span style="color: #0000ff">if</span><span style="color: #000000">((</span><span style="color: #000000">!</span><span style="color: #000000">negative </span><span style="color: #000000">&&</span><span style="color: #000000"> num</span><span style="color: #000000">></span><span style="color: #000000">INT_MAX) </span><span style="color: #000000">||</span><span style="color: #000000"> (negative </span><span style="color: #000000">&&</span><span style="color: #000000"> (</span><span style="color: #000000">-</span><span style="color: #000000">num)</span><span style="color: #000000"><</span><span style="color: #000000">INT_MIN)) <br />            </span><span style="color: #0000ff">return</span><span style="color: #000000"> num;<br /><br />        num </span><span style="color: #000000">=</span><span style="color: #000000"> num</span><span style="color: #000000">*</span><span style="color: #000000">10</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> (</span><span style="color: #000000">*</span><span style="color: #000000">ptr</span><span style="color: #000000">-</span><span style="color: #000000">'</span><span style="color: #000000">0</span><span style="color: #000000">'</span><span style="color: #000000">);<br />        </span><span style="color: #000000">++</span><span style="color: #000000">ptr;<br />    }<br />    ret </span><span style="color: #000000">=</span><span style="color: #000000"> Success;<br />    </span><span style="color: #0000ff">return</span><span style="color: #000000"> num;<br />}<br /><br /></span><span style="color: #0000ff">#define</span><span style="color: #000000"> MAX_LEN 101</span><span style="color: #000000"><br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000"><br />main(</span><span style="color: #0000ff">int</span><span style="color: #000000"> argc, </span><span style="color: #0000ff">char</span><span style="color: #000000"> </span><span style="color: #000000">**</span><span style="color: #000000">argv)<br />{<br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> result;<br />    </span><span style="color: #0000ff">char</span><span style="color: #000000"> value[MAX_LEN];<br />    </span><span style="color: #0000ff">while</span><span style="color: #000000">(scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%s</span><span style="color: #000000">"</span><span style="color: #000000">, value) </span><span style="color: #000000">!=</span><span style="color: #000000"> EOF) {<br />        result </span><span style="color: #000000">=</span><span style="color: #000000"> Str2Int(value);<br />        </span><span style="color: #0000ff">if</span><span style="color: #000000">(ret </span><span style="color: #000000">==</span><span style="color: #000000"> Success)<br />            printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">, negative </span><span style="color: #000000">?</span><span style="color: #000000"> (</span><span style="color: #000000">-</span><span style="color: #000000">result) : result);<br />        </span><span style="color: #0000ff">else</span><span style="color: #000000"><br />            printf(</span><span style="color: #000000">"</span><span style="color: #000000">Invalid\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br />    }<br /><br />    </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br />}<br /></span></div><br /><br /></div><img src ="http://m.shnenglu.com/Joe/aggbug/148457.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Joe/" target="_blank">simplyzhao</a> 2011-06-10 19:46 <a href="http://m.shnenglu.com/Joe/archive/2011/06/10/148457.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>带随机指针的链表复制http://m.shnenglu.com/Joe/archive/2011/06/09/148328.htmlsimplyzhaosimplyzhaoThu, 09 Jun 2011 03:35:00 GMThttp://m.shnenglu.com/Joe/archive/2011/06/09/148328.htmlhttp://m.shnenglu.com/Joe/comments/148328.htmlhttp://m.shnenglu.com/Joe/archive/2011/06/09/148328.html#Feedback0http://m.shnenglu.com/Joe/comments/commentRss/148328.htmlhttp://m.shnenglu.com/Joe/services/trackbacks/148328.html题目来源Q?br />http://zhedahht.blog.163.com/blog/static/254111742010819104710337/

题目Q有一个复杂链表,其结炚w了有一?font face="Calibri">m_pNext指针指向下一个结点外Q还有一?/span>m_pSibling指向链表中的Ml点或?/span>NULL。其l点?/span>C++定义如下Q?/span>

                struct ComplexNode

{

    int m_nValue;

    ComplexNode* m_pNext;

    ComplexNode* m_pSibling;

};


代码Q?
#include<stdio.h>
#include
<stdlib.h>
#include
<string.h>

struct Node {
    
char value;

    
struct Node *next;
    
struct Node *random;
};

void test_print(struct Node *);

struct Node *
list_copy_with_random_pointer(
struct Node *head)
{
    
struct Node *tmp, *ptr, *ret;

    ptr 
= head;
    
while(ptr != NULL) {
        tmp 
= (struct Node *)malloc(sizeof(struct Node));
        tmp
->value = (ptr->value)-32/* from lowercase to uppercase, just for testing */
        tmp
->next = ptr->next;
        tmp
->random = NULL;

        ptr
->next = tmp;

        ptr 
= ptr->next->next;
    }

    ptr 
= head;
    
while(ptr != NULL) {
        ptr
->next->random = ptr->random==NULL ? NULL : ptr->random->next;

        ptr 
= ptr->next->next;
    }

    ptr 
= head;
    ret 
= (head==NULL ? NULL : (head->next));
    
while(ptr != NULL) {
        tmp 
= ptr->next;
        ptr
->next = ptr->next->next;
        tmp
->next = ptr->next==NULL ? NULL : ptr->next->next;

        ptr 
= ptr->next;
    }

    
return ret;
}

void
test_print(
struct Node *head)
{
    
while(head != NULL) {
        printf(
"%c: [%c, %c]\n", head->value, head->next==NULL?'-':head->next->value, head->random==NULL?'-':head->random->value);

        head 
= head->next;
    }
}

int
main(
int argc, char **argv)
{
    
struct Node d = {'d', NULL, NULL};
    
struct Node c = {'c'&d, NULL};
    
struct Node b = {'b'&c, NULL};
    
struct Node a = {'a'&b, NULL};
    a.random 
= &c;
    d.random 
= &b;

    test_print(
&a);

    
struct Node *copy = list_copy_with_random_pointer(&a);

    printf(
"\n\n");
    test_print(
&a);
    printf(
"\n\n");
    test_print(copy);

    
return 0;
}



simplyzhao 2011-06-09 11:35 发表评论
]]>
求二元查找树的镜?/title><link>http://m.shnenglu.com/Joe/archive/2011/06/01/147890.html</link><dc:creator>simplyzhao</dc:creator><author>simplyzhao</author><pubDate>Wed, 01 Jun 2011 11:58:00 GMT</pubDate><guid>http://m.shnenglu.com/Joe/archive/2011/06/01/147890.html</guid><wfw:comment>http://m.shnenglu.com/Joe/comments/147890.html</wfw:comment><comments>http://m.shnenglu.com/Joe/archive/2011/06/01/147890.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Joe/comments/commentRss/147890.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Joe/services/trackbacks/147890.html</trackback:ping><description><![CDATA[<div>题目来源Q?<a >http://blog.163.com/prevBlogPerma.do?host=zhedahht&srl=2541117420072159363370&mode=prev</a><br /><br /><span style="font-family: SimSun">题目Q输入一颗二元查找树Q将该树转换为它的镜像,卛_转换后的二元查找树中Q左子树的结炚w大于叛_树的l点。用递归和@环两U方法完成树的镜像{换?/span> <p><span style="font-family: SimSun">例如输入Q?/span></p> <p><span style="mso-spacerun: yes">     </span>8<br /><span style="mso-spacerun: yes">    </span>/<span style="mso-spacerun: yes">  </span>\<br /><span style="mso-spacerun: yes">  </span>6<span style="mso-spacerun: yes">      </span>10<br /> /\<span style="mso-spacerun: yes">       </span>/\<br />5<span style="mso-spacerun: yes">  </span>7<span style="mso-spacerun: yes">    </span>9<span style="mso-spacerun: yes">   </span>11</p> <p><span style="font-family: SimSun">输出Q?/span></p> <p><span style="mso-spacerun: yes">      </span>8<br /><span style="mso-spacerun: yes">    </span>/<span style="mso-spacerun: yes">  </span>\<br /><span style="mso-spacerun: yes">  </span>10 <span style="mso-spacerun: yes">   </span>6<br /> /\ <span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes">  </span><span style="mso-spacerun: yes">  </span>/\<br />11<span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes"> </span>9  7<span style="mso-spacerun: yes">  </span>5</p> <div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">#include</span><span style="color: #000000"><</span><span style="color: #000000">stdio.h</span><span style="color: #000000">></span><span style="color: #000000"><br />#include</span><span style="color: #000000"><</span><span style="color: #000000">stdlib.h</span><span style="color: #000000">></span><span style="color: #000000"><br />#include</span><span style="color: #000000"><</span><span style="color: #0000ff">string</span><span style="color: #000000">.h</span><span style="color: #000000">></span><span style="color: #000000"><br /><br /></span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node {<br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> value;<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node </span><span style="color: #000000">*</span><span style="color: #000000">left;<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node </span><span style="color: #000000">*</span><span style="color: #000000">right;<br />};<br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000"><br />bst_preorder(</span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node </span><span style="color: #000000">*</span><span style="color: #000000">root)<br />{<br />    </span><span style="color: #0000ff">if</span><span style="color: #000000">(root </span><span style="color: #000000">==</span><span style="color: #000000"> NULL)<br />        </span><span style="color: #0000ff">return</span><span style="color: #000000">;<br /><br />    printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\t</span><span style="color: #000000">"</span><span style="color: #000000">, root</span><span style="color: #000000">-></span><span style="color: #000000">value);<br />    bst_preorder(root</span><span style="color: #000000">-></span><span style="color: #000000">left);<br />    bst_preorder(root</span><span style="color: #000000">-></span><span style="color: #000000">right);<br />}<br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000"><br />bst_mirror_recursive(</span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node </span><span style="color: #000000">*</span><span style="color: #000000">root) </span><span style="color: #008000">/*</span><span style="color: #008000"> easy </span><span style="color: #008000">*/</span><span style="color: #000000"><br />{<br />    </span><span style="color: #0000ff">if</span><span style="color: #000000">(root </span><span style="color: #000000">==</span><span style="color: #000000"> NULL)<br />        </span><span style="color: #0000ff">return</span><span style="color: #000000">;<br /><br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node </span><span style="color: #000000">*</span><span style="color: #000000">ptr </span><span style="color: #000000">=</span><span style="color: #000000"> root</span><span style="color: #000000">-></span><span style="color: #000000">left;<br />    root</span><span style="color: #000000">-></span><span style="color: #000000">left </span><span style="color: #000000">=</span><span style="color: #000000"> root</span><span style="color: #000000">-></span><span style="color: #000000">right;<br />    root</span><span style="color: #000000">-></span><span style="color: #000000">right </span><span style="color: #000000">=</span><span style="color: #000000"> ptr;<br /><br />    bst_mirror_recursive(root</span><span style="color: #000000">-></span><span style="color: #000000">left);<br />    bst_mirror_recursive(root</span><span style="color: #000000">-></span><span style="color: #000000">right);<br />}<br /><br /></span><span style="color: #008000">/*</span><span style="color: #008000"> STACK : naive </span><span style="color: #008000">*/</span><span style="color: #000000"><br /></span><span style="color: #0000ff">#define</span><span style="color: #000000"> STACK_SIZE 101</span><span style="color: #000000"><br /></span><span style="color: #0000ff">struct</span><span style="color: #000000"> Stack {<br />    </span><span style="color: #0000ff">void</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #000000">data[STACK_SIZE];<br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> top;<br />};<br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000"><br />stack_pop(</span><span style="color: #0000ff">struct</span><span style="color: #000000"> Stack </span><span style="color: #000000">*</span><span style="color: #000000">stack)<br />{<br />    </span><span style="color: #0000ff">if</span><span style="color: #000000">((stack</span><span style="color: #000000">-></span><span style="color: #000000">top) </span><span style="color: #000000">>=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">)<br />        </span><span style="color: #000000">--</span><span style="color: #000000">(stack</span><span style="color: #000000">-></span><span style="color: #000000">top);<br />}<br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #000000"><br />stack_top(</span><span style="color: #0000ff">struct</span><span style="color: #000000"> Stack </span><span style="color: #000000">*</span><span style="color: #000000">stack)<br />{<br />    </span><span style="color: #0000ff">if</span><span style="color: #000000">((stack</span><span style="color: #000000">-></span><span style="color: #000000">top) </span><span style="color: #000000">>=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">)<br />        </span><span style="color: #0000ff">return</span><span style="color: #000000"> stack</span><span style="color: #000000">-></span><span style="color: #000000">data[stack</span><span style="color: #000000">-></span><span style="color: #000000">top];<br /><br />    </span><span style="color: #0000ff">return</span><span style="color: #000000"> NULL;<br />}<br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000"><br />stack_push(</span><span style="color: #0000ff">struct</span><span style="color: #000000"> Stack </span><span style="color: #000000">*</span><span style="color: #000000">stack, </span><span style="color: #0000ff">void</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #000000">entity)<br />{<br />    stack</span><span style="color: #000000">-></span><span style="color: #000000">data[</span><span style="color: #000000">++</span><span style="color: #000000">(stack</span><span style="color: #000000">-></span><span style="color: #000000">top)] </span><span style="color: #000000">=</span><span style="color: #000000"> entity;<br />}<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000"><br />stack_isempty(</span><span style="color: #0000ff">struct</span><span style="color: #000000"> Stack </span><span style="color: #000000">*</span><span style="color: #000000">stack)<br />{<br />    </span><span style="color: #0000ff">return</span><span style="color: #000000"> (stack</span><span style="color: #000000">-></span><span style="color: #000000">top) </span><span style="color: #000000"><</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br />}<br /><br /></span><span style="color: #0000ff">void</span><span style="color: #000000"><br />bst_mirror_nonrecursive(</span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node </span><span style="color: #000000">*</span><span style="color: #000000">root, </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Stack </span><span style="color: #000000">*</span><span style="color: #000000">aux_stack) </span><span style="color: #008000">/*</span><span style="color: #008000"> stack used : good method </span><span style="color: #008000">*/</span><span style="color: #000000"><br />{<br />    stack_push(aux_stack, root);<br />    </span><span style="color: #0000ff">while</span><span style="color: #000000">(</span><span style="color: #000000">!</span><span style="color: #000000">stack_isempty(aux_stack)) {<br />        </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node </span><span style="color: #000000">*</span><span style="color: #000000">node </span><span style="color: #000000">=</span><span style="color: #000000"> (</span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node </span><span style="color: #000000">*</span><span style="color: #000000">)stack_top(aux_stack);<br /><br />        </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node </span><span style="color: #000000">*</span><span style="color: #000000">ptr </span><span style="color: #000000">=</span><span style="color: #000000"> node</span><span style="color: #000000">-></span><span style="color: #000000">left;<br />        node</span><span style="color: #000000">-></span><span style="color: #000000">left </span><span style="color: #000000">=</span><span style="color: #000000"> node</span><span style="color: #000000">-></span><span style="color: #000000">right;<br />        node</span><span style="color: #000000">-></span><span style="color: #000000">right </span><span style="color: #000000">=</span><span style="color: #000000"> ptr;<br /><br />        stack_pop(aux_stack);<br />        </span><span style="color: #0000ff">if</span><span style="color: #000000">(node</span><span style="color: #000000">-></span><span style="color: #000000">left)<br />            stack_push(aux_stack, node</span><span style="color: #000000">-></span><span style="color: #000000">left);<br />        </span><span style="color: #0000ff">if</span><span style="color: #000000">(node</span><span style="color: #000000">-></span><span style="color: #000000">right)<br />            stack_push(aux_stack, node</span><span style="color: #000000">-></span><span style="color: #000000">right);<br />    }<br />}<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000"><br />main(</span><span style="color: #0000ff">int</span><span style="color: #000000"> argc, </span><span style="color: #0000ff">char</span><span style="color: #000000"> </span><span style="color: #000000">**</span><span style="color: #000000">argv)<br />{<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node a </span><span style="color: #000000">=</span><span style="color: #000000"> {</span><span style="color: #000000">5</span><span style="color: #000000">, NULL, NULL};<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node b </span><span style="color: #000000">=</span><span style="color: #000000"> {</span><span style="color: #000000">7</span><span style="color: #000000">, NULL, NULL};<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node c </span><span style="color: #000000">=</span><span style="color: #000000"> {</span><span style="color: #000000">9</span><span style="color: #000000">, NULL, NULL};<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node d </span><span style="color: #000000">=</span><span style="color: #000000"> {</span><span style="color: #000000">11</span><span style="color: #000000">, NULL, NULL};<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node e </span><span style="color: #000000">=</span><span style="color: #000000"> {</span><span style="color: #000000">6</span><span style="color: #000000">, </span><span style="color: #000000">&</span><span style="color: #000000">a, </span><span style="color: #000000">&</span><span style="color: #000000">b};<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node f </span><span style="color: #000000">=</span><span style="color: #000000"> {</span><span style="color: #000000">10</span><span style="color: #000000">, </span><span style="color: #000000">&</span><span style="color: #000000">c, </span><span style="color: #000000">&</span><span style="color: #000000">d};<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node g </span><span style="color: #000000">=</span><span style="color: #000000"> {</span><span style="color: #000000">8</span><span style="color: #000000">, </span><span style="color: #000000">&</span><span style="color: #000000">e, </span><span style="color: #000000">&</span><span style="color: #000000">f};<br /><br />    bst_preorder(</span><span style="color: #000000">&</span><span style="color: #000000">g);<br />    printf(</span><span style="color: #000000">"</span><span style="color: #000000">\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br />    bst_mirror_recursive(</span><span style="color: #000000">&</span><span style="color: #000000">g);<br />    bst_preorder(</span><span style="color: #000000">&</span><span style="color: #000000">g);<br />    printf(</span><span style="color: #000000">"</span><span style="color: #000000">\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /><br />    bst_mirror_recursive(</span><span style="color: #000000">&</span><span style="color: #000000">g);<br />    bst_preorder(</span><span style="color: #000000">&</span><span style="color: #000000">g);<br />    printf(</span><span style="color: #000000">"</span><span style="color: #000000">\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Stack aux </span><span style="color: #000000">=</span><span style="color: #000000"> {{</span><span style="color: #000000">0</span><span style="color: #000000">}, </span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">};<br />    bst_mirror_nonrecursive(</span><span style="color: #000000">&</span><span style="color: #000000">g, </span><span style="color: #000000">&</span><span style="color: #000000">aux);<br />    bst_preorder(</span><span style="color: #000000">&</span><span style="color: #000000">g);<br />    printf(</span><span style="color: #000000">"</span><span style="color: #000000">\n</span><span style="color: #000000">"</span><span style="color: #000000">);<br /><br />    </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br />}<br /></span></div> <p> </p><br /><br /><br /></div><img src ="http://m.shnenglu.com/Joe/aggbug/147890.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Joe/" target="_blank">simplyzhao</a> 2011-06-01 19:58 <a href="http://m.shnenglu.com/Joe/archive/2011/06/01/147890.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>判断二叉树是不是q?/title><link>http://m.shnenglu.com/Joe/archive/2011/05/31/147762.html</link><dc:creator>simplyzhao</dc:creator><author>simplyzhao</author><pubDate>Tue, 31 May 2011 08:58:00 GMT</pubDate><guid>http://m.shnenglu.com/Joe/archive/2011/05/31/147762.html</guid><wfw:comment>http://m.shnenglu.com/Joe/comments/147762.html</wfw:comment><comments>http://m.shnenglu.com/Joe/archive/2011/05/31/147762.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Joe/comments/commentRss/147762.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Joe/services/trackbacks/147762.html</trackback:ping><description><![CDATA[<div>题目来源: <a >http://zhedahht.blog.163.com/blog/static/25411174201142733927831/</a><br /><br /> <p style="text-indent: 0.5in; margin: 0in 0in 10pt"><font size="3"><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri" lang="ZH-CN"><span style="line-height: 115%; font-family: 宋体; font-size: 11pt; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA" lang="ZH-CN">题目Q?/span><strong>输入一二叉树的根l点Q判断该树是不是q二叉树。如果某二叉树中Ll点的左叛_树的深度相差不超q?/strong></span><font face="Calibri"><strong>1</strong></font><span><strong>Q那么它是一^衡二叉树?br /></strong></span><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri" lang="ZH-CN"><br /></p> <div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">#include</span><span style="color: #000000"><</span><span style="color: #000000">stdio.h</span><span style="color: #000000">></span><span style="color: #000000"><br />#include</span><span style="color: #000000"><</span><span style="color: #000000">stdlib.h</span><span style="color: #000000">></span><span style="color: #000000"><br />#include</span><span style="color: #000000"><</span><span style="color: #0000ff">string</span><span style="color: #000000">.h</span><span style="color: #000000">></span><span style="color: #000000"><br /></span><span style="color: #0000ff">#define</span><span style="color: #000000"> MAX(a, b) ((a)>(b) ? (a) : (b))</span><span style="color: #000000"><br /></span><span style="color: #0000ff">#define</span><span style="color: #000000"> ABS(x) ((x)>=0 ? (x) : ((x)*(-1)))</span><span style="color: #000000"><br /><br /></span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node {<br />    </span><span style="color: #0000ff">void</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #000000">data;<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node </span><span style="color: #000000">*</span><span style="color: #000000">left, </span><span style="color: #000000">*</span><span style="color: #000000">right;<br />};<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000"><br />depth(</span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node </span><span style="color: #000000">*</span><span style="color: #000000">root)<br />{<br />    </span><span style="color: #0000ff">if</span><span style="color: #000000">(root </span><span style="color: #000000">==</span><span style="color: #000000"> NULL)<br />        </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br /><br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> ldepth </span><span style="color: #000000">=</span><span style="color: #000000"> depth(root</span><span style="color: #000000">-></span><span style="color: #000000">left);<br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> rdepth </span><span style="color: #000000">=</span><span style="color: #000000"> depth(root</span><span style="color: #000000">-></span><span style="color: #000000">right);<br /><br />    </span><span style="color: #0000ff">return</span><span style="color: #000000"> MAX(ldepth, rdepth) </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">;<br />}<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000"><br />btree_isbalanced_naive(</span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node </span><span style="color: #000000">*</span><span style="color: #000000">root) </span><span style="color: #008000">/*</span><span style="color: #008000"> return 1 if balanced, or return 0 </span><span style="color: #008000">*/</span><span style="color: #000000"><br />{<br />    </span><span style="color: #0000ff">if</span><span style="color: #000000">(root </span><span style="color: #000000">==</span><span style="color: #000000"> NULL)<br />        </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">;<br /><br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> ldepth </span><span style="color: #000000">=</span><span style="color: #000000"> depth(root</span><span style="color: #000000">-></span><span style="color: #000000">left);<br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> rdepth </span><span style="color: #000000">=</span><span style="color: #000000"> depth(root</span><span style="color: #000000">-></span><span style="color: #000000">right);<br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> diff </span><span style="color: #000000">=</span><span style="color: #000000"> ldepth </span><span style="color: #000000">-</span><span style="color: #000000"> rdepth;<br />    </span><span style="color: #0000ff">if</span><span style="color: #000000">(ABS(diff) </span><span style="color: #000000">></span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">)<br />        </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br /><br />    </span><span style="color: #0000ff">return</span><span style="color: #000000"> (btree_isbalanced_naive(root</span><span style="color: #000000">-></span><span style="color: #000000">left) </span><span style="color: #000000">&&</span><span style="color: #000000"> btree_isbalanced_naive(root</span><span style="color: #000000">-></span><span style="color: #000000">right));<br />}<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000"><br />btree_isbalanced(</span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node </span><span style="color: #000000">*</span><span style="color: #000000">root, </span><span style="color: #0000ff">int</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #000000">depth)<br />{<br />    </span><span style="color: #0000ff">if</span><span style="color: #000000">(root </span><span style="color: #000000">==</span><span style="color: #000000"> NULL) {<br />        </span><span style="color: #000000">*</span><span style="color: #000000">depth </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br />        </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">;<br />    }<br /><br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> ldepth, rdepth, diff;<br />    </span><span style="color: #0000ff">if</span><span style="color: #000000">(</span><span style="color: #000000">!</span><span style="color: #000000">btree_isbalanced(root</span><span style="color: #000000">-></span><span style="color: #000000">left, </span><span style="color: #000000">&</span><span style="color: #000000">ldepth) </span><span style="color: #000000">||</span><span style="color: #000000"> </span><span style="color: #000000">!</span><span style="color: #000000">btree_isbalanced(root</span><span style="color: #000000">-></span><span style="color: #000000">right, </span><span style="color: #000000">&</span><span style="color: #000000">rdepth))<br />        </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br /><br />    </span><span style="color: #000000">*</span><span style="color: #000000">depth </span><span style="color: #000000">=</span><span style="color: #000000"> MAX(ldepth, rdepth) </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">;<br />    diff </span><span style="color: #000000">=</span><span style="color: #000000"> ldepth </span><span style="color: #000000">-</span><span style="color: #000000"> rdepth;<br />    </span><span style="color: #0000ff">return</span><span style="color: #000000"> (ABS(diff)</span><span style="color: #000000"><=</span><span style="color: #000000">1</span><span style="color: #000000"> </span><span style="color: #000000">?</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000"> : </span><span style="color: #000000">0</span><span style="color: #000000">);<br />}<br /><br /></span><span style="color: #0000ff">int</span><span style="color: #000000"><br />main(</span><span style="color: #0000ff">int</span><span style="color: #000000"> argc, </span><span style="color: #0000ff">char</span><span style="color: #000000"> </span><span style="color: #000000">**</span><span style="color: #000000">argv)<br />{<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node a </span><span style="color: #000000">=</span><span style="color: #000000"> {NULL, NULL, NULL};<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node b </span><span style="color: #000000">=</span><span style="color: #000000"> {NULL, NULL, NULL};<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node c </span><span style="color: #000000">=</span><span style="color: #000000"> {NULL, NULL ,NULL};<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node d </span><span style="color: #000000">=</span><span style="color: #000000"> {NULL, </span><span style="color: #000000">&</span><span style="color: #000000">b, NULL};<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node e </span><span style="color: #000000">=</span><span style="color: #000000"> {NULL, </span><span style="color: #000000">&</span><span style="color: #000000">a, </span><span style="color: #000000">&</span><span style="color: #000000">d};<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node f </span><span style="color: #000000">=</span><span style="color: #000000"> {NULL, NULL, NULL};<br />    </span><span style="color: #0000ff">struct</span><span style="color: #000000"> Node g </span><span style="color: #000000">=</span><span style="color: #000000"> {NULL, </span><span style="color: #000000">&</span><span style="color: #000000">e, </span><span style="color: #000000">&</span><span style="color: #000000">f};<br /><br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> ret1 </span><span style="color: #000000">=</span><span style="color: #000000"> btree_isbalanced_naive(</span><span style="color: #000000">&</span><span style="color: #000000">g);<br />    printf(</span><span style="color: #000000">"</span><span style="color: #000000">%s\n</span><span style="color: #000000">"</span><span style="color: #000000">, ret1 </span><span style="color: #000000">?</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">YES</span><span style="color: #000000">"</span><span style="color: #000000"> : </span><span style="color: #000000">"</span><span style="color: #000000">NO</span><span style="color: #000000">"</span><span style="color: #000000">);<br /><br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> dpth </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br />    </span><span style="color: #0000ff">int</span><span style="color: #000000"> ret2 </span><span style="color: #000000">=</span><span style="color: #000000"> btree_isbalanced(</span><span style="color: #000000">&</span><span style="color: #000000">g, </span><span style="color: #000000">&</span><span style="color: #000000">dpth);<br />    printf(</span><span style="color: #000000">"</span><span style="color: #000000">%s : %d\n</span><span style="color: #000000">"</span><span style="color: #000000">, ret2 </span><span style="color: #000000">?</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">YES</span><span style="color: #000000">"</span><span style="color: #000000"> : </span><span style="color: #000000">"</span><span style="color: #000000">NO</span><span style="color: #000000">"</span><span style="color: #000000">, dpth);<br />    </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">;<br />}</span></div> <p style="text-indent: 0.5in; margin: 0in 0in 10pt"></span></font></p><span style="line-height: 115%; font-family: 'Calibri', 'sans-serif'; font-size: 11pt; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"></span><font size="3"><span style="font-family: 宋体; mso-ascii-font-family: Calibri; mso-hansi-font-family: Calibri" lang="ZH-CN"> <p align="center"> </p></span></font><br /><br /></div><img src ="http://m.shnenglu.com/Joe/aggbug/147762.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Joe/" target="_blank">simplyzhao</a> 2011-05-31 16:58 <a href="http://m.shnenglu.com/Joe/archive/2011/05/31/147762.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>设计包含min函数的栈http://m.shnenglu.com/Joe/archive/2011/05/25/147091.htmlsimplyzhaosimplyzhaoWed, 25 May 2011 07:49:00 GMThttp://m.shnenglu.com/Joe/archive/2011/05/25/147091.htmlhttp://m.shnenglu.com/Joe/comments/147091.htmlhttp://m.shnenglu.com/Joe/archive/2011/05/25/147091.html#Feedback0http://m.shnenglu.com/Joe/comments/commentRss/147091.htmlhttp://m.shnenglu.com/Joe/services/trackbacks/147091.html题目出处: http://zhedahht.blog.163.com/blog/static/25411174200712895228171/

题目Q定义栈的数据结构,要求d一?/span>min函数Q能够得到栈的最元素。要求函?/span>min?/span>push以及pop的时间复杂度都是O(1)?/span>
#include "StackWithMin.h"
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>

const int StackWithMin::MAX_SIZE ;

StackWithMin::StackWithMin() :
    top_index(
-1), min_index(-1)
{
    printf(
"StackWithMin Constructor\n");
}

StackWithMin::
~StackWithMin()
{
    printf(
"StackWithMin Destructor\n");
}

int
StackWithMin::top() 
const
{
    
if(top_index == -1) {
        printf(
"top() failed: Stack Empty\n");
        
return -1;
    }

    
return stack[top_index]; 
}

int
StackWithMin::get_min() 
const
{
    
if(min_index == -1) {
        printf(
"get_min() failed: Stack Empty\n");
        
return -1;
    }

    
return stack[min_index];
}

void
StackWithMin::push(
int value)
{
    
if(top_index == -1) { /* stack empty */
        stack[
++top_index] = value;
        min_index 
= top_index;
        
return;
    }
    stack[
++top_index] = value;
    
if(value < stack[min_index]) {
        index[top_index] 
= min_index;
        min_index 
= top_index;
    }
}

int
StackWithMin::pop()
{
    
if(top_index == -1) {
        printf(
"pop() failed: Stack Empty\n");
        
return -1;
    }
    
int ret = stack[top_index];
    
if(min_index == top_index)
        min_index 
= index[top_index];
    
--top_index;
    
return ret;
}



class StackWithMin
{
    public:
        StackWithMin();
        ~StackWithMin();
        int get_min() const;
        int top() const;
        void push(int value);
        int pop();      
    private:
        static const int MAX_SIZE = 101;
        int top_index, min_index;
        int stack[MAX_SIZE];
        int index[MAX_SIZE];
};











simplyzhao 2011-05-25 15:49 发表评论
]]>
把二元查找树转变成排序的双向链表http://m.shnenglu.com/Joe/archive/2011/05/23/146954.htmlsimplyzhaosimplyzhaoMon, 23 May 2011 01:09:00 GMThttp://m.shnenglu.com/Joe/archive/2011/05/23/146954.htmlhttp://m.shnenglu.com/Joe/comments/146954.htmlhttp://m.shnenglu.com/Joe/archive/2011/05/23/146954.html#Feedback0http://m.shnenglu.com/Joe/comments/commentRss/146954.htmlhttp://m.shnenglu.com/Joe/services/trackbacks/146954.html题目出处: http://zhedahht.blog.163.com/

题目Q输入一二元查找树Q将该二元查找树转换成一个排序的双向链表。要求不能创ZQ何新的结点,只调整指针的指向?

  比如二元查找树
   
                                        10
                                          /    \
                                        6       14
                                      /  \     /  \
                                    4     8  12    16
转换成双向链?/span>

4=6=8=10=12=14=16?/span>


思\Q递归Q在一时找不到递归的灵感的时候,多考虑考虑递归的参敎ͼ有时更重要的是考虑递归的返回?br />      每处理一个节点,首先获取左子树和叛_树所q回的链表,然后拼接

代码Q?
#include<stdio.h>
#include
<string.h>
#include
<stdlib.h>

/* Problem: Convert a binary search tree into a sorted linkedlist */
/* When it comes to Tree-Structure, recursion is always the most common solution.
   When designing recursion solution, should consider:
       1. the parameters
       2(important). the return object
*/

struct Node {
    
int value;
    
struct Node *left;
    
struct Node *right;
};

struct Node *
BTree2List(
struct Node *root)
{
    
if(root == NULL)
        
return NULL;
    
struct Node *ret = NULL;

    
/* Convert the left tree into a sorted linkedlist */
    
struct Node *l_linkedlist = BTree2List(root->left);
    ret 
= l_linkedlist==NULL ? root : l_linkedlist;

    
/* Convert the right tree into a sorted linkedlist */
    
struct Node *r_linkedlist = BTree2List(root->right);
    
while(l_linkedlist && l_linkedlist->right)
        l_linkedlist 
= l_linkedlist->right;

    
/* Combine */
    
if(l_linkedlist)
        l_linkedlist
->right = root;
    root
->left = l_linkedlist;
    root
->right = r_linkedlist;
    
if(r_linkedlist)
        r_linkedlist
->left = root;
    
    
return ret;
}

int main(int argc, char** argv)
{
    
struct Node a = {4, NULL, NULL};
    
struct Node b = {8, NULL, NULL};
    
struct Node c = {12, NULL, NULL};
    
struct Node d = {16, NULL, NULL};
    
struct Node e = {6, NULL, &b};
    
struct Node f = {14&c, NULL};
    
struct Node g = {10&e, &f};

    
struct Node *ret = BTree2List(&g);
    
while(ret && ret->right) {
        ret 
= ret->right;
    }

    
while(ret) {
        printf(
"%d\n", ret->value);
        ret 
= ret->left;
    }

    
return 0;
}




simplyzhao 2011-05-23 09:09 发表评论
]]>
99999þþþþ| þһٸ | ƷŮþþþ| ŵþ| þþþþþƷþþþ| þþþavۺϲҰ| 99þɫĻ| 97Ʒþ찴Ħ| þùƷҰAV | Ļav鲻þ | þŷձƷ| þƬѹۿ| þþƷAVɫ| þҹɫƷAV | þԭƷ| þSEƷһ| ˾Ʒһþþ| þSEƷһ| ɫþþ99Ʒ91| ˾þþƷ鶹| þۺϾɫۺϾ99| 㽶þavһ| þþþŮۺ | þþƷƷapp| þ¶ݺɫ| þþƷŷպ| 99þerֻоƷ18| þþþþҹƷ| þ99Ʒþþþþ| ھƷ˾þþþAVӰԺ| ԾþþӰԺ| þƵһ| ѾƷþþþþĻ| 91þþƷƵ| ԭ1769þѲ| ھƷ˾þþþAVӰԺ| þþþѿӰƬ| ھƷþþþþþþõӰ | þѾƷһ| ޾Ʒþ| ˳ŷþ|