Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1222266
  • 博文数量: 105
  • 博客积分: 127
  • 博客等级: 入伍新兵
  • 技术积分: 962
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-29 15:22
文章分类

全部博文(105)

文章存档

2021年(1)

2019年(3)

2018年(1)

2017年(11)

2016年(47)

2015年(32)

2014年(4)

2012年(6)

我的朋友

分类: LINUX

2016-06-15 12:38:06

转载地址:http://blog.csdn.net/evilcode/article/details/8301983

仅供参考,免责声明。

1. 判断一个整数是否是2的N次方


[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include "stdio.h"  
  3.   
  4. bool IsPowerOfTwo(int x)  
  5. {  
  6.     return (x > 0) && ((x & (x - 1)) == 0);  
  7. }  
  8. void main()  
  9. {  
  10.     int a = 0;  
  11.     bool result = false;  
  12.       
  13.     printf("Input a integer:");  
  14.     scanf("%d",&a);  
  15.     result = IsPowerOfTwo(a);  
  16.     printf("%s\n",result ? "true":"false");  
  17. }  


2. 关于传值调用


[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include "stdio.h"  
  3. void Swap(int a, int b)  
  4. {  
  5.     int temp;  
  6.   
  7.     temp = a;  
  8.     b = a;  
  9.     a = temp;     
  10. }  
  11.   
  12. void main()  
  13. {  
  14.     int a = 3, b = 5;  
  15.     Swap(a, b);  
  16.     printf("a = %d,b = %d\n", a, b);  
  17. }  

题目简单,知识点:函数调用中发生的数据传送是单向的。 即只能把实参的值传送给形参,而不能把形参的值反向地传送给实参。 因此在函数调用过程中,形参的值发生改变,而实参中的值不会变化。

所以结果是a = 3,b = 5

需要修改的话,传引用调用即可。

同样的题目还有:


[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include "stdio.h"  
  3. void test(char *p)  
  4. {  
  5.     p+=3;  
  6. }  
  7.   
  8. int main()  
  9. {  
  10.     char *p = "hello";  
  11.   
  12.     printf("%s-",p);  
  13.     test(p);  
  14.     printf("%s\n",p);  
  15.   
  16.     return 0;  
  17. }  

同样答案为 hello-hello


3. 将字符串逆序


[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include "string.h"  
  3.   
  4. void Swap(char &a, char &b)  
  5. {  
  6.     a = a ^ b;  
  7.     b = a ^ b;//(a ^ b) ^ b = a  
  8.     a = a ^ b;//(a ^ b) ^ a = b  
  9. }  
  10.   
  11. void Reverse(char *str)  
  12. {  
  13.     unsigned int length;  
  14.     unsigned int i = 0;  
  15.   
  16.     length = strlen(str);  
  17.       
  18.     //printf("[debug]string length is %d\n",length);  
  19.   
  20.     for(i = 0; i < length / 2; i++)  
  21.     {  
  22.         //XOR  
  23.         Swap(str[i], str[length - i - 1]);  
  24.     }  
  25. }  
  26.   
  27. void main()  
  28. {  
  29.     char string[200];//不要用char *str="hello"; 这样直接存在常量区了。  
  30.   
  31.     printf("Input String:\n");  
  32.     gets(string);  
  33.     Reverse(string);  
  34.     printf("After reverse:\n%s\n",string);  
  35. }  

4. 用二进制表示int型负数 (例 -1,-5)

概念:
1、原码:一个正数,按照绝对值大小转换成的二进制数;一个负数按照绝对值大小转换成的二进制数,然后最高位补1,称为原码。
比如 00000000 00000000 00000000 00000101 是 5的 原码。
     10000000 00000000 00000000 00000101 是 -5的 原码。
2、反码:正数的反码与原码相同,负数的反码为对该数的原码除符号位外各位取反[每一位取反(除符号位)]。
取反操作指:原为1,得0;原为0,得1。(1变0; 0变1)
比如:正数00000000 00000000 00000000 00000101  的反码还是 00000000 00000000 00000000 00000101
      负数10000000 00000000 00000000 00000101  的反码则是 11111111 11111111 11111111 11111010。
3、补码:正数的补码与原码相同,负数的补码为对该数的原码除符号位外各位取反,然后在最后一位加1.
比如:
10000000 00000000 00000000 00000101 的补码是:11111111 11111111 11111111 11111010。
那么,补码为:
11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111 11111011
所以,-5 在计算机中表达为:11111111 11111111 11111111 11111011。转换为十六进制:0xFFFFFFFB
我们来看整数-1在计算机中如何表示。假设这也是一个int类型,那么:
1、先取-1的原码:10000000 00000000 00000000 00000001
2、得反码:     11111111 11111111 11111111 11111110(除符号位按位取反)
3、得补码:     11111111 11111111 11111111 11111111


可见,-1在计算机里用二进制表达就是全1。16进制为:0xFFFFFF


5. 用C++函数模版写三个数中的最大数(int,double,long为例)

备注:尼玛,模版都不会用?翻翻书去!


[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include "iostream.h"  
  3.   
  4. template <typename T>  
  5. T max(T num1,T num2,T num3)  
  6. {  
  7.     T max = 0;  
  8.   
  9.     max = num1 > num2 ? num1:num2;  
  10.     max = max  > num3 ? max:num3;  
  11.   
  12.     return max;  
  13. }  
  14.   
  15.   
  16. void main()  
  17. {  
  18.     int i1 = -123, i2 = 0, i3 = 321, i_max;  
  19.     double d1= -11.12, d2 = 0.03, d3 = 14.14, d_max;  
  20.     long l1 = -890, l2 = 0, l3 = 67849321, l_max;  
  21.   
  22.     i_max = max(i1, i2, i3);  
  23.     d_max = max(d1, d2, d3);  
  24.     l_max = max(l1, l2, l3);  
  25.       
  26.     cout<<"i_max = "<
  27.     cout<<"d_max = "<
  28.     cout<<"l_max = "<
  29. }  

6.存储类型auto,static,extern,register的区别


下面这位仁兄写了很多。

http://blog.csdn.net/firefly_2002/article/details/7940802

7. 2的N次方引出的。


[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include "iostream.h"  
  3.   
  4. int count = 0;  
  5. int x = 2007;  
  6. void main()  
  7. {  
  8.     while(x)  
  9.     {  
  10.         count++;  
  11.         x = x&(x-1);  
  12.         printf("x = %d\n",x);  
  13.     }  
  14.   
  15.     printf("count = %d\n",count);  
  16. }  

count的值是多少?


x &(x-1)这个很熟悉吧。。。2的N次方就这样判断的。

x = x&(x-1)

会慢慢减小,能发现其中规律。实际测试printf打印内容x值为2006,2004,2000,1984,1920,1792,1536,1024,0 count值为9

x = x &(x-1)会逐渐减小到离它近的2的N次方数。

(2007-1024)= 983 = 512 + 256 + 128 + ..... + 1 = 2^0 + ... + 2^9  , count = 9;

有没有更清晰的解法,请回复,我这是打印出结果才分析的。


8. char *str和char str[]区别
[cpp] view plain copy
  1. "font-size:14px;">#include "stdafx.h"  
  2. #include  
  3.   
  4. char *str()  
  5. {  
  6.     char *str="hejk ";  
  7.     return str;  
  8. }  
  9.   
  10. void main()  
  11. {  
  12.     str();  
  13. }  
[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include  
  3.   
  4. char *str()  
  5. {  
  6.     char str[]="hejk ";  
  7.     return str;  
  8. }  
  9.   
  10. void main()  
  11. {  
  12.     str();  
  13. }  

其中程序二编译时就会报错,warning C4172: returning address of local variable or temporary
程序一是存在常量区的,程序二是存在栈上
需要补充知识点
一、预备知识----程序的内存分配

一个由C/C++编译的程序占用的内存分为以下几个部分

1、栈区(stack):由编译器自动分配释放,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。
2、堆区(heap):一般由程序员分配释放,若程序员不释放,程序结束时可能由OS回收。
3、全局区(静态区)(static):全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域;未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。程序结束后由系统释放。
4、文字常量区:常量字符串就是放在这里的。程序结束后由系统释放。
5、程序代码区
下面有个例子:
[cpp] view plain copy
  1. //main.cpp  
  2. int a=0;    //全局初始化区  
  3. char *p1; //全局未初始化区  
  4. main()  
  5. {  
  6.     int b;                  //栈  
  7.     char s[]="abc";  //栈  
  8.     char *p2;           //栈  
  9.     char *p3="123456";   //123456\0在常量区,p3在栈上。  
  10.     static int c=0;     //全局(静态)初始化区  
  11.     p1 = (char*)malloc(10);  
  12.     p2 = (char*)malloc(20);   //分配得来得10和20字节的区域就在堆区。  
  13.     strcpy(p1,"123456");   //123456\0放在常量区,编译器可能会将它与p3所向"123456"优化成一个地方。  
  14. }  

9. 如何判断CPU是大端还是小端?
[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include   
  3.   
  4. union u_T  
  5. {  
  6.     short s;  
  7.     char c[2];  
  8. };  
  9.   
  10. int main()  
  11. {  
  12.     u_T u;  
  13.   
  14.     if(sizeof(short) == 2)  
  15.     {  
  16.         u.s = 0x0102;  
  17.         if(u.c[0] == 1 && u.c[1] == 2)  
  18.         {  
  19.             printf("big enidan\n");  
  20.         }else if(u.c[0] == 2 && u.c[1] == 1)  
  21.         {  
  22.             printf("little endian.\n");  
  23.         }  
  24.         return 0;  
  25.     }  
  26.   
  27.     return -1;  
  28. }  

10. 进栈出栈
摘自:
设栈S的初始状态为空,元素a,b,c,d,e,f,g依次入栈,以下出栈序列不可能出现的是( )。
A.a,b,c,e,d,f,g  B.b,c,a,f,e,g,d  C.a,e,d,c,b,f,g
D.d,c,f,e,b,a,g  E.g,e,f,d,c,b,a
答案是E。
标准答案讲得很好,依次进栈的意思说明不是一次性进完,是有进进出出的。(不要邪恶!)
栈的定义:栈是一种特殊的表这种表只在表头进行插入和删除操作。因此,表头对于栈来说具有特殊的意义,称为栈顶。相应地,表尾称为栈底。不含任何元素的栈称为空栈。 
栈的逻辑结构:假设一个栈S中的元素为an,an-1,..,a1,则称a1为栈底元素,an为栈顶元 素。栈中的元素按a1 ,a2,..,an-1,an的次序进栈。在任何时候,出栈的元素都是栈顶元素。换句话说,栈的修改是按后进先出的原则进行的.因此,栈又称为后进先出(Last In First Out)表,简称为LIFO表。所以,只要问题满足LIFO原则,就可以使用栈。 
notice:换句话说,栈就是可以一个元素进后,可以接着进行输出的表.
这道题各个选项的进出次序为:
A:进,出,进,出,进,出,进,进,出,出,进,出,进,出
B:进,进,出,进,出,出,进,进,进,出,出,进,出,出
C:进,出,进,进,进,进,出,出,出,出,进,出,进,出
D:进,进,进,进,出,出,进,进,出,出,出,出,进,出
E:错误.原因自己仿照上面做做看.

11.有N阶的台阶,一个人每次只能走一阶或两阶,用一个递归算法求出共有多少种走法。
参考http://2274594.blog.51cto.com/2264594/422225
假设共有i阶台阶, 走完所有的台阶有n种走法,则:
1. i  = 1时,  n = 1;   {1}
2. i =  2,       n = 2;   {[1, 1],  [2]}
3. i =  3,       n = 3;   {[1, 1, 1],  [1, 2], [2, 1]}
4. i =  4,       n = 5;   {[1, 1, 1, 1], [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2]}
5. i =  5,       n = 8;    ............
.........................................
.........................................
因此,我们可以得到规律
f(n) = f(n-1) + f(n-2)  (n>=3), 从而得到递归算法。

12.sizeof和strlen,今天栽跟头的地方。
[cpp] view plain copy
  1. #include   
  2. #include   
  3.   
  4. void test0(const char *p)  
  5. {  
  6.     printf("%d ",sizeof(p));  
  7.     printf("%d ",strlen(p));  
  8. }  
  9.   
  10. void test1(char p[])  
  11. {  
  12.     printf("%d ",sizeof(p));  
  13.     printf("%d ",strlen(p));  
  14. }  
  15.   
  16. void main()  
  17. {  
  18.     char *s = NULL;  
  19.     char str[]="1234567";  
  20.   
  21.     printf("%d ",sizeof(s));  
  22.     printf("%d ",sizeof(str));  
  23.     printf("%d ",strlen(str));  
  24.   
  25.     printf("%d ",sizeof((const char*)str) );  
  26.     printf("%d ",strlen((const char*)str) );  
  27.   
  28.     test0(str);  
  29.     test1(str);  
  30. }  
结果是4 8 7 4 7 4 7 4 7
请自行分析。
13. 结构体的SIZE
[cpp] view plain copy
  1. struct test_T  
  2. {  
  3.         char test0;  
  4.         char *p;  
  5.         int int_32_1;  
  6.         int int_32_2;  
  7.         char test1;  
  8.         char test2;  
  9.         int int_32_3;  
  10. }t1;  

请问sizeof(t1)为多少?在GCC默认参数配置前提下。
使用默认参数测试是24.
[cpp] view plain copy
  1. struct test_T  
  2. {  
  3.         char test0;     //4  
  4.         char *p;        //4  
  5.         int int_32_1;   //4  
  6.         int int_32_2;   //4  
  7.         char test1;  
  8.         char test2;     //一共4  
  9.         int int_32_3;   //4  
  10. }t1;  
阅读(1323) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~