简单的题目3
来源:互联网 发布:dsp广告系统架构php 编辑:程序博客网 时间:2024/05/16 19:03
/* 定义一个宏比较两个数的大小,不能使用> < if */#include <iostream>#define max(a,b) (((a)-(b))&(1<<31))?(b):(a)using namespace std;int main(){ int a = max(3+4,2*4); cout<<a; cin.get(); }
/* 删除串中指定的字符: 1 查找指定的字符,查找操作有遍历、顺序存储的话二分、快排的变体快速查找第k个元素、将查找的内容hash然后线性查找 这里很明显该是将指定的字符hash然后O(1)查找,side effect是要开辟128的空间 2 删除操作,等价于在一个连续数组中删除几个字符,如果删除一个就是将后面的复制到前面就可以了 如果删除多个,假设多个的个数为M,数组的长度为N,那么还是复制的话复杂度为MN 如果不用复制的方法,使用两个索引都初始化为数组起始,第一个索引指向当前存放留下字符的位置,第二索引遍历数组 如果要留下就赋给第一个索引,如果不要留下就继续往后查找*/#include <iostream>#include <stdio.h>using namespace std;void deleteChars(char[],char[]);int main(){ char a[] = "edaclppjwoejrdrt43-32234ceopq"; char b[] = "1234567890"; deleteChars(a,b); cout<<a; cin.get();}/* a 串 b 要删除元素的集合*/void deleteChars(char a[],char b[]){ int chars[128]={0}; for(char* p=b;*p!='\0';p++) { chars[*p]++; } char *pFir,*pSec; pFir=pSec=a; for(;*pSec!='\0';pSec++) { if(!chars[*pSec]) { *pFir = *pSec; pFir++; } } *pFir = '\0';//最后补一个字符串结束标志 }
/* 递归判断数组升序 */#include <iostream>using namespace std;bool isAsc(int[],int,int);int main(){ int a[] = {1,2,3,4,3,5,6,7}; cout<<isAsc(a,0,sizeof(a)/sizeof(int)); cin.get(); }bool isAsc(int a[],int i,int n){ if(i==n-1) return true; else { if(a[i]>a[i+1]) return false; else return isAsc(a,i+1,n); } }