C编程小题

来源:互联网 发布:达内大数据百度云盘 编辑:程序博客网 时间:2024/04/25 05:21


1:数组越界和unsigned char

#define MAX 255void main(){unsigned char i;  unsigned char A[MAX];  for(i = 0; i <= MAX; i++)//i达到255后,加1变为0,无线循环下去  {    A[i] = i;    printf("*%d*",A[i]);  }}

两个问题:数字越界和无限循环,char的范围[-128,127],unsigned char [0,255],

--------------------------------------------------------------------------------------------------------------分割线----------------------------------------------------------------------------------------------------------------

2:程序

void GetMemory(char *p){    p = (char *)malloc(100);}void Test(void){    char *str = NULL;    GetMemory(&str);    strcpy(str,"hello world");    printf("%s",str);}
void GetMemory(char **p,int num){    *p = (char *)malloc(num);}void Test(void){    char *str = NULL;    GetMemory(&str,100);    strcpy(str,"hello world");    printf("%s",str);}

注意这些对内存的考察主要集中在,指针的理解和变量的生存期及作用范围,良好的动态内存申请,释放习惯,free,delete后置空是最常见的操作

--------------------------------------------------------------------------------------------------------------分割线----------------------------------------------------------------------------------------------------------------

3:指针问题

swap(){   int *p;//p是一个野指针,应该为int p   *p = *p1;    *p1=*p2;    *p2 = *p;}

--------------------------------------------------------------------------------------------------------------分割线----------------------------------------------------------------------------------------------------------------

4:冒泡排序

void BubleSort(int p[],int n){    int i = 0;    int j = 0;    int change_flag = 0;    int temp = 0;    for(j = n; j > 0;j--)    {        for(i = 0; i < j; i++)        {            if(p[i] > p[i+1])            {                temp = p[i];                p[i] = p[i+1];                p[i+1] = temp;                  change_flag = 1;            }          }        if(change_flag == 0)break;    }}

--------------------------------------------------------------------------------------------------------------分割线----------------------------------------------------------------------------------------------------------------

5:进制转换

十进制数N和其他进制数的转换,其解决方法有很多,其中一个简单算法基于下列原理: N = (N div D)*D  + N mod D

例如:十进制1348 转8进制等于2504,

N          N div 8      N mod 8

1348      168             4

16821    0

212    5

20    2

注意有个倒置的过程可以利用栈结构

void conversion(){InitStack(S);while (N) {Push(S,N%8);N = N / 8;}while (!StackEmpty(S)) {Pop(S,e);printf("%d",e);}}

m进制转为n进制

void m2n(int m, char* mNum, int n, char* nNum)   {      int i = 0;      char c, *p = nNum;        //这是一个考察地方,是否能用最少乘法次数。      while (*mNum != '\0')          i = i*m + *mNum++ - '0';            //辗转取余      while (i) {          *p++ = i % n + '0';          i /= n;      }      *p-- = '\0';        //逆置余数序列      while (p > nNum) {          c = *p;          *p-- = *nNum;          *nNum++ = c;      }  }  

--------------------------------------------------------------------------------------------------------------分割线---------------------------------------------------------------------------------------------------------------

6.求阶 阶乘问题的递归算法

long int fact(int n){if(n == 0|| n == 1)return 1;elsereturn n*fact(n - 1);}

7c++小题

#include <iostream>using namespace std;class human{public:human(){cout<<"构造"<<endl;human_num++;}; //默认构造函数static int human_num;//静态数据成员~human(){cout<<"析构函数"<<endl;human_num--;print();}void print(){cout<<"human num is:"<<human_num<<endl;}protected:private:};int human::human_num = 0;//类中静态数据成员在外部定义,仅定义一次human f1(human x){x.print();return x;}int main(){human h1;h1.print();cout<<"-----1----"<<endl;human h2 = f1(h1);//先调用f1(),输出human_num:1,而后输出human_num 为0,f1是按值传递对象,调用默认的复制构造函数,h2没有调用定义的构造函数。cout<<"-----2----"<<endl;h2.print();system("pause");return 0 ;}

结果:

构造
human num is:1
-----1----
human num is:1
析构函数 //return 调用析构函数
human num is:0
-----2----
human num is:0
请按任意键继续. .

--------------------------------------------------------------------------------------------------------------分割线---------------------------------------------------------------------------------------------------------------

8:大数相乘

#include <iostream>using namespace std;#define N 30int a[N],b[N],c[2*N];void strToNum(int *p,string &s)//字符串放入数组{for (int i = 0; i <=(s.length() - 1);i++){p[i] = s[s.length() - 1 - i] - '0';}}void multiplay(string&s1,string &s2)//相乘{strToNum(a,s1);strToNum(b,s2);for (int i = 0; i < N; i++)cout<<a[i];cout<<endl;for (int i = 0; i < N; i++)cout<<b[i];cout<<endl;for (int i = 0; i < 2*N; i++ ){c[i] = 0;}for (int i = 0; i < N; i++)for (int j = 0; j < N; j++)c[i+j] += a[i]*b[j]; //这句是大数相乘的关键for (int i = 0; i < 2*N-1; i++)//处理进位{int m = c[i]/10;c[i] = c[i]%10;c[i+1] += m;}}int main(){string s = "7898912";string s1 = "12312";multiplay(s,s1);for (int i = 2*N - 1; i >= 0;i--){cout<<c[i];}system("pause");return 0;}:

9:子数组最大值问题

#include <iostream>using namespace std;int a[10] = {1,3,-4,5,9,-10,3,4,1,-5};int summax(int *a,int len){int nStart = a[len - 1];int nAll = a[len - 1];for (int i = len - 2;i>=0; i--){if (nStart < 0)nStart = 0;nStart += a[i];if (nStart > nAll)nAll = nStart;}return nStart;}int main(){int m = summax(a,10);cout<<m<<endl;system("pause");return 0;}