C语言程序设计现代方法第二版,第九章课后编程习题

第一题

#include 
#include

void selection_sort(int n, int a[]) 
{

	int i, j, max = a[0];

	for (i = 0; i < n; i++) 
	{
		if (max <= a[i]) 
		{
			max = a[i];
			j = i;
		}
	}
	a[j] = a[n - 1];
	a[n - 1] = max;
	n--;
	if (n == 0) 
		return;
	else
		selection_sort(n, a);
}
int main(void)
{
	int n;
	printf("How many numbers do you want to resevse? ");
	scanf("%d", &n);
	int a[99];
	printf("Enter the number you want: ");
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);
	selection_sort(n, a);
	printf("In sorted order: ");
	for (int i = 0; i < n; i++) 
		printf("%d ", a[i]);
	system("pause");
	return 0;
}

第二题

#include 
#include
float js(float income)
{
	float out;
		if (income < 750)
			out = 750 * 0.01;
		else if (income < 2250)
			out = (income - 750)*0.02 + 7.50;
		else if (income < 3750)
			out = (income - 2250)*0.03 + 37.50;
		else if (income < 5250)
			out = (income - 3750)*0.04 + 52.50;
		else if (income < 7000)
			out = (income - 7000)*0.05 + 142.50;
		else
			out = (income - 750)*0.06 + 230.0;
		return out;
}
int main(void)
{
	float income;
	printf("Enter your income:");
	scanf_s("%f", &income);
	printf("The outmoney is %.2f$", js(income));
	system("pause");
	return 0;
}

第三题

#include 
#include
float js(float income)
{
	float out;
		if (income < 750)
			out = 750 * 0.01;
		else if (income < 2250)
			out = (income - 750)*0.02 + 7.50;
		else if (income < 3750)
			out = (income - 2250)*0.03 + 37.50;
		else if (income < 5250)
			out = (income - 3750)*0.04 + 52.50;
		else if (income < 7000)
			out = (income - 7000)*0.05 + 142.50;
		else
			out = (income - 750)*0.06 + 230.0;
		return out;
}
int main(void)
{
	float income;
	printf("Enter your income:");
	scanf_s("%f", &income);
	printf("The outmoney is %.2f$", js(income));
	system("pause");
	return 0;
}

第四题

#include 
#include
#include 

void generate_random_walk(char walk[10][10])
{
	srand((unsigned)time(NULL));
	int n = 0, i, j, way, m = 1, h = 0;
	for ( i = 0; i < 10; i++)
	{
		for ( j = 0; j < 10; j++)
		{
			walk[i][j] = '.';
		}
	}
	walk[0][0] = 'A';
	i = 0; j = 0;
	for (; m < 26; m++)
	{
		while (n == 0)
		{
			way = rand() % 4;
			if (((i == 0) || (walk[i - 1][j] != '.')) && (way == 0))//0
				way = 1;
			else if (((j == 9) || (walk[i][j + 1] != '.')) && (way == 1))//1
				way = 2;
			else if (((i == 9) || (walk[i + 1][j] != '.')) && (way == 2))//2
				way = 3;
			else if (((j == 0) || (walk[i][j - 1] != '.')) && (way == 3))//3
				way = 0;
			else
				n = 1;
			h++;;
		}
		if (h > 4)
			break;
		n = 0;
		h = 0;
		switch (way)
		{
		case 0:walk[i - 1][j] = 'A' + m; i -= 1; break;
		case 1:walk[i][j + 1] = 'A' + m; j += 1; break;
		case 2:walk[i + 1][j] = 'A' + m; i += 1; break;
		case 3:walk[i][j - 1] = 'A' + m; j -= 1; break;
		default:break;
		}
	}
}

void printf_array(char walk[10][10])
{
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			printf("%c ", walk[i][j]);
		}
		printf("\n");
	}
}

int main(void)
{
	char num[10][10];
	generate_random_walk(num);
	
	printf_array(num);
	system("pause");
	return 0;
}

第五题

#include 
#include

void creat_magic_square(int n, int magic_square[99][99])
{
	int i = 0, j, m, ii, jj;
	j = n / 2;
	magic_square[i][j] = 1;
	for (m = 2; m <= (n*n); m++)
	{
		ii = i;
		jj = j;
		i = (i + n - 1) % n;
		j = (j + n + 1) % n;
		if (magic_square[i][j] == 0)
			magic_square[i][j] = m;
		else
		{
			i = (ii + n + 1) % n;
			j = jj;
			magic_square[i][j] = m;
		}
	}
}

void print_magic_square(int n, int magic_square[99][99])
{
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
			printf("\t%d", magic_square[i][j]);
		printf("\n");
	}
}

int main(void)
{
	int n, i = 0, j, m, ii, jj, a[99][99] = { 0 };
	printf("Enter the siae oh magic square:");
	scanf("%d", &n);
	creat_magic_square(n, a);
	print_magic_square(n, a);
	system("pause");
	return 0;
}

第六题

#include 
#include

int js(int x)
{
	int y;
	y = 3 * x*x*x*x*x + 2 * x*x*x*x - 5 * x*x*x - x*x + 7 * x - 6;
	return y;
}
int main(void)
{
	int x;
	printf("Enter x:");
	scanf("%d", &x);
	printf("The resule is %d\n", js(x));
	system("pause");
	return 0;
}

第七题

#include 
#include


int power(int x, int n)
{
	if (n == 0) {
		return 1;
	}
	else if (n % 2 == 0 && n != 0) {
		return power(x, n / 2) * power(x, n / 2);
	}
	else if (n % 2 != 0) {
		return x * power(x, n - 1);
	}
}
int main(void)
{
	int x, n,a;
	printf("Enter x and n:");
	scanf("%d %d", &x, &n);
	printf("Result:%d\n", power(x, n));
	system("pause");
	return 0;
}

第八题

#include 
#include
#include

int roll_dice(void)
{
	int i, j;
	srand((unsigned)time(NULL));
	i = abs(rand() % 6 + 1);
	j = abs(rand() % 6 + 1);
	return i + j;
}
int play_game()
{	
	int i = roll_dice(),ii = 0;
	printf("You rolled : %d\n", i);
	if (i == 7 || i == 11)
		return 1;
	else if (i == 2 || i == 3 || i == 12)
		return 0;
	else
	{
		printf("You point is: %d\n", i);
		do{
			ii = roll_dice();
			printf("You rolled: %d\n", ii);
			if (ii == 7)
				return 0;
			else if (ii == i)
				return 1;
		} while (ii != 7 || ii != i);
	}
}
int main(void)
{
	int r,i = 0, j = 0,k ;
	char c;
	do {
		k = 0;
		r = play_game();
		if (r == 1)
		{
			i++;
			printf("You win!\n");
		}
		else
		{
			j++;
			printf("You lose!\n");
		}
		printf("PLay again?");
		c = getchar();
		getchar();
		if (c == 'y')
			k = 0;
		else if(c == 'n')
			k = 1;
	} while (k == 0);
	printf("Win: %d   Losses: %d\n", i, j);
	system("pause");
	return 0;
}

你可能感兴趣的:(c)