摘要: 长度为n的数组乱序存放着0至n-1. 现在只能进行0与其他数的swap 请设计并实现排序。google笔试小题。题目来源:http://wenku.baidu.com/view/5aa818dda58da0116c17498b.html休闲小题。2个key一个是只能与0 swap,另一个是数组的下标和值是一一对应的。第二个容易被忽略。所以读到一个元素时,如果值和下标不等,那么可以直接把这个元素的值放到正确位置上去,目标位置的值挪回来。当然这个过程要借助元素0来完成。假设输入是 2,0,3,1step 1遍历数组,找出值为0的元素,和num[0]交换0 2 3 1step 2如果num[1]下标 阅读全文
posted @ 2013-12-20 20:44 XIAOSHUA 阅读(2289) 评论(0) 推荐(0) 编辑
摘要: 题目是:查看以下代码,问结果是什么?结果是打印出“array:16345678910”吗?#include "stdafx.h"#include using namespace std;#define SUB(x,y) x-y#define ACCESS_BEFORE(element,offset,value) *SUB(&element, offset) =valueint _tmain(int argc, _TCHAR* argv[]){ int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int i; ACCE 阅读全文
posted @ 2013-12-20 11:07 XIAOSHUA 阅读(257) 评论(0) 推荐(0) 编辑
摘要: 比如:1(头)->2->2->3->3->1->1(头) 去除以后的结果是1->2->3,注意头尾的1也要去掉一个。#include "stdafx.h"#include using namespace std;struct tnode{ tnode* next; int value;};tnode* unique(tnode *head){ tnode* p = head; while (p->next != head) { if (p->next->value == p->value) { tnod 阅读全文
posted @ 2013-12-18 20:46 XIAOSHUA 阅读(456) 评论(0) 推荐(0) 编辑
摘要: // CPP.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include#include#includeusing namespace std;int main(){ int n, i; cout > n; if (n = i) { if (n % i == 0) { cout << i << '*'; n /= i; val = sqrt(n); } ... 阅读全文
posted @ 2013-12-16 23:21 XIAOSHUA 阅读(3777) 评论(0) 推荐(0) 编辑
摘要: // CPP.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include using namespace std;bool IsPrime(const int a){ bool flag = true; int b = a / 2; // 这里是a除以2,没有必要计算更大的值 while (b >= 2){ if (a % b == 0){ flag = false; break; } else{ b--; ... 阅读全文
posted @ 2013-12-10 21:49 XIAOSHUA 阅读(293) 评论(0) 推荐(0) 编辑
摘要: 1、深度优先遍历templatevoid DFS(Graph& G, const T& v){ int i, loc, n=G.NumberOfVerticese(); bool* visited = new bool[n]; for(i=0;ivoid DFS(Graph& G, int v, bool visited[]){ coutvoid BFS(Graph& G, const T& v){ int i, w, n=G.NumberOfVertices(); bool* visited = new bool[n]; for(i=0;i Q; .. 阅读全文
posted @ 2013-12-10 20:53 XIAOSHUA 阅读(255) 评论(0) 推荐(0) 编辑
摘要: Java全排列算法: 第一遍循环:将list数组index==0的元素依次与数组的每个元素交换,从而保证index==0的位置先后出现n个不同元素之一,实现对index==0位置的遍历。 第 i 遍循环:通过交换, 使index==i的元素,依次与index 为[ i, length-1] 的元素交 阅读全文
posted @ 2013-12-09 10:35 XIAOSHUA 阅读(8008) 评论(0) 推荐(0) 编辑
摘要: int *a=new int[10]; ...... delete []a; 后面“delete []a;”出现错误的情况大致有: 1 数组的首地址a被你有意无意更改了,如:a++之类的; 2 变量的作用域问题,如:你是在一个函数体中new的,却在另一个函数体中delete,这时的a变量已经失效了,这时delete会出现a为不认识的标识符的错误; 3 先前已经delete过一次了,不可以delete两次; 4、内存访问越界,VC分配内存,除了分配你需要的内存空间外还会分配一些管理单元,就在你分配空间的上界和下界,比如内存边界标志(0xFDFDFDFD),如果是数组在上界会记录数... 阅读全文
posted @ 2013-12-09 09:54 XIAOSHUA 阅读(568) 评论(0) 推荐(0) 编辑
摘要: 一个C++函数,如果没有函数声明而只有函数定义,程序照样运行,但要求这个函数定义必须放在main函数之前,否则编译按照从上到下的顺序扫描下来,就会出现编译器不认识它的情况。 如果一个程序同时有函数声明和函数定义,那么函数定义不允许有默认参数,默认参数只能放在函数声明里。如果程序没有函数声明,只有函数定义,函数定义才能有默认参数。有了默认参数,其实就是有了一个不带参数的重载版本。 #include "stdafx.h"#include using namespace std;void func(int a = 5); // 函数声明int _tmain(int argc, _ 阅读全文
posted @ 2013-12-09 09:52 XIAOSHUA 阅读(633) 评论(0) 推荐(0) 编辑
摘要: 题目:有一个人在论坛上发的帖子数超过了帖子总数的一半,每个帖子作者都有ID,怎么样迅速找出这个水王?解法一:先将所有ID排序,再扫描一遍求得每个ID出现的次数。解法二:先将所有ID排序,在N / 2处的ID一定是水王的。解法三:Type Find(Type* ID, int N){ Type candidate; int nTimes, i; for(i=nTimes=0;i<N;i++) { if(nTimes==0) { candidate = ID[i]; nTimes = 1; } ... 阅读全文
posted @ 2013-12-05 10:09 XIAOSHUA 阅读(214) 评论(0) 推荐(0) 编辑
摘要: 1 import java.io.*; 2 3 /* 4 * 问题描述:将正整数N(N>=1)转换成二进制数 5 * 例 11 = 1* 2^3 + 0* 2^2 + 1* 2^1 + 1 6 */ 7 8 public class Decimal2Binary { 9 10 // 这里初始的"1"表示二进制1、0串中始终存在的最高位的111 private static String str = "1";12 13 // 递归地判断最后一位是1还是0,然后右移一位14 private static void conv(int n) {15 ... 阅读全文
posted @ 2013-12-02 19:30 XIAOSHUA 阅读(1277) 评论(0) 推荐(0) 编辑
摘要: 在登录表单中,写入:记住我:在服务器端,有如下:$cookie = $_POST['cookie'];switch($cookie){case 0: setcookie("user", $user);break;case 1:setcookie("user", $user, time()+24*60*60);break;case 2:setcookie("user",$user, time()+30*24*60*60);break;}如果用户设置了保存30天,当他在一个月以内再次访问该网站时,网站首先在cookie中寻 阅读全文
posted @ 2013-11-02 19:41 XIAOSHUA 阅读(226) 评论(0) 推荐(0) 编辑
摘要: 这是一个简单的问题,大一刚学编程的时候做的笔记。打印出从1、2、3……n中取出r个数的不同组合(n>=r>=1)例如n=3,r=2,输出:1,22,3下面是实现的代码:public class Combination { public static void combine(int[] list, int k, int l, int r, int n) { if (k + l > n + 1) return; if (l == 0) { for (int i = 0; i < r; i++) ... 阅读全文
posted @ 2013-10-23 20:18 XIAOSHUA 阅读(632) 评论(0) 推荐(0) 编辑
摘要: 链表倒置 阅读全文
posted @ 2013-10-23 20:01 XIAOSHUA 阅读(2911) 评论(0) 推荐(0) 编辑
摘要: C# String IsNullOrEmpty 阅读全文
posted @ 2013-10-23 19:45 XIAOSHUA 阅读(290) 评论(0) 推荐(0) 编辑
摘要: 转自Oracle官网上的Swing Tutorial: 1 package components; 2 3 /** 4 * This application that requires the following additional files: 5 * TreeDemoHelp.html 6 * arnold.html 7 * bloch.html 8 * chan.html 9 * jls.html 10 * swingtutorial.html 11 * tutorial.html 12 * t... 阅读全文
posted @ 2013-04-03 09:19 XIAOSHUA 阅读(313) 评论(0) 推荐(0) 编辑
摘要: 1 /// 2 /// 生成随机字符串 3 /// 4 private class RandomStringGenerator 5 { 6 static readonly Random r = new Random(); 7 const string _chars = "0123456789"; 8 public static string GetRandomString() 9 { 10 char[] buffer = new char[5]; 11 for (int i = 0; i ... 阅读全文
posted @ 2013-03-06 11:16 XIAOSHUA 阅读(249) 评论(0) 推荐(0) 编辑
摘要: 1 using System; 2 using System.Json; 3 using System.Net.Http; 4 5 namespace WorldBankSample 6 { 7 /// 8 /// Sample download list of countries from the World Bank Data sources at http://data.worldbank.org/ 9 /// 10 class Program11 {12 static string _address = "http://ap... 阅读全文
posted @ 2013-03-06 11:10 XIAOSHUA 阅读(1213) 评论(0) 推荐(0) 编辑