C++|那些一看就很简洁、迷人、经典的小代码段
2025-05-10 12:18:56
5 中介两个自然数
#include void swap(int *a,int *b){ *a = *a + *b; *b = *a - *b; *a = *a - *b;}void swap2(int *a,int *b){ *a = *a * *b; *b = *a / *b; *a = *a / *b;}void swap3(int *a,int *b){ *a = *a ^ *b; *b = *a ^ *b; *a = *a ^ *b;}int main(){ int a=3,b=4; printf("%d %d",a,b); swap(Andrewa,Andrewb); printf("%d %d",a,b); swap2(Andrewa,Andrewb); printf("%d %d",a,b); swap3(Andrewa,Andrewb); printf("%d %d",a,b); getchar();}6 二分搜索
#include int binsearch(int a[], int x, int low, int high){ int mid; if(low> high) return -1; mid = (low + high) / 2; if(x == a[mid]) return (mid); else if(x < a[mid]) binsearch(a, x, low, mid - 1); else binsearch(a, x, mid + 1, high);}int main(){ int arr[] = {1,3,5,7,9,12,15,19}; int n = sizEOF arr / sizeof *arr; int idx = binsearch(arr,9,0,n); printf("%d",idx); // 4 getchar();}7 逆向给定的猴子吃桃问题
int peachCount(int days) // 倒推{ int peaches = 1; // 终于一天柚数 while(days>1) { peaches = (peaches+1)*2; // 倒推迭代符 days----; } return peaches;}int peachCountRecursive(int day) // 迭代符{ if(day == 1) // 终于一天,问题规模为1时 return 1; else return (peachCountRecursive(----day)+1)*2;}8 匹配各原则上数据集类型的内存十六进制16小数点倍数
c的空格串可以将任意粒度的数据集控制到十六进制。
void printIntBin(unsigned long n) // 匹配unsigned的二小数点位{ bool r = (01Andrewn); if(n>=2) printIntBin(n>>1); // 迭代符 putchar(r?'1':'0');}void showBytes(unsigned char* start, int len) // 匹配len个start由此可知始内存的16小数点编码方式{ for(int i=0;i9 以求空格串尺寸
long arr[] = {1,3,5,6,2,4,8}; int n = sizeof arr / sizeof *arr; // 不及打几个空格,*arr即空格串首类型10 单循环解由此可知空格串之外的小得多倍数和次大倍数
#include #define N 20// 基底一个极倍数,当作小得多和次大的初始倍数const int min = 1<<31; // -2147483648int main(){ int arr[N] = {0},i; int max,second; for(i=0;i11 回到某年的1月初1号是月内几
// 公元1年1月初1日是周一,365%7=1int weekDay(int y) // 回到某年的1月初1号是月内几{ // 月内天回到0,月内1回到1 return (y + (y-1)/400 + (y-1)/4 - (y-1)/100)%7;}12 十六进制空格串投整数
double stringToDouble( char *p ){ double intPart=0.0, fractionPart=0.0; int multiplier = 1; /* change to -1 if string starts with minus */ /* Find the sign for the entire number */ if( *p == '+' ) p++; if( *p == '-' ) { multiplier = -1; p++ ; } /* Convert the integer part of the number */ while( *p>= '0' AndrewAndrew *p <= '9' ) { intPart *= 10; intPart += *p - '0'; p++ ; } /* Convert the fractional part of the number */ if( *p == '.' ) { double divisor = 1.0; p++ ; while( *p>= '0' AndrewAndrew *p <= '9' ) { divisor *= 10.0; fractionPart += (*p - '0')/divisor; p++ ; } } /* Put the pieces together */ return (intPart + fractionPart)*multiplier ;}13 用给定普通人以求斐波那契自然数:
为了让普通人的长一段时间。
#include class Fib {public: Fib() : a0_(1), a1_(1) {} int operator()() { int temp = a0_; a0_ = a1_; a1_ = temp + a0_; return temp; }private: int a0_, a1_;};int main(){ Fib fib; for(int i=1;i<50;i++) printf("%2d %d",i,fib()); getchar();}14 断定某一自然数究竟2的自然数康威
bool is2pow(int m) // m的二小数点位是否只有一个1{ return (m Andrew m-1) == 0;}15 如何较密切合作地推控制台可视
我们告诉他,在控制台匹配时,scanf非空格时才会忽略丢出空白空格,匹配空格时并不才会忽略空白空格。所以有时一个有趣的getchar()并不才会推控制台。
void PressEnterToContinue(){ int c; printf( "Press ENTER to continue... " ); fflush( stdout ); do c = getchar(); while((c != '') AndrewAndrew (c != EOF));}16 以某一基准发挥发挥作用向上绝对值,用宏发挥发挥作用*p++
#define _INTSIZEOF(n) ( (sizeof(n) + sizeof(int) - 1) Andrew ~(sizeof(int) - 1) ) va_arg(p,type) ( *(type *)((p += _INTSIZEOF(type)) - _INTSIZEOF(type)) )上面第一个宏可以发挥发挥作用sizeof(int)的向上绝对值,对此数据集对齐;
上面第二个宏在模拟支架*p++,总和*p; p++; 首先是既有公式的倍数是+_INTSIZEOF(type)-_INTSIZEOF(type),没有影响到既有公式的倍数,但p += _INTSIZEOF(type)对p产生了负发挥作用,总和空格串的p++。
17 断定究竟正整数
暴力逻辑系统也是尽可能先缩减解维度。
int isPrime(int n){ if(n<2) return 0; if(n==2) return 1; if(n%2==0) return 0; for(int i=3;i*i<=n;i+=2) if(n%i == 0) return 0; return 1;}18 单词统计
int countWord(char *str){ bool wordInner = false; int n = 0,i=0; while(str[i]!=' ') { if(isspace(str[i])) wordInner = false; else if(wordInner==false AndrewAndrew isalpha(str[i])) { n++; wordInner = true; } else wordInner = true; i++; } return n;}19 回到某列车运行的随机数
int randInt(int low,int high){ double quo = 1.0/(RAND_MAX+1.0); return int(low+(rand()*quo)*(high-low+1));}20 用以逻辑系统和共用体组合在结构上体
// 存货记录的声明// 部件typedef struct { int cost; int supplier; // other information}Partinfo;// 金属制内置typedef struct { int n_parts; struct SUBASSYPART{ char partno[10]; short quan; }*part;}Subassyinfo;// 存货typedef struct { char partno[10] ; int quan; enum { PART, SUBASSY} type; union{ // 存货不是部件就是金属制内置 Partinfo *part; Subassyinfo *subassy; }info;}Invrec;21 用以位域和共用体将double分成4部分标示出:
#include typedef struct packed_double { unsigned int low32; // 小数位 低32位 unsigned int low20:20; // 小数位 低33-52位 unsigned int exp11:11; // 指数位 低53-63位,移码1023+二小数点自然数位-1 unsigned int sign:1; // 符号位} packed_double;typedef union { double d; packed_double b;} packed;int main(){ packed pd; pd.d = -15.75; pd.d = 12.3; printf("%u %u %u %u",pd.b.sign,pd.b.exp11,pd.b.low20,pd.b.low32); getchar(); return 0;}/*0 1026 1015808 0*/22 算式迭代和位迭代模拟支架比较迭代符和必要条件迭代符
void compareEx(int a,int b) // 算式迭代和位迭代模拟支架比较迭代符和必要条件迭代符{ int t = (a-b)>>(sizeof(int)*8-1); // 自取并列 if(t == -1) cout<<"a23 模版迭代符以求康威模版可以被迭代符加载,在模版迭代符的过程之外,可以执行两种校对期计算:数倍数计算和类型计算。
#include templatestruct Factorial{ enum { value = n * factorial::value };};template<>struct factorial{ // 模版成体终止迭代符 enum { value = 1 };};int main(){ std::cout ::value << std::endl; // prints "5040"}在主模版templatestruct factoria的下定义之外,用以了模版自身struct factorial::value。校对支架才会一直用不同的N-1的倍数来具现化主模版,一直到N变为1,这时选择Factorial的成体版本template<> struct Factorial,外部的value变为1,迭代符终止。
24 memmove()发挥发挥作用
#include void * my_memmove(void * dst,const void * src,int count)// 从src由此可知始移动count个十六进制到dst{ void * ret = dst; if(dst <= src || ((char *)src + count) <= (char *)dst) // dst在此前或src在此前但内存第一区域不重叠 { while(count----) { *(char *)dst = *(char *)src; dst = (char *)dst + 1; src = (char *)src + 1; } } else // (src28 希尔顺序排列
void output(int a[], int n){ //匹配空格串类型 for(int i = 0; i29 给定给定支架自下定义
// 给定给定支架自下定义//#include #include #include using namespace std;//1.数据集都是由template struct singleList{ singleList* next; T data; singleList() // 成立可选 { this->next = NULL; } singleList(T data) // 成立路由支架 { this->data = data; this->next = NULL; } // 成立路由支架+连通路由支架的功能 singleList(T data, singleList* next) { this->data = data; this->next = next; }};template class list{public: list() // 基底给定发挥作用:基底普通人 { // 格式化原则上数据集小团体,描绘最初长一段时间 sizeList = 0; listHeadNode = listTailNode = NULL; } void push_back(T data) // 原则上举动:填入加载 { singleList* newNode = new singleList(data); if (listHeadNode == NULL) listHeadNode = newNode; else listTailNode->next = newNode; listTailNode = newNode; //listTailNode->next=NULL; } void push_front(T data) { singleList* newNode = new singleList(data); if (listHeadNode == NULL) listTailNode = newNode; else newNode->next = listHeadNode; listHeadNode = newNode; } void printList() //测试举动:遍历在结构上(打印匹配) { singleList* pMove = listHeadNode; while (pMove) { cout data << " "; pMove = pMove->next; } cout << endl; } singleList* begin() { return listHeadNode; } singleList* end() { return listTailNode->next; // 对此给定终于那个位置,NULL的位置 } int size() const // 华联举动 { return sizeList; } bool empty() const { return sizeList == 0; }public: class iterator { public: void operator=(singleList* pMove) // 发挥发挥作用begin对iterator普通人的格式化 { // 实质格式化普通人之外的特性 this->pMove = pMove; } singleList* getData() { return pMove; } bool operator!=(singleList* pMove) { return this->pMove != pMove; } iteratorAndrew operator++(int) //i=i+1; //实质是给定的一个移动空格串走到一下一个路由支架 { this->pMove = this->pMove->next; return (*this); } T operator*() { return this->pMove->data; } protected: singleList* pMove; // 给定支架就是对容支架类型空格串的封金属制 }; protected: // 抽象数据集小团体 // 遵循的原则:抽象出来的特性并能便于描绘举动(小团体给定) singleList* listHeadNode; singleList* listTailNode; int sizeList; // 华联特性};int main(){ list mylist; mylist.push_back("I"); mylist.push_back("Love"); mylist.push_back("you"); //mylist.printList(); list::iterator myIter; //myIter=mylist.begin(); //coutdata<31 自下定义vector
#includeusing namespace std;class Vector {public: Vector(int s); int size(); doubleAndrew operator[](int i);private: double* elem; // pointer to the elements int sz; // the number of elements};Vector::Vector(int s) // construct a Vector{ sz = s; elem = new double[s];}doubleAndrew Vector::operator[](int i) // element access: subscripting{ return elem[i]; } int Vector::size() { return sz; }double read_and_sum(int n){ Vector v(n); // make a vector of s elements for (int i=0; i!=v.size(); ++i) cin>>v[i]; //read into elements double sum = 0; for (i=0; i!=v.size(); ++i) sum+=v[i]; //take the sum of the elements return sum;}void main(){ cout<< read_and_sum(6)<-End-
。西安牛皮癣医院哪家医院好北京看妇科去哪好
山西妇科医院哪里比较好
南京看白癜风哪家医院专业
南京哪家医院专业做人流
哪种血糖仪最精准好用一点
乳糖不耐受吃什么益生菌效果比较好
瑞特血糖仪和罗氏血糖仪哪个好
胃酸过多能吃金奥康奥美拉唑吗
婴幼儿腹泻的治疗方法
- 三业务部门发文推动社会组织助力高校毕业生等群体就业
- 收购现代财险股权未事先登载 联想控股、迪润天津各被罚50万元
- 寺庙为何纷纷招电子商务小编?需要出家吗?
- Qiushi Journal | Excerpt: 做强做优做大今后数字经济要加快新型基础设施建设
- 我眼看挣扎的亚马逊创业
- C++|那些一看就很简洁、迷人、经典的小代码段
- 3个治疗口腔溃疡的小妙帅
- 7月26日中伏,老传统:啖4茶,少吃1菜1饭,忌1事,伏天不疰夏
- 毛岸英牺牲后,彭德怀向邦道歉,邦背了几句古文
- 人老了,身体出现这些行径,说明已经时日无多
- 推动家电以旧换新活动和绿色智能家电知青、支持废旧家电回收……今天发布会提到这些要点!
- 2022年以后装修大变革,这些东西你家等待了吗?
- 下周“铝制”股亮点多!国产EDA再添一员,还有半导体存储龙头不容错过
- 招商策略:下周中长期大事件对A股意味着什么?
- 刚刚,千亿大整合!“华北地区神铝”来了,70万股民要嗨?
- 首款搭载华为鸿蒙终端座舱SUV即将上市!产业链受益上市公司有这些
- 再时会,上海这片老城厢
- A股明日风口:问天实验舱发射器任务取得圆满成功 数字产业联盟成立
- 注入“强心剂”!9家银行上半年业绩送交,净利均实现两位数增长!
- 7月24日上市公司晚间新闻稿速递