c++01


intarray[10]={1,2,3,4,5,6};

for(inti=0;i<10;i++)

cout<< array[i];


定义一个数组,他的下标就是就代表其中每个元素!



**************************************************


complexc99中是一种数据类型,编译的时候要加-lm选项


_Complexdouble z1 = -1; // C99

doublecomplex sz1;//gnu gcc extension


sz1= csqrt(z1); //C99 and in math library, build with -lm


complexC++中被定义为class,两者不能兼容!


complex<double> z1 = complex<double>(1,2);

complex<double>sz1;


*****************************************************


如何找到字符串结尾?

char*s1, char*s2


while(*s1) //找到字符串结尾的方法!!

s1++;

len= strlen(s2);


while(*s2&& len) //这里将把s2字符串的len长度复制到s1后面

{

*s1 = *s2;

s1++;

s2++;

len--;

}


******************************************************


charca[]= "abc\012\0x34";

//注意\012表示一个字节,而\0x34则表示已经到了结尾,所以一共a,b,c,\012,4个字节

//其实如果\x之间没有0的话,则最后几个字节都是算得!!!!!!!!!!!


cout<< "Sizeis "<< strlen(ca)<<endl;

所以ca的大小是4个字节


******************************************************


overload


同一个函数名,不同的定义,在调用的时候会根据参数类型不同,数量不同自动判断

#include<iostream>

usingnamespacestd;


voidf(inti); // integer parameter

voidf(inti, intj); // two integer parameters

voidf(doublek); // one double parameter


intmain()

{

f(10); //call f(int)

f(10, 20); //call f(int, int)

f(12.23); //call f(double)

return0;

}


voidf(inti)

{

cout<< "In f(int), i is "<< i << '\n';

}

voidf(inti, intj)

{

cout<< "In f(int, int), i is "<< i;

cout<< ", j is "<< j << '\n';

}

voidf(doublek)

{

cout<< "In f(double), k is "<< k << '\n';

}


*****************************************************


关于reference的使用!


double&f();


doubleval = 100.0;


intmain()

{

doublenewval;

cout<< f() << '\n';// display val's value

newval = f();// assign value of val to newval

cout<< newval << '\n';// display newval's value

f()= 99.1; // change val's value

cout<< val<< '\n';// display val's new value

//因为是引用,所以将原来的变量也该变了!!!!

//所以最后的val=99.1


return0;

}


double&f()

{

returnval; // return reference to val

}


***********************************************************


关于有符号及无符号的区别:


shortinti; // a signed short integer

shortunsignedintj; // an unsigned short integer


j= 32768; //无符号

i= j; //有符号,i变为负值!!

cout<< i << ""<< j <<endl;

//short类型,无符号的是16位,216次方是65536,所以范围是到65535

//有符号时,去掉符号位还剩15位,215次方是32768,所以范围是32767

//当进行赋值时,如果有符号的数大于32767,有符号的将会变成负数!!!!!


************************************************************


同样的数组下标问题:

对某一下标元素的改变将会改变字符串对应的字母,注意下标从0开始!


charact[]= "Learn";

cout<< act << " "<< name <<endl;

name[5]='';

cout<<"Name was changed"<<endl;

act[2]='';

cout<< act << " "<< name <<endl;


如果被定义成constcharact[] = "Learn";

因为有const,不能对其进行改变!


******************************************************************


将两变量定义为指针类型,利用取地址符&进行交换


#include<iostream>

usingnamespacestd;


//Declare swap() using pointers.

voidswap(int*x, int*y);


intmain()

{

inti, j;


i= 10;

j= 20;


cout<< "initial values of i andj: ";

cout<< i << ' '<< j << '\n';

swap(&j,&i); // call swap() with addressesof i and j

cout<< "swapped values of i andj: ";

cout<< i << ' '<< j << '\n';


return0;

}


//Exchange arguments.

voidswap(int*x, int*y)

{

inttemp;


temp= *x; // save the value at address x

*x= *y; // put y into x

*y= temp; // put x into y

}


****************************************************************


将两个变量定义成reference进行交换:


#include<iostream>

usingnamespacestd;


//Declare swap() using reference parameters.

voidswap(int&x, int&y);


intmain()

{

inti, j;


i= 10;

j= 20;


cout<< "initial values of i andj: ";

cout<< i << ' '<< j << '\n';

swap(j, i);

cout<< "swapped values of i andj: ";

cout<< i << ' '<< j << '\n';


return0;

}


/*Here, swap() is defined as using call-by-reference,

notcall-by-value. Thus, it can exchange the two

arguments itis called with.

*/

voidswap(int&x, int&y)

{

inttemp;


temp= x; // save the value at address x

x= y; // put y into x

y= temp; // put x into y

}


***************************************************


使用union交换比特位的方式,注意后面的位与方法,底层经常用到:


#include<iostream>

usingnamespacestd;


voiddisp_binary(unsignedu);


unionswap_bytes{ //union结构特性的一种使用

shortintnum; //short类型2个子节,16

charch[4]; //char类型1个子节,8

};


intmain()

{

swap_bytessb;

chartemp;


sb.num= 15 ; // binary: 0000 0000 0000 00000000 0000 0000 1111


cout<< "Original bytes: ";

disp_binary(sb.ch[1]);

cout<< " ";

disp_binary(sb.ch[0]);

cout<< "\n\n";



//exchange the bytes

temp= sb.ch[0];

sb.ch[0]= sb.ch[1];

sb.ch[1]= temp;


cout<< "Exchanged bytes: ";

disp_binary(sb.ch[1]);

cout<< " ";

disp_binary(sb.ch[0]);

cout<< "\n\n";


return0;

}


//Display the bits within a byte. 按照字节输出比特位!

voiddisp_binary(unsignedu)

{

registerintt;


for(t=512;t>0; t=t/2) //这里t的值决定打印出来的位数,28次方是256,所以打印出来时是 //9位!!!!

if(u& t) cout << "1 "; //这里用的方法是位与

elsecout << "0 ";

}


***************************************************



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c 01背包问题是一种经典的背包问题,其中给定一个背包的容量和一组物品,每个物品有一个重量和一个价值。目标是选择物品放入背包中,使得放入的物品总重量不超过背包容量,并且总价值最大化。 采药问题是c 01背包问题的一个具体应用场景。在采药问题中,给定一个草药园和一些草药的价值和采摘时间,每种草药在一定的时间内可以采摘得到一定的价值。采药者只有有限的时间来采摘草药,他需要选择哪些草药采摘,并且使得采摘的草药的总价值最大化。 思路和解法可以参考引用和引用中提供的链接和提示。其中,引用提供了洛谷和AcWing中关于c 01背包问题的其他约束条件和实现方式的说明,引用提供了优化思路和解法的具体描述。 总结来说,c 01背包采药问题是一个经典的背包问题的具体应用,目标是在有限时间内选择草药使得总价值最大化。具体的解法可以参考引用和引用中提供的链接和提示。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [[C++]背包问题(1):洛谷 采药 01背包模型详解](https://blog.csdn.net/weixin_62712365/article/details/124640545)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [[AcWing] 423. 采药(C++实现)01背包问题](https://blog.csdn.net/weixin_43972154/article/details/124313102)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值