c语言数组练习题

1)定义大小为100的整型数组, 使用随机函数给数组元素赋值。数值范围1..100, 并且排序,使用冒泡排随机函数

2)定义大小为100的整型数组,使用随机函数给数组元素赋值。数值的范围是1.. 100,并且不容许重复

(两题类似都用同一种方法,只不过第二题,进行了数字筛选)

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ARSIZE 100
//int FindValue(int ar[], int n, int val)
//{
//	int i = 0;
//	for (i = 0; i <= n - 1; i++)
//	{
//		if (val == ar[i])
//		{
//			return i;
//		}
//		if (val != ar[i])
//		{
//			return -1;
//		}
//	}
//}
int main()
{
	int ar[ARSIZE] = { 0 };
	int br[ARSIZE + 1] = { 0 };
	int i = 0;
	srand(unsigned int(time(NULL)));//随机种子
	while (i < ARSIZE)
	{
		int ret = rand() % 100 + 1;//伪随机数
		if (br[ret] == 0)
		{
			ar[i++] = ret;//有两个数组,都初始化为0且大小都为100,把br下标填进ar中,查表法
			br[ret] = 1;
		}
	}
	for (int i = 1; i <ARSIZE; i++)//冒泡排序法:比较的趟数
	{
		int flag = 1;//设立一个标签,如果是有序的就不进行冒泡排序
		for (int n = 0; n < ARSIZE-i; n++)//每一趟比较的次数
		{
			if (ar[n] > ar[n + 1])
			{
				int tmp = 0;
				tmp = ar[n + 1];
				ar[n + 1] = ar[n];
				ar[n] = tmp;
				flag = 0;
			}
		}	
		if (flag == 1)
			{
				break;
			}
	}
	for (i = 0; i <ARSIZE; i++)//打印以及制表
	{
        printf("%3d", ar[i]);
		if ((i+1) % 10 == 0)//每十个一行
		{
			printf("\n");
		}
	}
	return 0;
}

3)统计字符串中每个英文字符出现的次数, 不区分大小写, 只统计英文字符。

#include<stdio.h>
#include<ctype.h>
#define ARSIZE 26
int main()
{
	int arr[ARSIZE] = { 0 };
	char ch;
	while ((ch = getchar()) != EOF)
	{
		if (isalpha(ch))//如果得到的字符是英文字符就进入if语句
		{
			arr[tolower(ch) - 'a'] += 1;//由于是不区分大小写,所以要用tolower将大写英文字符转换成小写英文字符
		}
	}
	for (int i = 0; i < ARSIZE; i++)
	{
		printf("%c=>%d\n", i+'a', arr[i]);
	}
	printf("\n");
	return 0;
}

4)统计字符串中每个英文字符出现的次数, 区分大小写,只统计英文字符。

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define ARSIZE 26
int main()
{
	int arr[ARSIZE] = { 0 };
	int brr[ARSIZE] = { 0 };
	char ch;
	while ((ch = getchar()) != EOF)
	{
		if (isalpha(ch))
		{
			if (islower(ch))
			{
				arr[ch - 'a'] += 1;
			}
			if (isupper(ch))
			{
				brr[ch - 'A'] += 1;
			}
		}
	}
	for (int i = 0; i < ARSIZE; i++)
	{
		printf("%c=>%d\n", i + 'a', arr[i]);
		
	}
	for (int i = 0; i < ARSIZE; i++)
	{
		printf("%c=>%d\n", i + 'A', brr[i]);
	}
	printf("\n");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值