摘要:
迭代法: 1 // 反转单链表.cpp : 定义控制台应用程序的入口点。 2 3 #include "stdafx.h" 4 #include 5 6 typedef struct node 7 { 8 int data; 9 struct node *next;10 }linknode,*linklist;11 12 linknode *reverse(linknode *head)//带有头结点的单链表迭代反转,非递归方法13 //思路:将当前结点的next指向前驱;当前结点指向next14 {15 ... 阅读全文
摘要:
给一个数组,元素都是整数(有正数也有负数),寻找连续的元素相加之和为最大的序列;如果都是负数,则输出最大和是0。 1 // 连续元素相加和为最大的序列(有正有负).cpp : 定义控制台应用程序的入口点。 2 3 #include "stdafx.h" 4 5 int max(int a,int b,int c) 6 { 7 double max_v=a; 8 if(b>a) 9 max_v=b;10 if(c>max_v)11 max_v=c;12 return max_v;13 }14 15 int min(int a,i... 阅读全文
摘要:
给定一个浮点数的序列,F1,F2,F3,……,Fn(1a) 9 max_v=b;10 if(c>max_v)11 max_v=c;12 return max_v;13 }14 15 double min(double a,double b,double c)16 {17 double min_v=a;18 if(bmax_val)37 max_val=Max[i];38 }39 return max_val;40 delete [] Max;41 delete [] Min;... 阅读全文
摘要:
1 // 插入排序.cpp : 定义控制台应用程序的入口点。 2 3 #include "stdafx.h" 4 5 void insertsort(int a[],int n) 6 { 7 int i,key; 8 for(int j=1;j=0)&&(a[i]>key)) //交换a[i]和a[i+1]13 {14 int temp;15 temp=a[i];16 a[i]=a[i+1];17 a[i+1]=temp;18 i--;1... 阅读全文
摘要:
1 // 快速排序.cpp 2 3 #include "stdafx.h" 4 5 int partition(int a[],int p,int r) 6 { 7 int x,i,j,tmp; 8 x=a[r]; 9 i=p-1;10 for(j=p;j<=r-1;j++)11 {12 if(a[j]<=x) 13 {14 i++;15 tmp=a[i];//交换a[j]和a[i]16 a[i]=a[j];17 a[j... 阅读全文
摘要:
给出统计一个字节中被置1的位的个数的函数,要求效率尽可能高 1 // 一个字节中置1的位数.cpp 2 3 #include "stdafx.h" 4 5 int bit1count(char x) 6 { 7 int count=0; 8 while(x) 9 {10 x&=(x-1);//去掉最右边的111 count++;12 }13 return count;14 }15 16 void main()17 {18 int num=0;19 char ch;20 printf("inpu... 阅读全文
摘要:
有一由小到大排列的数组m[],数组大小为n,请用二分法查找算法找出与关键数key相等的元素,若查找成功返回元素在数组中的位置,没找到返回-1. 1 // 二分查找.cpp 2 3 #include "stdafx.h" 4 #include 5 6 int search(int m[],int key,int low,int high) 7 { 8 int mid=(low+high)/2; 9 if (low>high)10 return -1;11 if (key==m[mid])12 return mid;13 el... 阅读全文
摘要:
有N个大小不等的自然数(1,2,3,…..N)请将它们从小到大排列。算法要求:时间复杂度为O(n),空间复杂度为O(1)。请简要说明你采用的排序算法并写出c的伪代码。 1 // 计数排序.cpp : 定义控制台应用程序的入口点。 2 //有N个大小不等的自然数(1,2,3,…..N)请将它们从小到大排列。算法要求:时间复杂度为O(n),空间复杂度为O(1) 3 4 #include "stdafx.h" 5 #include 6 7 void counting_sort(int A[],int N) 8 { 9 int C[100],i,j;10 int B[100];1. 阅读全文
摘要:
请不用任何c runtime函数实现以下函数:Inter trim_str(char *pstr)函数功能如下:1)滤掉字符串头尾的空格、回车、tab2)输出字符串通过输入字符串指针返回3)如果成功则返回0否则返回非0 1 // 字符串过滤空格、回车、tab 2 3 #include "stdafx.h" 4 5 int trim_str(char *pstr) 6 { 7 char *p=pstr; 8 char *q; 9 while (*p!='\0')10 {11 if (*p==' '||*p=='\t'||*p== 阅读全文
摘要:
用算法实现将一个输入的数字颠倒(输入12345->54321),要求不调用任何系统函数,也不能将输入的数字转换为字符串作为中间过渡 1 //用算法实现将一个输入的数字颠倒(输入12345->54321),要求不调用任何系统函数,也不能将输入的数字转换为字符串作为中间过渡 2 #include "stdafx.h" 3 4 long reverse(long num) 5 { 6 long x=0; 7 while(num) 8 { 9 x=x*10;10 x=x+num%10;11 ... 阅读全文
摘要:
1 // 删除一个字符串中的子串 2 3 #include "stdafx.h" 4 #include 5 6 int delete_substr(char *str1,char *str2) 7 { 8 int pos=0,k=0,staic=0,n; 9 int str1_len=strlen(str1);10 int str2_len=strlen(str2);11 for(int i=0;i<str1_len;i++)12 {13 for(int j=0;j<str2_len;j++)14 {15 ... 阅读全文
摘要:
不调用任何系统函数,实现一个字符串查找子串的函数,如果包含字串,则返回该字符串的位置值,如果不包含,则返回-1 1 //不调用任何系统函数,实现一个字符串查找子串的函数,如果包含字串,则返回该字符串的位置值,如果不包含,则返回-1 2 #include "stdafx.h" 3 #include 4 5 //方法1 6 int search_max_substr1(const char *str1,const char *str2)//在str1中查找子串str2 7 { 8 int pos=0,k=0,staic=0; 9 int str1_len=strlen(str1 阅读全文
摘要:
1 #include "stdafx.h" 2 #include 3 # define MAX 100 4 5 void main() //输出100以为的质数 6 { 7 int i,j; 8 for(i=1;i<=MAX;i++) 9 {10 for(j=2;j<=i;j++)11 {12 if(i%j==0)13 break;14 }15 if(i==j)16 printf("%d\t",i);17 }18... 阅读全文
摘要:
1 #include "stdafx.h" 2 #include 3 4 bool Is_palindromic(int num) //判断一个数是不是回文数 5 { 6 int pal=0; 7 int number=num; 8 while(num) 9 {10 pal*=10;11 pal+=num%10;12 num/=10;13 }14 return pal==number;15 }16 17 void main()18 {19 int n,m;20 printf("... 阅读全文
摘要:
1 #include "stdafx.h" 2 #include 3 #include 4 #define MAX 100 5 6 //在字符串str1和str2中查找最大的公共子串maxsubstr 7 void findmaxsubstr(const char *str1,const char *str2,char *maxsubstr) 8 { 9 int maxlen=0,maxpos=-1;10 int k;11 for(int i=0;imaxlen)22 {23 ... 阅读全文