吉林大学967-高级语言程序设计-2015

2015-1
//编写一个程序,对输入的任意字符串,
//统计并输出其中每个英文小写字母在该
//字符串中出现的次数
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 100
int main() {
	char ch[N];
	char str[N][2] = {'\0'};
	gets_s(ch);
	int i, j, mark;
	char key;
	for (i = 0; ch[i] != '\0'; i++) {
		key = ch[i];
		mark = 0;
		j = 0;
		if (key >= 'a' && key <= 'z') {
			while (str[j][0] != '\0') {
				if (str[j][0]==key) {
					str[j][1]=str[j][1]+1;
					mark = 1;
					break;
				}
				j++;
			}
			if (mark == 0) {
				str[i][0] = key;
				str[i][1] = 1;
			}
		}
	}
	for (i = 0; (key = str[i][0]) != '\0'; i++) {
		printf("%c  %d\n",key,str[i][1]);
	}
	system("pause");
	return 0;
}


2015-2
#include<stdio.h>
#include <stdbool.h>
#include<stdlib.h>
#define N 100
int main() {
	bool jude(char str[]);
	char str[N];
	gets_s(str);
	if (jude(str))
		puts(str);
	else
		printf("错误\n");
	system("pause");
	return 0;
}
bool jude(char str[]) {
	int i;
	char s;
	for (i = 0; (s = str[i]) != '\0'; i++) {
		if (s>'9' || s<'0')
			return false;
	}
	return true;
}



2015-3
//螺旋矩阵
#include<stdio.h>
#include<stdlib.h>
#define M 10
int main() {
	int A[M][M];
	int i, j, T, n = 1;
	T = M;
	while (T>0) {
		//保持行不变,列变。向右顺序加一
		for (j = M - T; j<T; j++) {
			A[M - T][j] = n;
			n++;
		}
		//保持列不变,行变。向下顺序加一
		for (i = M - T + 1; i<T; i++) {
			A[i][T - 1] = n;
			n++;
		}
		//保持行不变,列变。向左顺序加一
		for (j = T - 2; j >= M - T; j--) {
			A[T - 1][j] = n;
			n++;
		}
		//保持列不变,行变。向上顺序加一
		for (i = T - 2; i>M - T; i--) {
			A[i][M - T] = n;
			n++;
		}
		//向内缩一圈
		T--;
	}
	for (i = 0; i<M; i++) {
		for (j = 0; j<M; j++) {
			printf("%d\t", A[i][j]);
		}
		printf("\n");
	}
	system("pause");
	return 0;
}


2015-4
//设有一个包含N个整数的集合S,编写函数求S的所有元素个数为
//M(0<=M<=N)的子集。
//例如:N=4,S=(1,2,3,4),若M=2,则输出结果为:
//(1,2)、(1,3)、(1,4)、(2,3)、(2,4)、(3,4)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define N 6 //集合元素的总个数
#define M 2 //子集元素的个数
int main() {
	//定义数组
	int A[N] = {1,2,3,4,5,6};
	//定义二进制数组
	int Bin[N] = { 0 };
	int i,n,j,k;
	//N位的二进制数字,共有2^N个,循环2^N遍,每次 Bin[0]+1 
	for (i = 0; i < pow(2, N); i++) {
		n = 0;
		Bin[0]++;
		//Bin加一后,整理二进制数组
		for (j = 0; j < N; j++) {
			Bin[j + 1] += Bin[j] / 2;
			Bin[j] = Bin[j] % 2;
		}
		//遍历二进制数组,统计二进制数组中值为1的个数
		for (j = 0; j < N; j++) {
			if (Bin[j] == 1)
				n++;
		}
		//如果n=M,则达到规定的子集数据元素个数,此时输出
		if (n == M) {
			k = 0;//标记位置
			printf("{");
			for (j = 0; j < N; j++) {
				if (Bin[j] == 1) {
					if (k == 0) {
						printf("%d", A[j]);
						k = 1;
					}
					else {
						printf(",%d", A[j]);
					}
				}
			}
			printf("}	");
		}
	}
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36109528/article/details/86231626