谭浩强【C程序设计】课后编程题(第六章)

#include "user.h"
//homeWork
//P168

//6.1
void num_100(void)
{
	int i=100, j=2;
	for (i = 100; i > 2; i--)
	{
		while (i % j != 0 && i > j)
		{
			j++;
			printf("%d\n", i);
			break;
		}
		j = 2;
	}

}

//6.2
//选择法就是依次选择出第一大,第二大
void sort_10Num(void)
{
	int a[10] = { 12,13,45,86,58,12,75,85,96,100 };
	// 下面的语句用于输入10个数
	//for(int i=0;i<10;i++)
	//{
	//	scanf_s("%d", &a[i]);
	//}
	int base,mid; //选定基准
	int index;
	for (int j = 0; j < 10; j++)
	{
		base = a[j]; //选基准 base = 12
		for (int i = j; i < 10; i++) // 第一趟排9次
		{
			if (a[i] >= base)  // 大于第一个元素
			{
				base = a[i];
				index = i;
			}
		}
		{ // 交换两元素
			mid = a[index];
			a[index] = a[j];
			a[j] = mid;
		}
	}
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", a[i]);
	}
}

//6.3
void line_Xsum(void)
{
	int arr[3][3] = { {1,2,3},{4,5,6},{7,8,8} };
	int i, j;   
	int sum = 0;
	int tot = 0;
	// 主对角线元素下标一致
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
		{
			if(i==j)
			{
				sum += arr[i][j];
			}
		}
	}
	// 副对角线下标之和为2
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
		{
			if (i+j==2)
			{
				tot += arr[i][j];
			}
		}
	}
	printf("Sum of main diagonal elements is: %d.\n", sum); // 主对角线元素之和
	printf("Sum of sub diagonal elements is: %d.\n", tot);; //副对角线元素之和
}

//6.4

void insert_1Numthensquence(void)
{
	#define N 10
	int a[N] = { 1,2,3,5,6,7,8,9,10,15 };
	int insert_index=0;
	int insert_num = 0;
	int b[N + 1] = {0};          //排序后的数组
	int pr_index=0;
	printf("Please input a number:\n"); 
	scanf_s("%d", &insert_num);
	// 寻找插入位置
	for (int i = 0; i < N-1; i++)
	{
		if (a[i] <= insert_num && a[i + 1] > insert_num)
		{
			insert_index = i + 1;  //得到插入位置
		}
	}
	printf("插入序号:%d\n", insert_index);
	b[insert_index] = insert_num;  // 插入数据
	// 插入
	for (int i = 0; i < N+1; i++)
	{
		if (i == insert_index)
		{
			b[i] = insert_num;  
		}
		else if (i > insert_index)
		{
			b[i] = a[i-1];
		}
		else
		{
			b[i] = a[i];
		}
	}
	while (pr_index != N + 1)
	{
		printf("%d ", b[pr_index]);
		pr_index++;
	}
}

//6.5
void reverse_Array(void)
{
	int i,j=0;
	int temp=0;
	int a[10] = {1,3,5,7,9,11,45,85,22,10 };
	for (i = 0; i < 10/2; i++)
	{
		temp = a[i];
		a[i] = a[9 - i];
		a[9 - i] = temp;
	}
	for (j = 0; j < 10; j++)
	{
		printf("%d ", a[j]);
	}
}

//6.6
// 杨辉三角
void yanghui_Triangle(void)
{
	int i, j;
	int s = 1;
	for (i = 1; i <= 10; i++)  // 行编号从1开始,列从0开始
	{
		for (j = 0; j < i; j++)   // 第i行有i列
		{
			if (i == j || j==0)  // 第0列和最后一列为1
			{
				s = 1;
			}
			else
			{
				s = s/j* (i - j);    // 第i行的j列数k可表示为k= C(i-1,j-1)
			}
				printf("%d ",s); // 加空格分隔
		}
		printf("\n");         // 输出一行后换行
	}
}

//6.7
// 输出三阶魔法阵
// 重点在于计算行列坐标
// 更多阶请参考
// 每一个数存放的行比上一个数的行减1;每一个数存放的列比上一个数的列加1;
// https://blog.csdn.net/MacanS/article/details/51209557
void rubic_Cubearr(void)
{
	int n = 3; //
	int i,j;
	int num = 0;
	int arr[3][3] = {0};
	int row, colum,next_row,next_colum;
	row = 0;
	colum = n / 2;
	while(num++!=9)   // 1-9
	{
		arr[row][colum] = num; // 把1放在第一行中间一列
		next_colum = colum;    // 从2开始
		next_row = row;
		// 行数-1
		// 越界自动回0
		next_row = (next_row)==0? n-1: next_row -1; 
		// 列数+1
		next_colum = (next_colum)==2?0:next_colum+1; // 列数+1

		if((row==0 && colum ==n-1) || arr[next_row][next_colum]!=0)           // 上述规则位置上有数字或上一个数位于1行n列
		{
			next_row = row + 1; // 行数减一
			next_colum = colum;  // 列数不变
		}
		row = next_row;
		colum = next_colum;
	}
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < n; j++)
		{
			printf("%d  ", arr[i][j]);
		}
		printf("\n");
	}

}

//6.8
//鞍点:该行最大,该列最小
void saddle_Point(void)
{
	int arr[4][4] = { {   1,42,  5,  8},
							{  9,67,  5,  6},
							{  8,41,  2,40},
							{36,58,78,14} };
	int i, j;
	int line=0, colum=0;
	int max=0;
	int index=0;
	for (i = 0; i < 4; i++)
	{
		max = arr[i][0];                 // 每行第一个元素作为比较值      
		for (j = 0; j < 4; j++)
		{
			if(arr[i][j] >= max)
			{
				max = arr[i][j];               // 得到给该行最大元素
				colum = j;                   // 记录行列索引
				line = i;
			}

		}
		for (j = 0;j < 4; j++)
		{
			if (arr[j][colum] >= arr[line][colum])  // 
			{
				if(index==3)
				{
					printf("The saddle point is %d\n", arr[line][colum]);
					printf("Index is (%d,%d)\n", line, colum);
				}
				index++;
			}
		}
		line = 0;
		colum = 0;
		index = 0;
	}
}

//6.9 
// 折半法查找索引
void find_SquenceIndex(void)
{
	int arr[15] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
	int input;
	scanf_s("%d", &input);
	int  mid,left,right;
	left = 0;
	right = 14;
	int i = 15;
	while(left+1!=right)
	{
		mid = (left + right) / 2;
		if (input == arr[mid])
		{
			printf("the finded number is %d.\n", arr[mid]);
			printf("the index of being finded number is %d.\n", mid);
			break;
		}
		else if (input > arr[mid])
		{
			left = mid+1;
		}
		else
		{
			right = mid-1;
		}
	}
}

//6.10
void cal_Character(void)
{
	char a[] = "The pattern was about March 2017. I bought a 1TB hard disk.";
	int  j;
	int achar=0, Achar=0, num=0, space=0, other=0;
	int len = strlen(a);
	printf("%s\n", a);
		for (j = 0; j < len; j++)
		{
			if (a[j] >= 'a' && a[j] <= 'z')
			{
				achar++;
			}
			else if (a[j] >= 'A' && a[j] <= 'Z')
			{
				Achar++;
			}
			else if (a[j] >= '0' && a[j] <= '9')
			{
				num++;
			}
			else if (a[j] ==' ')
			{
				space++;
			}
			else
			{
				other++;
			}
	}
	printf("The number of lowcase letter is %d\n", achar);
	printf("The number of capital letter is %d\n", Achar);
	printf("The number of number  is %d\n", num);
	printf("The number of space is %d\n", space);
	printf("The number of other is %d\n", other);
}

//6.11
void printf_Pattren(void)
{
	int i, j;
	for (i = 1; i <= 5; i++)
	{
		j = i;
		while(j--)
		{
			printf(" ");
		}
		printf("* * * * *\n");
	}
}


//6.12
//有一行电文,已按下面规律译成密码
void retrans_Password(void)
{
	int i;
	char a[100];
	char cap_start = 'A';
	char let_start = 'a';
	printf("please enter the code:\n");
	gets(a);
	// 编码
	for (i = 0; i < 100; i++)
	{
		if((a[i] >= 'A' && a[i] <= 'Z'))
		{
			a[i] = 'Z'-(a[i] -'A'); 
		}
		if(a[i] >= 'a' && a[i] <= 'z')
		{
			a[i] = 'z' - (a[i] - 'a');
		}
	}
	puts(a);
	// 译码
	for (i = 0; i < 100; i++)
	{
		if ((a[i] >= 'A' && a[i] <= 'Z'))
		{
			a[i] = 'Z' - (a[i] - 'A');
		}
		if (a[i] >= 'a' && a[i] <= 'z')
		{
			a[i] = 'z' - (a[i] - 'a');
		}
	}
	puts(a);
}

//6.13
void rewrite_Strcpy(void)
{
	char str1[100];
	char str2[100];
	char str3[200];
	printf("please enter the string1:\n");
	gets(str1);
	printf("please enter the string2:\n");
	gets(str2);
	memcpy(str3, str1, strlen(str1));
	memcpy(str3+ strlen(str1), str2, strlen(str2));
	str3[strlen(str1) + strlen(str2)] = '\0';
	puts(str3);
}

//6.14
int comp_2String(void)
{
	char str1[100];
	char str2[100];
	int i,j=0;
	printf("please enter the string1:\n");
	gets(str1);
	printf("please enter the string2:\n");
	gets(str2);
	for (i = 0; i < 100; i++)
	{
		if (str1[i] > str2[i])
		{
			return str1[i] - str2[i];
		}
		else if(str1[i]<str2[i])
		{
			return str1[i]-str2[i];
		}
		else
		{
			j++;
			if(j==100)
			return 0;
		}
	}
}

//6.15
void arr_Copy(void)
{
	char str1[100];
	char str2[100];
	int i, j = 0;
	printf("please enter the source string:\n");
	gets(str1);
	for (i = 0; str1[i] != '\0'; i++)
	{
		str2[i] = str1[i];
	}
	str2[i] = '\0';
	printf("please enter the target string:\n");
	puts(str2);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vicssic

与你一起成长

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值