• 吉林大学高级程序设计(红皮书例题)(1~7章)


    1、有n个学生,学生信息包括学号、姓名、成绩要求从高到低输出各个学生信息

    #include
    
    
    struct Student{
    	
    	int num;
    	char name[20];
    	float score;
    }; 
    
    
    
    int main(){
    	
        struct Student stu[5]={{10101,"zhang",78},{10102,"wang",98.5},{10106,"ling",73.6},{10117,"sun",100},{10118,"li",86}};
    	Student temp;
    	int n=5,i,j;
    	for(i=0;i<n-1;i++)//冒泡排序 
    	{
    		for(j=0;j<n-i-1;j++)
    		{
    			if(stu[j].score<stu[j+1].score){
    				temp=stu[j];
    				stu[j]=stu[j+1];
    				stu[j+1]=temp; 
    			}
    		}
    	}
    	for(i=0;i<n-1;i++)printf("%d  %s  %.2lf\n",stu[i].num,stu[i].name,stu[i].score); 
    	
    	
    	
    	return 0; 
    }
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    typedef 取别名这两种结构体定义一致

    typedef struct Student{
    	
    	int num;
    	char name[20];
    	float score;
    }Student; 
    
    
    
    int main(){
    	
        Student stu[5]={{10101,"zhang",78},{10102,"wang",98.5},{10106,"ling",73.6},{10117,"sun",100},{10118,"li",86}};
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2、求出100~200以内的素数

    #include
    #include
    
    
    //质素 
    int isPrime(int n)
    {	
    	for(int i=2;i<=sqrt(n);i++)
    	{
    		if(n%2==0)return 0; 
    	} 
    	
    	return 1; 
     } 
    
    
    
    
    
    int main(){
    	
    	
    	int i,j;
    	int count=0;
    	for(int i=101;i<=200;i++)
    	{
    		
    		if(isPrime(i)) {
    			count++;
    			printf("%d  ",i);
    			if(count%5==0)printf("\n");//每五个换行 
    		}	
    	 } 
      
    	
    	
    	return 0; 
    }
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    3、求出一个数的所有约数之和

    #include
    #include
    
    
    int main(){
    	
    	
    	int n,sum;
    	printf("请输入一个正数: ");
    	scanf("%d",&n);//18=1+2+3+6+9+18
    	sum=0;
    	for(int i=1;i<=n;i++)
    	{
    		if(n%i==0)sum+=i; 
    	 } 
    	 
    	 printf("约数和为:%d\n",sum); //39
      
    	
    	
    	return 0; 
    }
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    找出1000以内完全数即n等于所有因子之和 例如6=1+2+3

    #include
    #include
    
    
    int main() {
    
    
    	int i,n;
    	int factors[1000];//存放因子
    	int count;//统计因子个数
    	int sum;
    
    	for(n=1; n<=1000; n++) {// 6   28   496
    
    		count=0;
    		sum=0;
    		for(i=1; i<n; i++) {
    			if(n%i==0) {
    				sum+=i;
    				factors[count++]=i;
    			}
    		}
    
    		if(sum==n) {
    			printf("%dits factors are",n);
    			for(i=0; i<count; i++)
    				printf("%d ",factors[i]);
    
    			printf("\n");
    		}
    
    	}
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    4、将一个数表示为质因数的乘积形式 例如90=2x3x3x5

    #include
    #include
    
    
    
    
    int main() {
    
    
        int i,n;
    	printf("请输入n:");
    	scanf("%d",&n);
    	printf("%d=",n);
    	for(i=2;i<n;i++) //90=2*3*3*5
    	{
    		while(i!=n)//求出前k-1个质因数 
    		{
    			if(n%i==0){ 
    				n/=i;
    				printf("%d*",i); 
    			}else break; 	
    		 } 
    	 } 
    	 printf("%d\n",n); //输出最后一个质数 
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    5、输出三位数中完全平方数,并且该数任意两位相同

    #include
    #include
    
    
    int  isSquare(int n) {
    
    	int i;
    	if(n==1||n==0)return 1;
    
    	i=sqrt(n);
    	if(i*i==n)return 1;
    	else return 0;
    }
    
    
    int main() {
    
    
    	int  i,a,b,c,num=100;
    
    	while(num<1000) {
    		if(isSquare(num)) {
               a=num/100; //百位 
               b=(num%100)/10; //十位 
               c=num%10;//个位 
               
               if(a==c||a==b||b==c)printf("%d  ",num); 
    		}
    		num++; 
    	}
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    6、输入立方和小于m的最大正整数

    #include
    #include
    
    
    
    
    int main() {
    
    
    	int m,i;
    	scanf("%d",&m);
    	for(i=0;i*i*i<m;i++);
    	
    	i--;
    	printf("%d",i); 
    	
    	
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    7、2019年软件专硕 求出前20对孪生素数对 (3,5) 后者比前者大2

    #include
    #include
    
    
    int  isPrime(int n) {
    
    	if(n==1||0)return 0;
    
    	for(int i=2; i<=sqrt(n); i++) {
    		if(n%i==0)return 0;
    	}
    	return 1;
    }
    
    
    int main() {
    
    
        int count=0;
        int n=3;
        
        while(count!=20)
        {
            if(isPrime(n)&&isPrime(n+2)){
            	count++;
            	printf("(%d,%d)\n",n,n+2);
    		}
    		n+=2;//素数直接考虑奇数 
    	}
    
    
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    8、输入年月日求出今年的第几天

    #include
    #include
    
    int  leap_year(int year) {
    	if(year%400==0||year%4==0&&year%100!=0) { //能被400整除或者能被4整除不能被100整除
    		return 1;
    	}
    	return 0;
    }
    
    
    int  fun(int year,int month,int day) {
    	int count=0;
    	int Month[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
    	Month[1]+=leap_year(year);
    	for(int i=0; i<month; i++)count +=Month[i];
    	count+=day;
    	return count;
    }
    
    int main() {
    
    
    	int year,month,day;
        scanf("%d%d%d",&year,&month,&day);
    	
    	printf("%d 年 %d月 %d日 是 该 年 第 %d 天\n",year,month,day,fun(year,month,day)); 
    
    
    
    
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    9、复数运算法则

    在这里插入图片描述

    在这里插入图片描述

    #include
    #include
    
    
    
    int main() {
    
    
        int  a1,b1,a2,b2,p,q,e,f; // a1+b1i   a2+b2i 
    	
    	printf("请输入复数");
    	scanf("%d%d%d%d",&a1,&b1,&a2,&b2);
    	p=a1+a2;//加法 
    	q=b1+b2;
    	e=a1*a2-b1*b2;//乘法 
    	f=a1*b2+a2*b1;
    	
    	printf("相加得%d+%di\n",p,q);
    	
    	printf("相乘得%d+%di\n",e,f); 
    	 
    
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    10、1、2、3、4可以组成多少不相同的三位数,无重复数字

    #include
    #include
    
    
    
    int main() {
    
    
        int i,j,k;
    	
    	for(i=1;i<5;i++)
    	  for(j=1;j<5;j++)
    	    for(k=1;k<5;k++){
    	    	if(i!=k&&i!=j&&k!=j){
    	    		printf("%d%d%d\n",i,j,k); 
    			} 
    		} 
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    11、将x,y,z由小到大输出

    #include
    #include
    
    
    
    int main() {
    
    
       int x,y,z,t;
       
       printf("请输入三个数字\n");
       scanf("%d%d%d",&x,&y,&z);
       
       if(x>y){ //小的向前移动 
       	  t=x;
    	  x=y;
    	  y=t; 
       } 
       
       if(x>z){//将x设置为最小 
       	 t=x;
    	 x=z;
    	 z=t; 
       } 
       
       if(y>z){ //找出次小 
       	 t=y;
    	 y=z;
    	 z=t; 
       } 
       
       printf("从小到大排序: %d %d %d",x,y,z); 
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    12、求出1到20阶乘之和1!+2!+…20!

    #include
    #include
    
    
    
    int main() {
    
    
    	int i;
    
    	long sum,mix=1;
    
    	for(int i=1; i<=20; i++) { 
    		mix=mix*i;
    		sum+=mix;
    	}
    
    	printf("%ld\n",sum);
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    12、水仙花数 n=百位三次方+十位三次方+个位三次方

    #include
    #include
    
    
    
    int main() {
    
    
    	int hum,ten,ind,n;
    
    	for(int n=100; n<1000; n++) {
    		hum=n/100;
    		ten=(n%100)/10;
    		ind=n%10; 
    		if(n==hum*hum*hum+ten*ten*ten+ind*ind*ind)printf("%d ",n); 
    	}
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    13、将一个各个为提取出来,分别求出对应阶乘,求和

    #include
    #include
    
    
    
    
    
    
    
    
    int main() {
    
    
      int n,sum;
      scanf("%d",&n);
      
      while(n)
      {
      	 int t=n%10;
      	 n/=10; 
    	 int k=1; 
    	 for(int i=1;i<=t;i++)k*=i;
    	 sum+=k; 
       } 
       
       printf("%d",sum); 
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    14、a+aa+aaa+…+aaaaa…a a每次乘以10然后加上原来的a一共n个

    #include
    #include
    
    
    
    
    int main() {
    
    
       int n,a;
       scanf("%d%d",&n,&a);// 3  2
       int sum=0; 
       int k=a; 
       for(int i=1;i<=n;i++)
       {
       	  sum+=k;
    	  k*=10;
    	  k+=a; 
       }
       printf("%d",sum); // 246
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    15、 求出一个数各个位存放数组,并求出各个位的加和

    #include
    #include
    
    
    
    
    int main() {
    
    
    	int n;
    
    	printf("请输入一个正整数: ");
    	scanf("%d",&n);
    	int m,i,count=0,a[20]= {0};
    	while(n) {
          m=n%10;
    	  n/=10;
    	  count+=m;
    	  a[i++]=m; 
    	}
    	printf("%d",count); 
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    16、数字逆转 123–>321

    #include
    #include
    
    
    
    
    
    
    
    
    int main() {
    
    
    	int n,m,t;
    	
    	scanf("%d",&n);
    	while(n)
    	{
    		t=n%10; 
    		n/=10;
    		m=m*10+t; 
    	 } 
    	 
    	 printf("%d",m); 
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    17、字符串反转 abc–>cba

    #include
    #include
    
    
    void  reverse(char* s) {
    	int len=0;
    	char *p=s;
    	while(*p!=0) {
    		len++;
    		p++; //指针右移
    	}
    	char c;
    	//交换过程 
    	for(int i=0; i<=len/2-1; i++) {
    		c=*(s+i);
    		*(s+i)=*(s+len-1-i);
    		*(s+len-1-i)=c;
    	}
    }
    
    
    int main() {
       
       char s[]="www.zh.com";
       printf("'%s'==>",s);
       reverse(s);
       printf("'%s'\n",s);
        
    
    
    
    
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    18、判断字符串中是否全部为数字,如果是则打印该数字,如果含有字母则提示错误

    #include
    #include
    
    
    
    
    
    int main() {
    
    
    	int i;
    	int n=0;
    
    	char str[100];
    	printf("请输入任意字符串: \n");
    	scanf("%s",str);
    	for(i=0; str[i]!='\0'; i++) {
              if(str[i]>='0'&&str[i]<='9') n=str[i]-'0'+n*10; 
              else {
              	printf("错误\n");
    			  return 0; 
    		  } 
    	}
    	printf("%d",n); 
    
    
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    19、123x5=1x4+1x5+2x4+2x5+3x4+3x5 用第一个数的第i位分别乘以的二个数的每个位置求和

    #include
    #include
    
    int main() {
    
    
       int a,b,sum=0;
       
       printf("请输入两个数");
       scanf("%d%d",&a,&b);
       int buf1[10]={0},buf2[10]={0},len1,len2;
       
       while(a!=0){
       	  buf1[len1++]=a%10;
       	  a/=10;
       } 
       
       while(b!=0){
       	  buf2[len2++]=b%10;
       	  b/=10;
       }
         for(int i=0;i<len1;i++)
          for(int j=0;j<len2;j++){
          	 sum+=buf1[i]*buf2[j];
    	  }
    	  
    	printf("%d",sum);
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    20、 将四位数每个位置数加5然后取余10,最后第一位和第四位交换,第二位和第三位交换

    #include
    #include
    
    void exChange(int *a,int *b) {
    
    	int temp;
    	temp=*a;
    	*a=*b;
    	*b=temp;
    }
    
    
    
    int main() {
    
    
    	int a[4],n,t;
    	printf("请输入要加密的数字: ");
    	scanf("%d",&n);
    	for(int i=3; i>=0; i--) {
    		a[i]=(n%10+5)%10;
    		n=n/10;
    	}
    
    	exChange(&a[0],&a[3]);
    	exChange(&a[1],&a[2]);
    
    	for(int i=0; i<4; i++) {
    		printf("%d",a[i]);
    	}
    
    
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    21、 将y=2n+1和z=3n+1 从n=1开始一次,将y,z插入集合,并且打印前100个元素

    #include
    #include
    
    int a[200];
    
    void insert(int k) {
    
    	int i;
    
    	for(i=0; i<200; i++) if(a[i]==k)return ; //如果集合中已经存在这个元素
    
    	for(i=199; i>=0; i--) {
    		if(a[i]==0)continue;
    		if(k<a[i]) a[i+1]=a[i];//后移
    		else {
    			a[i+1]=k;//插入
    			return ;
    		}
    	}
    
    
    }
    
    
    int main() {
    
    	int count=0,n,i,y,z;
    
    	a[0]=1;
    	for(int i=0; i<200; i++) {
    
    		n=a[i];
    		printf("%d ",a[i]);
    		count++;
    		y=2*n+1;
    		z=3*n+1;
    		insert(y);
    		insert(z);
    		if(count==100)break; 
    	}
    
    
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    22、统计子集

    1、子集有2^n个 eg3:的子集
    在这里插入图片描述

    在这里插入图片描述

    #include
    #include
    
    
    void  powerset(int  n) {
    	int m=pow(2,n);//求出一共多少组不同组合
    
    	int subsets[n];//存放子集元素
    
    	int nums;
    	for(int i=0; i<m; i++) { //这个for循环提供多少不同的位不同组合 000、001、010、011....
    		printf("{");
    		nums=0;
    		// j每次判断第j个位置是否匹配,nums统计匹配个数,并且将匹配位置记录在subset数组中 
    		for(int j=0; j<n; j++) { //这个for循环用来判断当前i可以匹配几个 eg:000一个也不会匹配输出{},001可以和001匹配在第一个位置结果为{0} 
                    if(i&(1<<j)) { //eg:  010可以匹配010 1在第二个位置结果为{1},011可以匹配011有两个位置匹配最后集合有两个元素{0,1}
    				               //eg:  111 可以和111匹配有三个元素 结果为{0,1,2} 
                    	subsets[nums++]=j; 
    				}
    		}
            //打印输出子集
    		for(int j=0;j<nums;j++){
    			printf("%d",subsets[j]);
    			if(j<nums-1)printf(","); 
    		} 
    		printf("}\n"); 
    	}
    
    }
    
    
    
    
    int main() {
    
    
        int n;
    	scanf("%d",&n);
    	powerset(n); 
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    23、十六进制转换为10进制

    #include
    #include
    #include
    
    
    //从左向右算,最左侧一共乘以16的len-1次,左2元素乘以16的len-2次以此类推 
    int  fun(char *array) {
    	
    	int i,sum=0;
    	for(int i=0;i<=strlen(array)-1;i++){
    		  if(array[i]>='0'&&array[i]<='9'){
    		  	   sum=sum*16+array[i]-'0';
    		  }else if(array[i]>='A'&&array[i]<='F') sum=sum*16+array[i]-'A'+10;
    		  else break;
    	}
       return sum;
    }
    
    
    
    int main() {
    
    	char array[10];
    	gets(array);
    	printf("%d\n",fun(array));
    
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    24、将十进制转换为2进制

    #include
    #include
    #include
    
    
    void  fun(int  n) {
    	
    	 int a[32]={0};
    	 int i=0; 
    	 while(n>0){ //最先算出来的在最右侧 
    	 	 a[i++]=n%2;
    		  n/=2; 
    	 } 
    	 for(int j=i-1;j>=0;j--){
    	 	 printf("%d",a[j]); 
    	 } 
    }
    
    
    
    int main() {
    
        int n;
    	scanf("%d",&n);
    	fun(n); 
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    25、二进制进位操作

    #include
    #include
    #include
    
    
    void  AddOne(int a[],int m) {
    	//二进制进位
    	int i,temp=1;
    
    	for(int i=m-1; i>=0; i--) {
    		if(temp==1) {
    			if(a[i]==1) { //进位 
                  a[i]=0;
                  temp=1;
    			}else{
    				a[i]=1;
    				temp=0;//进位完成 
    			} 
    		}else break; 
    	}
    }
    
    int main() {
    
    	int a[4]={1,0,1,1};
    	AddOne(a,4);
    	for(int i=0;i<4;i++){//  1 1 0 0
    		printf("%d",a[i]); 
    	} 
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    26、求两个集合的交集和并集

    #include
    #include
    #include
    #include
    
    
    //求交集
    int*  AIntersectionB(int A[],int a,int B[],int b) {
    	int c=a>b?b:a;
    
    	int *C;
    	C=(int*)malloc(sizeof(int)*c);
    	for(int i=0; i<c; i++) {
    		if(A[i]==B[i]&&B[i]==1)C[i]=1;
    		else C[i]=0;
    	}
    	return C;
    }
    
    int* AUnionB(int A[],int a,int B[],int b) {
    	int c=a>b?a:b; // a与b中较大者
    	int d=a>b?b:a; // a与b中较小者
    	int *C;
    	C=(int*)malloc(sizeof(int)*c);
    	for(int i=0; i<d; i++) {
    		if(A[i]==1||B[i]==1)C[i]=1;
    		else C[i]=0;
    	}
    	if(a>b) {
    		for(int i=d; i<a; i++) C[i]=A[i];
    	} else if(a<b) {
    		for(int i=d; i<b; i++)  C[i]=B[i];
    	}
    	return C;
    }
    
    
    int main() {
    
    	int a[4]= {1,0,1,1};
    	int b[4]= {0,1,1,1};
    	int *c=AIntersectionB(a,4,b,4);
    	for(int i=0; i<4; i++) { //  1 1 0 0
    		printf("%d",c[i]);
    	}
    	printf("\n");
    	int *d=AUnionB(a,4,b,4);
    	for(int i=0; i<4; i++) { //  1 1 0 0
    		printf("%d",d[i]);
    	}
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    27、给出一个集合求出个数为M的所有子集

    eg S{1,2,3,4} M=2
    ans= {1,2} {1,3} {1,4} {2,3} {2,4} {3,4}
    代码类比22题

    #include
    #include
    #include
    #include
    
    
    void  subSet(int S[],int N,int M) {
    
    	int i,j,k;
    	int a[N];
    
    	for(int i=0; i<pow(2,N); i++ ) {
    
    		k=0;
    		for(int j=0; j<N; j++) {
    			if(i&(1<<j)) {
    				a[k++]=S[j];
    			}
    		}
    		if(k==M) {
    			printf("{");
    			for(int j=0; j<k-1; j++) {
    				printf("%d,",a[j]);
    			}
    			printf("%d}",a[k-1]);
    		}
    
    	}
    
    }
    
    
    
    int main() {
    
    	int S[5]= {1,2,3,4};
    	int N=4,M=2;
    	subSet(S,N,M);
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    28、任意进制转换

    #include
    #include
    #include
    #include
    
    
    
    
    int main() {
    
    	int a,b;
    	char str[40];
    	while(scanf("%d%s%d",&a,&str,&b)!=EOF) {
    		int tmp=0,lenth=strlen(str),c=1;
    
    		for(int i=lenth-1; i>=0; i--) {
    			int x;
    			if(str[i]>='0'&&str[i]<='9') {
    				x=str[i]-'0';
    			} else if(str[i]>='a'&&str[i]<='z') {
    				x=str[i]-'a'+10;
    			} else {
    				x=str[i]-'a'+10;
    			}
    			tmp+=x*c;
    			c*=a;
    		}
    
    		char ans[40],size=0;
    		while(tmp) {
    			int x=tmp%b;
    			ans[size++]=(x<10)?x+'0':x-10+'A';//转换为字符
    			tmp/=b;
    		}
    		for(int i=size-1; i>=0; i--) {
    			printf("%c",ans[i]);
    		}
    		printf("\n");
    	}
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    29、将数组左侧全部变为奇数、右侧全部变为偶数

    类似于partion过程

    #include
    #include
    #include
    #include
    
    
    void func(int a[],int n){
    	
    	int i=0,j=n-1,t;
    	while(i<j){
    		 while(a[i]%2!=0){
    		 	i++; 
    		 }
    		 while(a[j]%2==0){
    		 	 j--; 
    		 } 
    		 if(i<j){
    		 	t=a[i];
    			 a[i]=a[j];
    			 a[j]=t; 
    		 } 
    	} 
    	
    	for(int i=0;i<n;i++){
    		printf("%d ",a[i]); 
    	} 
    	
    } 
    
    int main() {
    
        int a[5]={1,2,3,4,5};
        func(a,5); 
        
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    30、将二维数组元素对换,第一个换最后一个,依次类推

    注意二维数组,传参方式
    注意行为奇数情况

    #include
    #include
    #include
    #include
    
    
    void  func(int (*a)[4],int m,int n) {
    	int i,j,t;
    	for( i=0; i<(m/2); i++) {
    		for( j=0; j<n; j++) {
    			t=a[i][j];
    			a[i][j]=a[m-i-1][n-j-1];
    			a[m-i-1][n-j-1]=t;
    		}
    	}
    	if(m%2!=0) { //奇数行自己对换
    		for(int j=0; j<(n/2); j++) {
    			t=a[i][j];
    			a[i][j]=a[i][n-j-1];
    			a[i][n-j-1]=t;
    		}
    	}
    	for(i=0; i<m; i++) {
    
    		for(j=0; j<n; j++) {
             printf("%d ",a[i][j]); 
    		}
    		printf("\n"); 
    	}
    }
    
    int main() {
    
    	int a[3][4]={{0,1,2,3},{10,11,12,13},{20,21,22,23}};
    	func(a,3,4); 
    
    
    
    
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    31、将两个有序数组合并为一个有序数组,类似归并排序里的merge操作

    #include
    #include
    #include
    #include
    
    
    void  func(int a[],int b[],int c[],int m,int n) {
    
    	int i=0,j=0,k=0;
    
    	while(i<m&&j<n) {
    		if(a[i]<b[j]) {
    			c[k]=a[i++];
    		} else {
    			c[k]=b[j++];
    		}
    		k++;
    	}
    	while(i<m) {
    		c[k++]=a[i++];
    	}
    	while(j<n) {
    		c[k++]=b[j++];
    	}
    }
    
    int main() {
    
         int a[5]={1,3,5,7,9};
    	 int b[6]={2,4,6,8,10,11};
    	 int c[100]; 
    	 func(a,b,c,5,6);
    	 for(int i=0;i<11;i++){
    	 	printf("%d ",c[i]); 
    	 } 
    
    
    
    
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    32、将数组偶数放在前面并且有序,奇数放在后面并且有序

    先按奇数偶数排列整齐,在对偶数部分和奇数部分分别排序

    #include
    #include
    #include
    #include
    
    
    
    int  Partition(int a[],int low,int high) {
    	int pivot=a[low];//选取第一个元素为基准
    	while(low<high) {
    		while(low<high&&a[high]>=pivot)high--;
    		a[low]=a[high];
    		while(low<high&&a[low]<=pivot)low++;
    		a[high]=a[low];
    	}
    	a[low]=pivot;
    	return low;
    }
    void  QuickSort(int a[],int low,int high) {
    
    	if(low<high) {
    		int pivot=Partition(a,low,high);
    		QuickSort(a,low,pivot-1);
    		QuickSort(a,pivot+1,high);
    	}
    }
    
    
    
    int main() {
    
    	//先交换,在排序
    	int a[7]= {1,4,3,2,5,9,7};
    	int n=7;
    	int i=0,j=n-1,t,p=0,q=0;
    	while(i<j) {
    		while(a[i]%2==0) {
    			i++;
    			p++;
    		}
    		while(a[j]%2!=0) {
    			j--;
    			q++;
    		}
    		if(i<j) {
    			t=a[i];
    			a[i]=a[j];
    			a[j]=t;
    		}
    	}
    	QuickSort(a,0,p-1);
    	QuickSort(a,p,p+q-1);
    
    	for(int i=0; i<n; i++) {
    		printf("%d",a[i]);
    	}
    
    
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    33、将数组a1、、、、am b1、、、、bn 翻转为b1、、、bn a1、、、、am

    经典的reverse操作

    #include
    #include
    #include
    #include
    
    
    void  reverse(int a[],int n) {
    	int i,t;
    	for(int i=0; i<(n/2); i++) {
            t=a[i];
    		a[i]=a[n-i-1];
    		a[n-i-1]=t; 
    	}
    }
    
    
    
    
    int main() {
         
         int a[11]={1,2,3,4,5,6,7,8,9,10,11}; 
         int m=5,n=6;//前半部份5个元素,后半部分6个元素 
    	 reverse(a,m+n); 
    	 reverse(a,n);
    	 reverse(a+n,m);
    	 
    	 for(int i=0;i<11;i++)printf("%d ",a[i]); // 6 7 8 9 10 11 1 2 3 4 5
    
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    34、对n个字符串按照ASCII进行匹配

    n个字符串用二维数组存储
    利用strcmp函数进行比较,利用strcpy进行交换

    #include
    #include
    #include
    #include
    
    
    
    void   sort(char st[][10],int n) {
    
    	int i,j;
    	char t[10];
    	for(int i=0; i<n-1; i++) {
    		for(int j=i+1; j<n; j++) { //升序排列
    			if(strcmp(st[i],st[j])>0) { //前大于后
                    strcpy(t,st[i]);
    				strcpy(st[i],st[j]);
    				strcpy(st[j],t); 
    			}
    		}
    	}
    }
    
    
    
    int main() {
    
    	char st[80][10];
    	int i,j,n;
    	printf("请输入要输入字符串的个数\n");
    	scanf("%d",&n);
    	printf("请输入%d个字符串:\n",n);
    	for(int i=0; i<n; i++)scanf("%s",st[i]);
        sort(st,n);
    	printf("排序后的结果为:\n");
    	for(int i=0;i<n;i++)printf("%s\n",st[i]); 
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    35、将数组中所有0元素移动到后面,非零元素按相对位置不变

    
    #include
    #include
    #include
    #include
    
    
    
    int   sort(int a[],int n) {
    
    	int k=0;
    	for(int i=0; i<n; i++) {
    		if(a[i]==0)k++;
    		else a[i-k]=a[i];
    	}
    
    	return n-k;
    
    }
    
    
    
    int main() {
    
    	int a[]= {7,0,0,3,0,5,0};
    	int n=sort(a,7);
    	for(int i=0; i<n; i++)printf("%d",a[i]);
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    36、数组中删除元素值在[x,y]之间所有元素

    利用一个指针k记录待删除元素个数

    #include
    #include
    #include
    #include
    
    int del(int a[],int n,int x,int y){
    	 int i=0,k=0;
    	 
    	 for(int i=0;i<n;i++){
    	 	if(a[i]>=x&&a[i]<=y){
    	 		k++;
    		 }else{
    		 	a[i-k]=a[i];
    		 }
    	 }
    	 return n-k;
    }
    
    
    
    
    
    int main() {
    
    	int a[]= {7,1,2,3,9,5,4};
    	
    	int k=del(a,7,2,5);
    	for(int i=0;i<k;i++)printf("%d",a[i]); 
    	
    	
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    37、删除数组中相同元素,保留一个

    #include
    #include
    #include
    #include
    
    void del(int a[],int n) {
    	int k=0,sum=0;//sum用来记录相同个数
    
    	for(int i=0; i<n; i++) {
    
    		for(int j=i+1; j<n; j++) {
    			if(a[i]==a[j]) {
    				k++;
    				sum++;
    			} else a[j-k]=a[j];
    		}
    	}
    
    }
    
    
    
    int main() {
    
    	int a[]= {7,1,2,2,2,5,5};
    	int n=7;
    	del(a,7);
    	for(int i=0; i<n; i++)printf("%d",a[i]);
    
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    38、每行最小值乘以每列最大值,求和

    #include
    #include
    #include
    #include
    
    #define N 5
    
    int  vector(int a[N][N],int n) {
    
    	int max,min,sum,A[N],B[N];
    	for(int i=0; i<n; i++) {
    		max=a[i][0];
    		for(int j=1; j<n; j++) {
    			if(a[i][j]>max)max=a[i][j];
    		}
    		A[i]=max;
    	}
    	
    	for(int i=0;i<n;i++){
    		min=a[0][i]; 
    		for(int j=1;j<n;j++){
    			if(a[j][i]<min)min=a[j][i]; 
    		} 
    		B[i]=min;
    	}
    	
    	for(int i=0;i<n;i++){
    	  //	printf("%d  %d  ",A[i],B[i]);
    		sum+=A[i]*B[i];
    	} 
       return sum;
    }
    
    
    
    int main() {
    
        int a[5][5]={{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5}};
        printf("%d",vector(a,5)); // 75
    
    
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    39、判断数组中是否存在a[i]等于i之前所有元素和

    #include
    #include
    #include
    #include
    
    
    int  exist(int a[],int n){
    	int sum=0;
    	
    	for(int i=0;i<n;i++){
    		if(a[i]==sum){
    			return 1;
    		}
    		sum+=a[i];
    	}
    	return 0;
    }
    
    
    
    
    int main() {
    
       int a[5]={1,2,0,1,0};
       printf("是否存在: %d",exist(a,5));
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    40、删除数组中所有质数

    #include
    #include
    #include
    #include
    
    
    int  prime(int n) {
    
    	if(n==1||n==2)return 1;
    	for(int i=2; i*i<=n; i++) {
    		if(n%i==0)return 0;
    	}
    
    	return 1;
    }
    
    int  delarr(int a[],int n) {
    
    	int k;
    	for(int i=0; i<n; i++) {
    		if(prime(a[i]))k++;
    		else a[i-k]=a[i];
    	}
    	return n-k;
    }
    
    
    
    int main() {
    
    	int a[5]= {12,2,6,4,11};
    	int n=delarr(a,5);
    	for(int i=0; i<n; i++) {
    		printf("%d ",a[i]); //12 6 4
    	}
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    41、给字符串数组从小到大排序

    #include
    #include
    #include
    #include
    
    
    
    
    int partition(char str[],int low,int high) {
    	char pivot=str[low];
    	while(low<high) {
    		//注意先high--不然有可能首次high对应位置满足关系,直接替换导致损失一个元素 
    		while(low<high&&str[high]>=pivot)high--;
    		str[low]=str[high];
    		while(low<high&&str[low]<=pivot)low++;
    		str[high]=str[low];
    
    	}
    	printf("%d\n",low);
    	str[low]=pivot;
    	printf("%c\n",str[low]);
    	return low;
    }
    
    void  quickSort(char str[],int low,int high) {
    	if(low<high) {
    		int pos=partition(str,low,high);
    		quickSort(str,low,pos-1);
    		quickSort(str,pos+1,high);
    	}
    }
    
    
    int main() {
    
    
    	char str[5]= {'A','Z','B','D','C'};
    	quickSort(str,0,4);
    	for(int i=0; i<5; i++) {
    		printf("%c",str[i]);
    	}
    
    
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    42、将数组b插入到数组a中,不允许开新的数组,相同元素保留一个,a与b保证递增有序

    先找到待插入位置,将数组元素后移动一位,插入元素,并且要记录好数组长度++

    #include
    #include
    #include
    #include
    
    
    
    int  merger(int a[],int m,int b[],int n) {
    	int len=m, j=0;
    	for(int i=0; i<n; i++) { //将b数组每个元素插入a数组中
    		while(j<len&&a[j]<b[i])j++; //找到在a中待插入位置  (这里为len不是n)
    		if(a[j]==b[i]) continue;
    		for(int k=len; k>j; k--)a[k]=a[k-1];//a中元素后移一位
    		a[j]=b[i];//插入
    		len++;
    	}
    	return len;
    }
    
    
    
    
    
    int main() {
    
    	int a[15]= {1,3,5,7,9};
    	int b[15]= {2,4,6,8,10};
    
    	int n=merger(a,5,b,5);
    
    	for(int i=0; i<n; i++) {
    		printf("%d ",a[i]);
    	}
    
    
    
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    43、使用数组精确计算M/N,的小数各个位的值,如果小数部分无限循环,则保留第一循环节同时输出循环节起止位置

    先求出整数部分,用m记录余数
    利用两个数组a,b分别存放小数和余数,b用来判断是否存在循环节,a存放结果小数

    #include
    #include
    #include
    #include
    
    
    
    
    int main() {
    
    	int a[100],b[100];//a用于存放小数部分,b存放余数
    
    	int m,n,i=0,s;//i记录小数个数,s存放正数部分 
    	printf("请输入分子和分母");
    	scanf("%d%d",&m,&n);
    	if(m>n){
    		s=m/n;
    		m%=n;
    		printf("整数部分为%d\n",s); 
    	} 
    	
    	while(m!=0){
    		 m*=10;//为求出小数做准备 
    		 a[i]=m/n; //求出小数
    		 m%=n;
    		 b[i]=m;//做记录余数,用来判断循环节
    		 for(int j=0;j<i;j++){
    		 	if(b[j]==m){
    		 		printf("从小数点%d 位开始循环,到%d 位结束.\n",j+1,i);
    				 m=0;
    				 break; 
    			 } 
    		 }
    		 i++; 
    	} 
        printf("%d.",s);
    	for(int j=0;j<i-1;j++)printf("%d",a[j]);
    
    
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    43、删除数组中为key的所有元素

    #include
    #include
    #include
    #include
    
    
    int  del(int a[],int n,int key){
    	 
    	 int k=0;
    	 for(int i=0;i<n;i++){
    	 	if(a[i]==key){
    	 		k++;
    		 }else{
    		 	a[i-k]=a[i];
    		 }
    	 }
    	 return n-k;
    }
    
    
    
    
    
    int main() {
    	
    	int a[5]={1,2,2,3,4};
    	
    	int n=del(a,5,2);
    	for(int i=0;i<n;i++){
    		printf("%d ",a[i]);
    	}
    
       
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    44、求出3x3矩阵对角线元素和

    #include
    #include
    #include
    #include
    
    
    
    int main() {
    	
    	int a[3][3]={{1,2,3},{1,2,3},{1,2,3}};
    	
        int sum=0;
        for(int i=0;i<3;i++){
        	sum+=a[i][i];
    	}
    	printf("%d",sum);
    
       
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    45、利用指针对二维数组进行排序

    #include
    #include
    #include
    #include
    
    
    
    
    
    
    
    int main() {
    
    
    	int i,j,t;
    
    	int a[3][3]= {{1,5,4},{2,8,7},{6,3,10}};
    	int *p=&a[0][0];//将二维数组起始地址赋值给p
    	int n=3*3; //求出元素总个数
    	//冒泡排序
    	for(int i=0; i<n; i++) {
    		for(int j=0; j<n-i-1; j++) {
    			if(*(p+j)>*(p+j+1)) {
    				t=*(p+j);
    				*(p+j)=*(p+j+1);
    				*(p+j+1)=t;
    			}
    		}
    	}
    
    	for(int i=0; i<3; i++) {
    		for(int j=0; j<3; j++) {
    			printf("%d ",a[i][j]);
    		}
    		printf("\n");
    	}
    
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    46、螺旋矩阵问题

    #include
    #include
    #include
    #include
    
    #define N 10
    
    
    
    int main() {
    
    
    	int a[10][10];
    	int k,n,i,j,num=1;
    
    	for(n=0; n<=N/2; n++) { //最多进行N/2+1轮 
    
    		for(j=n; j<N-n-1; j++)a[n][j]=num++;//右 
    
    		for(i=n; i<N-n-1; i++)a[i][N-n-1]=num++;//下 
    
    		for(j=N-n-1; j>n; j--)a[N-n-1][j]=num++;//左 
    
    		for(i=N-n-1; i>n; i--)a[i][n]=num++;//上 
    
    
    
    	}
    
    	for(int i=0; i<N; i++) {
    		for(int j=0; j<N; j++) {
    			printf("%d ",a[i][j]);
    		}
    		printf("\n");
    	}
    
    
    	return 0;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    >47、统计数组中最长连续相等序列
    
    ```c
    #include
    #include
    #include
    #include
    
    #define N 10
    
    int  fun(int a[],int n){
    	int length=1,cnt=1;
    	int p=a[0];
    	
    	for(int i=1;i
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    48、统计输入数据字母、数字、空格、其他字符分别个数多少

    #include
    #include
    #include
    #include
    
    #define N 10
    
    
    
    
    int main() {
    
    
    	char c;
    	int letters=0,spaces=0,digits=0,others=0;
    	while((c=getchar())!='\n') {
    		if(c>='a'&&c<='z'||c>='A'&&c<='Z')letters++;
    		else if(c>='0'&&c<='9')digits++;
    		else if(c==' ')spaces++;
    		else others++;
    	}
    	printf("%d  %d   %d  %d",letters,spaces,digits,others);
    
    
    
    
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    49、将数组中所有整数重复出现两次

    -2 -1 -1 0 0 —>-2 -2 -1 -1 -1 -1 0 0 0 0

    #include
    #include
    #include
    #include
    
    #define N 10
    
    void f(int a[],int n){
    	int i,j;
    	for(i=0;i<2*n;i+=2){
    		for(j=n+i;j>i;j--){
    			a[j]=a[j-1]; 
    		}
    	}
    }
    
    
    int main() {
    
    
    	int a[9]={-2,-1,-1,0,0,1,4,4,4};
    	f(a,9);
    	for(int i=0;i<9*2;i++){
    		printf("%d ",a[i]); 
    	} 
    
    
    
    
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    50、统计二维数组中出现频率最高次数的数字

    #include
    #include
    #include
    #include
    
    #define N 5
    
    int fun(int a[N][N]) {
    	int i,j,b[11]= {0};
    	for(i=0; i<N; i++) {
    		for(j=0; j<N; j++) {
    			b[a[i][j]]++;
    		}
    	}
    	int maxf=b[1],ans;
    	for(i=1; i<11; i++) {
    		if(b[i]>maxf) {
    			maxf=b[i];
    			ans=i;
    		}
    	}
    	return ans; 
    }
    
    int main() {
    
    
        int a[N][N]={{3,2,4,5,1},{10,10,10,10,10},{8,7,6,7,7},{3,3,4,1,2},{4,5,3,1,1}}; 
    	int ans=fun(a);
    	printf("%d",ans); 
    
    
    
    
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    51、统计数组中不同元素出现个数

    这道题注意,不能开vis数组用下标直接统计,有空能元素值很大,应该避免这种做法

    #include
    #include
    #include
    #include
    
    #define N 5
    
    
    int fun(int a[],int n,int b[],int cnt[]) {
    
    	int i,j,k=0;//k记录当前不同数字个数
    
    	for( i=0; i<n; i++) {
    		for( j=0; j<k; j++) {
    			if(b[j]==a[i]) {//找到相同元素
    				cnt[j]++;
    				break;
    			}
    		}
    		if(j==k) {//出现新的元素
    			b[k]=a[i];
    			cnt[k]=1;
    			k++;
    		}
    	}
    	return k;
    }
    
    
    int main() {
    
    
    	int a[10]= {3,5,3,3,10,7,7,5,3,7};
    	int b[10],cnt[10];
    	int k=fun(a,10,b,cnt);
    
        for(int i=0;i<k;i++){
        	printf("%d 出现次数 %d\n",b[i],cnt[i]); 
    	} 
    
    
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    52、设有两个字符数组a,s统计a中每个元素在s中出现次数
    8

    #include
    #include
    #include
    #include
    
    #define N 5
    
    
    int*  fun(char a[],char s[],int n){
    	 int len=strlen(s);
    	 int *c=(int*)malloc(sizeof(int)*n); 
    	 for(int i=0;i<len;i++){
    	 	c[i]=0; 
    	 } 
    	 for(int i=0;i<n;i++){
    	 	for(int j=0;j<len;j++){
    	 		if(a[i]==s[j])c[i]++; 
    		 } 
    	 } 
    	 
    	 return c; 
    } 
    
    
    
    int main() {
     
        char a[5]={'a','b','a','c','d'};
    	char s[5]={'a','a','c','d','e'};
    	int*  c=fun(a,s,5);
    	
    	for(int i=0;i<5;i++){
    		printf("%d ",c[i]); 
    	} 
     
    	
    
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    53、统计小写字母出现次数

    #include
    #include
    #include
    #include
    
    #define N 5
    
    
    
    
    
    int main() {
    
    
    	int cnt[26]= {0};
    
    	int n;
    	printf("请输入字符串长度\n");
    	scanf("%d",&n);
    	char c;
    	for(int i=0; i<n; i++) {
    		scanf("%c",&c);
    		int index=c-'a';
    		cnt[index]++;
    	}
    
    	for(int i=0; i<26; i++) {
    		if(cnt[i]) {
    			printf("%c出现次数为:%d\n",'a'+i,cnt[i]);
    		}
    	}
    
    
    
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    MIPI CSI-2笔记(12) -- Low Level Protocol(数据加扰,扰码,Data Scrambling)
    u盘初始化后怎么恢复文件?这几步操作帮你找回
    超精准!AI 结合邮件内容与附件的意图理解与分类!
    『力扣刷题本』:移除链表元素
    CSS实现文本左右对齐
    JavaWeb AJAX请求
    ssm+vue的疫情高校师生外出请假管理系统(有报告)。Javaee项目,ssm vue前后端分离项目。
    【OpenFeign常用配置】
    拿走吧,你,可视化大屏一次性解决
    项目难管理?先学会用好甘特图(内附操作方法及实用模板)
  • 原文地址:https://blog.csdn.net/weixin_46503238/article/details/126374495