自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(46)
  • 收藏
  • 关注

原创 C++数组。

#include using namespace std;//指定一个位置开始,然后每隔k个位置剔除一个元素,输出最后剩余的元素。//相当于所有数据绕成一圈,按k个位置长度剔除数据,当只剩一个时,游戏结束,得到结果.int Grial(int a[],int end,int start,int k){ int n = end; int i = start; while(n>1)

2015-04-30 16:44:03 562

原创 C++对文本里面的大量数据进行排序(shell,c++,fopen,awk,sed)

#/bin/bash#对文本里面的数据进行排序awk 'BEGIN{ RS=","}{print $0}END{}'<a | sort -n | sed '/^$/ d' | awk 'BEGIN{ RS="\n";ORS=","}{print $0}END{}'|sed 's/,$//g'下面是c++版本的。---------------------------

2015-04-29 09:27:29 1378

原创 C++单链表找倒数第k个节点(时间复杂度为o(n)哦,用相距k节点的2个指针进行操作)

//输入一个单向链表,输出该链表中倒数第k个结点。链表的倒数第0个结点为链表的尾指针。//我的思路是2个指针往后面找,你想啊,如果是一个指针,肯定需要遍历2次,第一个遍历总共节点数,第二次才遍历最终结果//这样的做法明显是不够好的,时间复杂度变成了2n,但是如果我们用2个指针,他们之间的距离差k个节点,有一个节点到达NULL//时(尾部),另一个节点就是我们要求的节点可以返回得到结果.#

2015-04-29 08:54:58 1889

原创 c++数组操作(重复,排序,bitset)

//问题: //给你一个数组,a[]={1,1,1,1,1,2,2,2,2,3,3,3,4,5,6} //要输出的结果是1,2,3,4,5,6.(去除重复的数字,要求时间空间的考虑).#include using namespace std;templateclass Bitset{ public: Bitset() { _Tidy();//调用_Tidy()函数会初

2015-04-28 09:58:42 1724

原创 C++查找

//有这样一个二维矩阵A[N][N],满足j < k时, 1)a[j] < a[k]; 2)a[j] < a[k]//(其实就数据从左上角到右下角纵横方向上都递减),给定一个数target,如何快//速搜索是否 在这个矩阵中,是的话输出二维坐标,否则输出Null。 #include using namespace std;void Grial(int (*a)[4],int x){ i

2015-04-28 09:54:02 887 12

原创 C++数组排成最大的数(数组,算法,排列)

//把数组排成最大的数(数组、算法)。//题目:输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最大的一个。//例如输入数组{32, 321},则输出这两个能排成的最大数字32321.。//或者输入数组{10,9,33,1000}输出这四个能排列的最大数字933101000.#include #define MAXSIZE 100using namespace s

2015-04-27 15:36:16 3261

原创 C++求解字符串(最小子字符串,最大子字符串,删除指定字符串)

#include #include #define SIZE 10#define MAXVALUE 0x7fffffffusing namespace std;//题目是:求一个字符串中最小字串.//求最小字串,比求最大字串难的多,下面有我的求最大字串代码,我没有想到更好的方法,下面的这个方法虽然空间复杂度太大,可是时间复杂度是比较快的。templatestruct Node{

2015-04-27 09:51:00 1432

原创 c++算法极致(一行代码求1+2+3+...+n , 一行代码求n!)

#include //n!的求解using namespace std;int Giral(int n){ (n>2) && (n*=Giral(n-1));//这里不能写n>0,因为n=0时会返回0,将乘式的结果会化成0,n>2比n>1要少一次,效率要高一些. return n;}int main(){ cout<<Giral(4)<<endl;}#include

2015-04-26 15:46:58 3580

原创 c++感悟

无论何时何地都不要去问别人问题,如果是和别人讨论就除外,因为当你从一个问题开始思考的时候,这就收获是无法言语的,时间一长,你就会发现,你学什么都会了,学什么都容易了,如果你一有问题就去问别人的话,时间一长,你自己做的事情或者解决的问题,你都不能确定是不是正确的,所以你又要去问别人,如此恶性循环,是一个it猿的悲剧,因为他总是很努力,却总是也无法提高,要想提高自己,就必须每问必问问自己,最后

2015-04-26 10:19:44 2371 16

原创 字符串反转及数组奇偶划分

//将字符串反转,型如:123 456 789 abc ,反转后的结果是 abc 789 456 123/*#include #include using namespace std;void Exchange(char *&str){ char *p=str+strlen(str)-1; char *q=str; while(q<p) { char temp=*q;

2015-04-26 09:46:37 1173

原创 C++在已排序数组中查找和值确定的第一次出现的两个数(要求时间复杂度为o(n))

#include using namespace std;//输入一个已经按升序排序过的数组和一个数字,//在数组中查找两个数,使得它们的和正好是输入的那个数字。//要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。//例如输入数组1、2、4、7、11、15和数字15。由于4+11=15,因此输出4和11。void Grial(int a[],int x,i

2015-04-25 22:47:48 1525 1

原创 C++求n!中0的个数

/*#include using namespace std;//2014!里面0的个数.int Giral(int x){ int i=0; int j=0; while(x) { int sum = x; if(x%2==0) { while(x%2==0) { x/=2; i++; } } if(x%5==0)

2015-04-25 21:34:01 2138

原创 C++单链表(下标n到下标m的逆序)

#include using namespace std;templatestruct Node{ T data; Node *next; Node():next(NULL){}};templateclass List{ public: List() { head = new Node(); } void Insert(T x) { Node *s =

2015-04-24 10:23:43 1090 1

原创 C++重载(关于const的重载)

#include #define DefaultSize 10using namespace std;templateclass Grail{ public: Grail() { data = new T[DefaultSize]; for(int i=0;i<10;i++) { data[i]=i; } } const T& operator[](

2015-04-22 10:01:30 765

原创 shell脚本编程(严格的终端格式控制,美丽的输出字体颜色)

#!/bin/bash##下面是字体输出颜色及终端格式控制#字体色30-37echo -e "\033[30m黑色字\033[0m"echo -e "\033[31m红色字\033[0m"echo -e "\033[32m绿色字\033[0m"echo -e "\033[33m黄色字\033[0m"echo -e "\033[34m蓝色字\033[0m"echo -e "\033

2015-04-22 09:47:25 1515

原创 二叉树

#include #include #include using namespace std;templatestruct BinTreeNode{ T data; BinTreeNode *leftchild; BinTreeNode *rightchild; BinTreeNode(T x=T()):data(x),leftchild(NULL),rightchild(NU

2015-04-21 17:29:10 781 2

原创 shell脚本编程(快速排序)

#!/bin/bash#shell脚本编程之快速排序的实现(以最右边为元点的思想)#a=(8 5 10 3 2 93 4 1 2 3 40 9 61 8 6 29)a=(0 0 0 0 1 2 3 3 4 4 8 28 30495 921 43716)temp=buff=#交换函数swap(){ buff=${a[$1]} a[$1]=${a[$2

2015-04-21 09:05:47 3625

原创 shell脚本编程(合并排序)

#!/bin/bash#shell脚本排序之合并排序a=(1 3 5 7 9)b=(2 4 6 8 10 12 14)c=n=5m=7i=0j=0k=0while [ $i -lt $n -a $j -lt $m ];do if test ${a[$i]} -gt ${b[$j]};then c[$k]=${b[$j]} j=$(($j+1)) k=$(($k+

2015-04-21 08:38:38 719

原创 图的邻接表(广度优先遍历,深度优先遍历,最小生成树(Kruskal算法))

main.h:#include #include #define DefaultSize 10#define maxWeight -1using namespace std;templatestruct Edge{ int dest; E cost; Edge *link; Edge(int d=0,int c=0):dest(d),cost(c),link(NUL

2015-04-20 12:50:26 4498

原创 shell脚本添加用户删除用户删除文件的脚本实现

#/bin/bash#DEBUG=0ADD=0DEL=0RM=0while [ $# -gt 0 ]docase $1 in -v|-verbose) DEBUG=1 shift ;;-h|--help)#帮助文档 echo "`basename $0` --add LIST --del LIST -h|--help --rm LIST" exit 0 ;;--

2015-04-20 12:38:31 1672

原创 shell脚本排序(冒泡排序)

#/bin/basha=(9 84 51 0 345 1 2 34 1 0) #自己定义一个数组temp=for((i=0;i<10;i++)){ for((j=i;j<10;j++)) { x=${a[$i]} if test $x -ge ${a[$j]} then temp=${a[$i]} a[$i]=${a[$j]} a[$

2015-04-20 08:30:05 3770 2

原创 C查看分配给用户的内存及分配给系统的内存大小

#include #include #include using namespace std;int main(){ int count = 0; while(malloc(1204*1204)) { count++; } cout<<count<<endl; cout<<(count/1024)<<"G"<<endl; return 0;}我的总内存是4G,

2015-04-19 12:33:33 799

原创 exec函数簇的使用

#include#include using namespace std;int main(int argc,char *argv[]){ execl("/bin/ls","-l",NULL);//1 execv("/bin/ls",&argv[1]);//2 execle("/bin/ls","-l",(char *)0,NULL);//3 execve("/bin

2015-04-18 11:52:56 819

原创 C++内存检测(定位到确定地址,并且用hash表来保存提高了搜索效率)

#include #include #include #include #define DefaultSize 7using namespace std;struct Node{ char *pname;//文件名字 int line;//行号 int size;//大小 long long save;//存储地址ip Node *next;};struct Hash

2015-04-18 10:13:06 748

原创 C++快速排序(随机值元法)

#include #include using namespace std;int sum(int a,int b){ return (rand()%(b-a)+a+1);}void Grial(int a[],int x,int y){ if(x>=y)return ; int i=x; int j=y; int temp; int b=sum(i,j);//求取随机值

2015-04-15 11:23:31 946

原创 C++快速排序(以中间元素为元点)

#include using namespace std;void Grial(int a[],int x,int y){ if(x>=y)return ; int i=x; int j=y; int temp; int key=a[(i+j)/2]; while(i<j) { while(a[i]<key)i++;//找到第一个比key大的数 while(a[j]>k

2015-04-15 11:08:18 1272

原创 c++快速排序(以最右值为元点)

#include using namespace std;void swap(int *a,int *b){ int temp = *a; *a = *b; *b = temp;}void Grial(int a[],int x,int y){ int i=x-1; int j=x; int key = a[y]; if(x>=y)return ; while(j<y

2015-04-15 09:55:08 729

原创 linux命令(while,shell参数的用法)

#!/bin/bash while IFS=: read name1 name2 name3 name4 #IFS是从文件读取内容时指定的分割符号,将a中的内容以:分开的部分分别赋值给相应变量. do echo $name1 '|' $name2 '|' $name3 '|' $name4 done <a #反引号与$()的功能是命令替换,将反引号或$()中的字

2015-04-15 08:35:46 4373

原创 linux命令(find中-a,-o,not的用法)

sudo find /etc -size +1M -exec echo {} \;#在/etc目录下找文件大小在1M以上的文件并且显示,文件用换行符隔开。sudo find /etc -size +1M | xargs echo#达到-exec相同的功能但是用空格分开寻找到的文件。#-exec后面的{} \;是不能少的。find 紧跟的是指定开始寻找的绝对路径,我取当前路径。find

2015-04-15 08:31:24 13620

原创 C求解一个数二进制1的个数

#include using namespace std;int Grail(int x){ int N=0; while(x) { x=x&(x-1); N++; } return N;}int main(){ cout<<Grail(7)<<endl; //0111 //0110 //0101 //0100 //0100 //0011 ret

2015-04-14 08:55:58 798

原创 C求一个数的二进制1的个数(精版)

#include using namespace std;int Grail(int x){ int N=0; for(;x!=0;x>>=4)//一次左移4位。 { N+="\0\1\1\2\1\2\2\3\1\2\2\3\2\3\3\4"[x&0xf]; } return N;}int main(){ cout<<Grail(15)<<endl;}

2015-04-13 14:18:05 615

原创 C++单链表逆序(时间与空间的考虑)

#include using namespace std;struct Node{ int data; Node * next; Node(int x=0):data(x),next(NULL){}};class List{ public: List() { first = new Node(); } void Insert(int x) {

2015-04-13 09:26:44 633

原创 C++排序(合并排序)

//合并排序#include #include using namespace std;void sort(int c[],int a[],int b[],int n){ int i=0; int j=0;//a int k=0;//b while(k!=5 && j!=5) { if(a[j]>b[k]) { c[i++]=b[k++]; }

2015-04-12 11:49:35 707

原创 Hash线性探测法C++实现

#include #include #define DefaultSize 10using namespace std;enum KindOfStatus{Active,Empty,Deleted};templateclass HashTable{public: HashTable(int d,int sz=DefaultSize) { _D = d; Ta

2015-04-11 20:00:19 1446

原创 素数求解一二三

#include #include #include using namespace std;void Grial(int _N){ int count=0; vector ar; ar.push_back(2); int _A=0;//统计循环次数。 int _K; int _I; for(_I=3;_I<=_N;_I+=2) { for(_K=3;_K<_I;_

2015-04-10 00:08:55 457

原创 gdb调试的一些基本操作

1.g++ -g filename (生成a.out可调试程序)2.gdb a.out(进入调试界面)3.b+行号(默认包含main函数的文件);b+filename:行号(指定文件中设置断点).4.info + b (查看所有断点)5.d + NUM(info b得到断点的编号NUM),删除断点编号是NUM的断点。6.p + 变量名(打

2015-04-09 16:33:17 638

原创 C++单链表对环的操作。

#include using namespace std;templateclass Node{ public: Node(_Ty _X=_Ty()):_Data(_X),_Next(NULL){} int _Data; Node *_Next;};templateclass List{ public: List() { _First = new Node()

2015-04-09 14:33:30 533

原创 c++排序(快速排序)

#include #define _SZ 10using namespace std;templateclass Grial{ public: Grial(_Ty *_P,int _X=_SZ) { _SP=_X; data = new _Ty[_SP]; for(int _I=0;_I<_SP;_I++) { data[_I]=_P[_I]; }

2015-04-08 19:05:09 549

原创 C++排序(小堆排序)

#include#includeusing namespace std;templateclass MinHeap{public: MinHeap(int sz=DefaultSize) { capacity = sz>DefaultSize?sz:DefaultSize; heap = new Type[capacity]; size = 0; } MinHea

2015-04-08 17:12:52 601

原创 c++中虛表函数

#include using namespace std;class Grail{ public: virtual void _A() { cout<<"Grail::_A()"<<endl; } virtual void _B() { cout<<"Grail::_B()"<<endl; } virtual void _C() { cout<<"Grail:

2015-04-08 10:02:17 936 1

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除