C语言程序设计之数组1

问题1_1

        函数 f u n fun fun 的功能是:移动一位数组中的内容,若数组中有 n n n 个整数,要求把下标从 0 0 0 ~ p p p (含 p p p p p p 小于等于 n − 1 n-1 n1 的数组元素平移到数组的最后 )。
        例如,一维数组的原始内容为: 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 1,2,3,4,5,6,7,8,9,10 p p p 的值为 3 3 3 。移动后,一维数组中的内容应为 : 4 , 5 , 6 , 7 , 8 , 9 , 10 , 1 , 2 , 3 , 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4,5,6,7,8,9,101,2,3,

代码1_1

#include<stdio.h>

#define N 80 

void fun(int *w, int p, int n){
	int x, j, ch;
	for(x=0; x<=p; x++){
		ch = w[0];
		for(j=1; j<n; j++){
			w[j-1] = w[j];
		}
		w[n-1] = ch;
	}
}

void main(void){
	int a[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
	int i, p, n=15;
	printf("The original data:\n");
	for(i=0; i<n; i++){
		printf("%3d", a[i]);
	}
	printf("\n\nEnter p:");
	scanf("%d", &p);
	fun(a, p, n);
	printf("\nThe data after moving:\n");
	for(i=0; i<n; i++)
		printf("%3d", a[i]);
	printf("\n\n");
}

结果1_1

Resulit_1_1

问题1_2

        函数 f u n fun fun的功能是:删除一维数组中所有相同的数,使之只剩一个。数组中的数按由从小到大的顺序排列,函数返回删除后数组中数据的个数。
        例如,若一维数组中的数据是: 2   2   2   3   4   5   6   6   6   6   7   7 2\ 2\ 2\ 3\ 4\ 5\ 6\ 6\ 6\ 6\ 7\ 7 2 2 2 3 4 5 6 6 6 6 7 7
        删除后,数组中的内容应该是: 2   3   4   5   6   7 2\ 3\ 4\ 5\ 6\ 7 2 3 4 5 6 7

代码1_2

#include<stdio.h>

#define N 80

int fun(int a[], int n){
	int i, j=1;
	for(i=1; i<n; i++){
		if(a[j-1]!=a[i])
			a[j++] = a[i];
	}
	return j;
}

void main(void){
	int a[N] = {2, 2, 2, 3, 4, 5, 6, 6, 6, 6, 7, 7}, i, n=20;
	printf("The original data:\n");
	for(i=0; i<n; i++)
		printf("%3d", a[i]);
	n = fun(a, n);
	printf("\n\ nThe data after deleted:\n");
	for(i=0; i<n; i++)
		printf("%3d", a[i]);
	printf("\n\n");
}

结果1_2

Result_1_2

问题1_3

         用筛选法可得到 2 2 2~ n n n( n < 10000 n<10000 n<10000)的所有素数,方法是:首先从素数 2 2 2 开始,将所有 2 2 2 的倍数的数从数表中删去(把数表中相应位置的值置成 0 0 0);接着从数表中找下一个非 0 0 0 数,并从数表中删去该数的所有倍数;依次类推,直到所找的下一个数等于 n n n 为止。这样会得到一个序列 : 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , . . . :2,3,5,7,11,13,17,19,23,... :2,3,5,7,11,13,17,19,23,...

代码1_3

#include<stdio.h>

int fun(int n){
	int a[10000], i, j, count=0;
	for(i=2; i<=n; i++)
		a[i] = i;
	i = 2;
	while(i<n){
		for(j=a[i]*2; j<=n; j+=a[i])
			a[j] = 0;
		i++;
		while(a[i]==0)
			i++;
	}
	printf("\nThe prime number between 2 to %d\n", n);
	for(i=2; i<=n; i++)
		if(a[i]!= 0){
			count++;
			printf(count%15?"%5d":"%5d", a[i]);
		}
	return count;
} 

void main(void){
	int n=20, r;
	r = fun(n);
	printf("\nThe number  of prime is : %d\n", r);
}

结果1_3

Result_1_3

问题1_4

         为一个偶数寻找两个素数,这两个素数之和等于该偶数,并将这两个素数通过形参指针 传回主函数。

代码1_4

#include<stdio.h>
#include<math.h>

void fun(int a, int *b, int *c){
	int i, j, d, y;
	for(i=3; i<=a/2; i=i+2){
		y = 1;
		for(j=2; j<=sqrt((double)i); j++){
			if(i%j==0)
				y = 0;
		}
		if(y==1){
			d = a-i;
			for(j=2; j<=sqrt((double)d); j++){
				if(d%j==0)
					y = 0;	
			}
			if(y==1){
				*b = i;
				*c = d;
			}
		}
	}
}

void main(void){
	int a, b, c;
	do{
		printf("\nInput a: ");
		scanf("%d", &a);
	}while(a%2);
	fun(a, &b, &c);
	printf("\n\n %d = %d + %d\n", a, b, c);
}

结果1_4

Result_1_4

问题1_5

         将 形参 s s s 所指字符串中的所有数字字符顺序前移,其它字符顺序后移,处理后新字符串的首地址作为函数值返回。

代码1_5

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>

char *fun(char *s){
	int i, j, k, n;
	char *p, *t;
	n = strlen(s)+1;
	t = (char*)malloc(n*sizeof(char));
	p = (char*)malloc(n*sizeof(char));
	j = 0;
	k = 0;
	for(i=0; i<n; i++){
		if(isdigit(s[i])){
			p[j] = s[i];
			j++;
		}
		else{
			t[k] = s[i];
			k++;
		}
	}
	for(i=0; i<k; i++)
		p[j+i] = t[i];
	p[j+k] = 0;
	return p;
} 

void main(void){
	char s[80];
	printf("Please input : ");
	scanf("%s", s);
	printf("\nThe result is  : %s\n", fun(s));
}

结果1_5

Result_1_5

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值