筛选100内素数,杨辉三角形,折半查找,翻译密码

筛选100内素数,杨辉三角形,折半查找,翻译密码 

 

#include<stdio.h>
#include<assert.h>
void Get_prime()  //筛选一百以内素数
{
	for (int i = 2; i < 100; i++)
	{
		for (int j = 2; j <= i; j++)
		{
			if (i % j == 0 && i != 2)
			{
				break;
			}
			if (j >= i - 1)
			{

				printf("%d ", i);
			}
		}		
	}
	printf("\n");
}
void Triangle()  //输出杨辉三角形的前十行
{
	int arr[12] = { 1 };
	int brr[12] = { 1 };
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j <= i; j++)
		{
			if (j - 1 >= 0)
			{
				arr[j] = brr[j - 1] + brr[j];	
			}
		}
		for (int k = 0; k <= i; k++)
		{
			brr[k] = arr[k];
			printf("%4d", arr[k]);
		}
		printf("\n");
	}
}

int BinSearch(int arr[], int len, int key) //输入一个数,用折半查找算法求出一组从大到小排列的                                                                
{                                          //数有无此数,并说明是数组第几个元素的值
	assert(arr != NULL);
	if (arr == NULL)
	{
		return NULL;
	}
	int high = 0;
	int low = len - 1;
	int mid = 0;
	while (low >= high)
	{
		mid = (high + low) / 2;
		if (arr[mid] == key)
		{
			return mid;
		}
		else if (arr[mid] > key)
		{
			
			high = mid + 1;
		}
		else
		{
			low = mid - 1;
		}
	}
	return -1;
}

char* Password(char *brr,char*crr,int len1)  //翻译密码
{
	for (int i = 0; i < len1; i++)
	{
		if (brr[i] >= 'A' && brr[i] <= 'Z')  
		{
			
			crr[i] = 26 - (brr[i] - 'A') + 64;
			
		}
		else if (brr[i] >= 'a' && brr[i] <= 'z')
		{
			crr[i] = 26 - (brr[i] - 'a') + 96;
		}
		else
		{
			crr[i] = brr[i];
		}
	}
	
	return crr;
	//	printf("原文为: %s\n", crr);
	
}
int main()
{
	int arr[] = {78,64,63,52,41,40,35,32,29,24,12,10,9,8,4,3,1 };
	int len = sizeof(arr) / sizeof(arr[0]);
	char brr[] = "awejawgnAWFJOI223asf";
	const int len1 = sizeof(brr) / sizeof(brr[0]);
	char crr[len1] = "";

	Get_prime();

	Triangle();

	printf("%d",BinSearch(arr, len, 35));

	printf("密码为:%s\n", brr);
	printf("原文为: %s\n", Password(brr, crr, len1)); 

	return 0;
}
上一篇:orabbix监控默认参数


下一篇:《算法》学习笔记(一)