![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C/C++
文章平均质量分 96
二十六画生的博客
国内Top5硕士,先后在京东、字节从事多年后端开发、大数据开发。
展开
-
C++ 冒泡排序及优化
#include using namespace std;int main(){ int arr[] = {3,6,8,1,9,4}; int temp; int len = sizeof(arr)/sizeof(arr[0]); //简单的冒泡排序 for(int i=0;i<len;i++){ for(int j=1;j<len-i;j++){ if(arr[j-1]原创 2017-12-21 13:44:55 · 1687 阅读 · 1 评论 -
C++构造单链表
看到很多关于链表处理的函数,一直没看到关于构造链表的函数,今天自己写一个#include "stdafx.h"#include using namespace std;struct ListNode{ int val; ListNode* next;};ListNode* initList(ListNode* head){ ListNode* node1 = new L原创 2017-08-25 13:51:18 · 2766 阅读 · 0 评论 -
C++调用构造函数
#include "stdafx.h"#include using namespace std;class Goods{private: char* name;//char* 类型 float price;public: Goods(char* nameParam,float priceParam ){ name = nameParam; price = pricePara原创 2017-08-25 12:48:37 · 378 阅读 · 0 评论 -
C++调用结构体
#include "stdafx.h"#include using namespace std;struct Goods{ char* name;//char* 类型 float price; };//这个分号别忘了int _tmain(int argc, _TCHAR* argv[]){ struct Goods goods = {"巧克力",1.5}; cout<原创 2017-08-25 13:00:52 · 4715 阅读 · 0 评论 -
字符串反转--句子反转和单词反转
#include using namespace std;//整个句子反转void allReverse(char s[]){ int i=0,j=strlen(s)-1;//别忘了减1 char temp ; while(j > i){ temp = s[i];s[i]=s[j];s[j]=temp; j--; i++; } cout<<s<<endl;}//all原创 2017-08-21 18:00:11 · 3843 阅读 · 0 评论 -
为什么要有地址对齐和结构体大小
什么是对齐,以及为什么要对齐⒈现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内存地址访问,这就需要各类型数据按照一定的规则在空间上排列,而不是顺序的一个接一个的排放,这就是对齐。⒉对齐的作用和原因:各个硬件平台对存储空间的处理上有很大的不同。一些平台对某些特定类型的数据只能从某些特原创 2017-08-21 16:18:58 · 1710 阅读 · 0 评论 -
C语言--函数指针
函数指针是指向函数的指针变量。通常我们说的指针变量是指向一个整型、字符型或数组等变量,而函数指针是指向函数。函数指针可以像一般函数一样,用于调用函数、传递参数。函数指针变量的声明:typedef int (*fun_ptr)(int,int); // 声明一个指向同样参数、返回值的函数指针类型#include #include // 回调函数void原创 2017-08-21 10:28:13 · 387 阅读 · 0 评论 -
C语言--数组做函数参数
#include //double getAverage(int arr[], int size)//正确 //double getAverage(int arr[5], int size)//正确//double getAverage(int arr[10], int size)//正确 double getAverage(int* arr, int size)//正确{ i原创 2017-08-21 09:55:33 · 806 阅读 · 0 评论 -
C/C++的变量生存期和作用域
静态变量 存储在动态存储区,只能在本文件的本函数内使用,函数结束后释放,全局变量 存储在静态存储区,可以在本文件和其他文件内使用,程序运行结束后释放静态局部变量 存储在静态存储区,只能在本文件的本函数内使用,函数结束后不释放,程序运行结束后释放静态全局变量 存储在静态存储区,只能在本文件内使用,程序运行结束后释放原创 2017-08-20 21:24:49 · 523 阅读 · 0 评论 -
双核处理--网易2017春招实习笔试编程题1
时间限制:1秒空间限制:32768K一种双核CPU的两个核能够同时的处理任务,现在有n个已知数据量的任务需要交给CPU处理,假设已知CPU的每个核1秒可以处理1kb,每个核同时只能处理一项任务。n个任务可以按照任意顺序放入CPU进行处理,现在需要设计一个方案让CPU处理完这批任务所需的时间最少,求这个最小的时间。 输入描述:输入包括两行:第一行为整数n(1 ≤ n ≤原创 2017-08-11 11:14:19 · 546 阅读 · 1 评论 -
C++合并两个有序的单链表
一直看到的都是合并的函数,却没有看到完整的输入输出啥样的,今天自己写一个,包含链表的初始化// 合并两个有序链表.cpp : 定义控制台应用程序的入口点。#include "stdafx.h"#include using namespace std;struct ListNode{ int val; ListNode* next;};ListNode* initList1原创 2017-08-25 16:55:35 · 4411 阅读 · 0 评论 -
LeetCode 21. Merge Two Sorted Lists 合并两个有序单链表
一直觉得操作链表用C++比用Java药方便很多Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.合并两个排好序的链表,并返回这个新链表。新链表应该由这两个链原创 2017-08-25 17:28:50 · 617 阅读 · 0 评论 -
素数对
给定一个正整数,编写程序计算有多少对质数的和等于输入的这个正整数,并输出结果。输入值小于1000。如,输入为10, 程序应该输出结果为2。(共有两对质数的和为10,分别为(5,5),(3,7))#include using namespace std;bool isPrime(int num){//判断这个数是否是素数 if(num ==2 || num==3) return tr原创 2017-09-11 23:01:48 · 730 阅读 · 0 评论 -
a ~ y的25个字母,从1位到4位的编码,输出这个编码对应的Index.
假定一种编码的编码范围是a ~ y的25个字母,从1位到4位的编码,如果我们把该编码按字典序排序,形成一个数组如下: a, aa, aaa, aaaa, aaab, aaac, … …, b, ba, baa, baaa, baab, baac … …, yyyw, yyyx, yyyy 其中a的Index为0,aa的Index为1,aaa的Index为2,以此类推。 编写一个函数,输入是任意转载 2017-09-11 22:56:51 · 2984 阅读 · 0 评论 -
游戏任务标记
游戏里面有很多各式各样的任务,其中有一种任务玩家只能做一次,这类任务一共有1024个,任务ID范围[1,1024]。请用32个unsigned int类型来记录着1024个任务是否已经完成。初始状态都是未完成。 输入两个参数,都是任务ID,需要设置第一个ID的任务为已经完成;并检查第二个ID的任务是否已经完成。 输出一个参数,如果第二个ID的任务已经完成输出1,如果未完成输出0。如果第一或第二原创 2017-09-11 22:59:42 · 742 阅读 · 0 评论 -
geohash编码
geohash编码:geohash常用于将二维的经纬度转换为字符串,分为两步:第一步是经纬度的二进制编码,第二步是base32转码。此题考察纬度的二进制编码:算法对纬度[-90,90]通过二分法进行无限逼近(取决于所需精度,本题精度为6)。注意,本题进行二分法逼近过程中只采用向下取整来进行二分,针对二分中间值属于右区间。算法举例如下: 针对纬度为80进行二进制编码过程:1) 区间[-90,原创 2017-09-12 00:23:39 · 1200 阅读 · 0 评论 -
C++字符串连接,输入和输出
#include #include //这一句有了才能编译s1.append(s2);cout<<s1<<endl;#include using namespace std;int main(){ string s1 = "a"; string s2 = "b"; s1.append(s2); cout<<s1<<endl; string s3 = "a"; string原创 2017-09-11 22:09:53 · 1505 阅读 · 0 评论 -
C++ pair,map,vector简单用法
#include #include #include using namespace std;int main(){ pair p1; p1 = std::make_pair(1,2.5); cout<<p1.first<<endl<<p1.second<<endl; pair p2(2,4.56); cout<<p2.first<<endl<<p2.second<<end原创 2017-09-01 15:04:38 · 1459 阅读 · 0 评论 -
C++中vector的用法详解
vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的. 用法: 1.文件包含: 首先在程序开头处加上#include以包含所需要的类文件vector 还有一定要加上using name转载 2017-09-01 14:49:02 · 877 阅读 · 0 评论 -
Kolakoski序列--输入任意数,输出前n个数
package com.main;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class Main { //n表示输出序列的前多少个元素,m表示输入几个数,array表示输入的数组 public void outPutKolakoski(int n,原创 2017-08-29 19:49:39 · 878 阅读 · 0 评论 -
vector insert用法 C++
#include #include using namespace std; int main() { vector v(3); v[0]=2; //v[0]是第0个元素 v[1]=7; v[2]=9; v.insert(v.begin(),8);//在最前面插入新元素。 v.insert(v.begin()+原创 2017-08-29 13:02:10 · 99763 阅读 · 3 评论 -
C++,vs中pow和宏的使用
#include using namespace std;//#define Mul ++x*y++ //错的,必须要是Mul(x,y)#define Mul(x,y) ++x*y++int main(){//cout<<pow(10,2);错误cout<<pow((float)10,2)<<endl;cout<<pow((double)10,2)<<endl;in原创 2017-09-13 18:00:17 · 1258 阅读 · 0 评论 -
C/C++知识要点5——智能指针原理及自定义实现
智能指针概述:智能指针用来管理动态对象。其行为类似于常规指针,重要的区别是:它负责自动释放所指向的对象。C++ 11标准库提供两种智能指针:shared_ptr、unique_ptr区别是:shared_ptr允许多个指针指向同一个对象;unique_ptr则独占所指向的对象。另外,还有一种weak_ptr的伴随类,它是一种弱引用,指向shared_ptr所管理的对转载 2017-08-09 23:25:06 · 861 阅读 · 0 评论 -
LeetCode 8. String to Integer (atoi) C++ --字符串转为数字,包含正负号、空格、字母、数字等字符
Implementatoito convert a string to an integer.Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input ca原创 2017-08-09 12:26:10 · 1232 阅读 · 0 评论 -
把**a*b**c***d**转换为**********abcd
// JIEFA_2.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include #include using namespace std;void swap(char *a,char *b){ char *temp; *temp=*a;*a=*b;*b=*temp;}//void swap(char a,char原创 2016-04-05 21:33:44 · 1152 阅读 · 0 评论 -
C语言getch(),getchar(),getche()区别实例
#include "stdafx.h"#include #include #include int _tmain(int argc, _TCHAR* argv[]){ char ch1,ch2,ch3; ch1=getch();/////输入一个字符不显示,直接结束, //putchar(ch1); //ch2=getchar(); //putchar(原创 2016-03-22 17:42:23 · 4443 阅读 · 0 评论 -
C语言,函数指针与指针函数
#include "stdafx.h"#include using namespace std;char* func(char* s1,char* s2){if(strcmp(s1,s2)>=0){return s1;}else{return s2;}}int _tmain(int argc, _TCHAR* argv[]){char *str1="abcdef"; //原创 2016-04-02 17:50:01 · 324 阅读 · 0 评论 -
C语言,数组做形参
// Array_2.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include using namespace std;void travel(int *p){///void travel(int p[5]){ 也正确 int i; for(i=0;i<5;i++){ //cout<<*(p+i)<<endl; ////正确原创 2016-04-02 16:56:31 · 3846 阅读 · 0 评论 -
C/C++读入写出空格和\0的区别
// Struct_1.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include #include using namespace std;struct stu { char *name; int score;}stus[3]={{"Tom",2},{"Dave",3},{"Tony",4}};void av原创 2016-04-02 16:06:23 · 1431 阅读 · 0 评论 -
C,数组指针,*(a+i)与*a+i;
#include "StdAfx.h"#include #include float avg(float *a){ float sum=0; int i=0; for(i=0;i<5;i++){ //sum+=*(a+i); sum+=*a+i; ////每次都是a[0]+i,,,,, printf("sum=%f\n",sum); } return sum/5;}in原创 2016-03-12 21:33:37 · 3544 阅读 · 0 评论 -
实现power(base,exponent),不用库函数和for循环
// 11_Powre_1.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include double power(double base,unsigned int exponent){ if(exponent==0){ return 1; }if(exponent==1){ return base; } //原创 2016-04-02 10:45:45 · 1167 阅读 · 0 评论 -
C语言正负数右移左移
int main(){ int a=7; printf("7>2=%d\t\n",a>2); int b=-7; printf("-7>2=%d\n",b>2); printf("-8>>2=%d,-9>>2=%d,-10>>2=%d\n",-8>>2,-9>>2,-10>>2); printf("-8<<2=%d,-9<<2=%d,-10<<2=%d\n",-8<<2,-9<<原创 2016-03-17 10:22:33 · 2531 阅读 · 2 评论 -
红黑树插入算法实现
参考文件: http://www.soft568.com/info/detail/4-11258.html在VC环境下编译。主体语言是C,用到了点C++语法。 由于红黑树中的元素必须各不相同,定义了一个时间复杂度仅为O(n)的随机数产生函数:void Random(int *A, int max, int length);该函数能返回一个长度为length,元素随机分布在0~max之间的数组A。转载 2015-11-06 21:16:34 · 599 阅读 · 0 评论 -
红黑树的插入C实现
#include #include #include using namespace std;#define red 0 #define black 1#define SIZE 10#define MAX 100typedef struct RB{ int key; int color; struct RB *ptParent; struct RB *转载 2015-11-06 21:03:42 · 371 阅读 · 0 评论 -
数字与字母乱序输入a34b56c7777d8,数字在前字母在后输出345677778abcd
// JIEFA_2.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include #include using namespace std;void swap(char *a,char *b){ char *temp; *temp=*a;*a=*b;*b=*temp;}void movestr(char *str原创 2016-04-05 23:13:20 · 1207 阅读 · 0 评论 -
*(p+i) ,C语言数组指针_C语言中文网
一个变量有一个地址,一个数组包含若干元素,每个数组元素都在内存中占用存储单元,它们都有相应的地址。所谓数组的指针是指数组的起始地址,数组元素的指针是数组元素的地址。字节是内存中的最小可操作单位,我们通常将一个字节称为一个内存单元。一个数组是由连续的一块内存单元组成的。数组名就是这块连续内存单元的首地址。一个数组也是由各个数组元素(下标变量)组成的。每个数组元素按其类型不同占有几个连续的原创 2016-03-12 20:47:38 · 7884 阅读 · 4 评论 -
LeetCode 7. Reverse Integer C++--带正负号数字反转
Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321click to show spoilers.Note:The input is assumed to be a 32-bit signed integer. Your function should原创 2017-08-09 10:29:30 · 1319 阅读 · 0 评论 -
回文子序列个数 C++
回文子序列个数要求:给定字符串,求它的回文子序列个数。回文子序列反转字符顺序后仍然与原序列相同。例如字符串aba中,回文子序列为"a", "a", "aa", "b", "aba",共5个。内容相同位置不同的子序列算不同的子序列。#include #include using namespace std;int NumOfPalindromeSubSequen原创 2017-08-08 16:12:20 · 1435 阅读 · 0 评论 -
Java与C++区别--函数默认参数
默认参数与缺省参数一个意思Java取消了默认函数参数,增加了函数重载,所以缺少某个参数时会调用另一个方法,不会调用不缺少这个参数的方法。而C++会仍然调用不缺少这个参数的方法。#include using namespace std; int sum(int a, int b=20){ int result; result = a + b; return (r原创 2017-06-08 22:11:13 · 478 阅读 · 0 评论 -
C++ 中的运算符重载
您可以重定义或重载大部分 C++ 内置的运算符。这样,您就能使用自定义类型的运算符。重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。Box operator+(const Box&);声明加法运算符用于把两个 Box 对象相加,返回最终的 Box 对象。大多数的重载运算转载 2017-06-08 21:59:20 · 481 阅读 · 0 评论