谭浩强C程序设计第四版第六章课后答案

谭浩强C程序设计第四版第六章课后答案

1.筛选法求素数

#include 

int main()
{
	int a[100];
	for (int i = 0;i < 100;i++)
	{
		a[i] = 0;
	}
	a[0] = 1;
	a[1] = 1;
	for (int i = 2;i < 100;i++)
	{
		for (int j = 2;j * i < 100;j++)
		{
			a[i*j] = 1;
		}
	}
	for (int i = 0;i < 100;i++)
	{
		if ( !a[i])
		{
			printf("%d\n",i);
		}
	}
}

2.选择排序法,每次选最小的放到最开始

#include 

int main()
{
	int a[10];
	for (int i = 0;i < 10;i++)
	{
		scanf("%d",&a[i]);
	}
	int t;
	for (int i = 0;i < 9;i++)
	{
		for (int j = i + 1;j < 10;j++)
		{
			if ( a[i] > a[j])
			{
				t = a[i];
				a[i] = a[j];
				a[j] = t;
			}
		}
	}
	printf("sorted array:\n");
	for (int i = 0;i < 10;i++)
	{
		printf("%d\t",a[i]);
	}
	return 0;
}

3.输出对角线之和

#include 

int main()
{
	int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
	int sum = 0;
	for(int i = 0;i < 3;i++)
	{
		for (int j = 0;j < 3;j++)
		{
			if ( i == j || i + j == 2)
			{
				sum = sum + a[i][j];
			}
		}
	}
		printf("sum = %d\n",sum );
}

4.插入排序的一部分

#include 

int main()
{
	int a[15] = {11,22,33,44,55,66,77,88,99};
	int num;
	for (int i =  0;i < 9;i++)
	{
		printf("%3d",a[i]);
	}
	printf("\n请输入需要插入的数:\n");
	scanf("%d",&num);
	int i;
	for ( i = 8;i >= 0 && num < a[i];i--)
	{
		a[i+1] = a[i];
	}
	a[i + 1] = num;
	printf("排好序的序列为:\n");
	for (int i =  0;i < 10;i++)
	{
		printf("%3d",a[i]);
	}



}

5.逆序输出

#include 

int main()
{
	int a[10];
	int b[10];
	for (int i = 0;i < 10;i++)
	{
		scanf("%d",&a[i]);
	}
	for (int i = 0;i < 10;i++)
	{
		printf("a[%d] = %d\t",i,a[i]);
		b[i] = a[9-i];
		printf("b[%d] = %d\n",i,b[i]);
	}
	return 0;
}

6.十行杨辉三角

#include 

int main()
{
	int a[10][10];
	for (int i = 0;i < 10;i++)
	{
		for (int j = 0;j <= i;j++)
		{
			if ( i == j || j == 0)
			{
				a[i][j] = 1;
			}
			else
				a[i][j] = a[i-1][j-1] + a[i-1][j];
			printf("%d\t",a[i][j]);
		}
		printf("\n");
	}
	return 0;
}

7.魔方阵,还没写出来
8.鞍点

#include 

int main()
{
	int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
	for (int i = 0;i < 3;i++)
	{
		for (int j = 0;j<3;j++)
		{
			int flag = 1;
			for (int m = 0;m < 3;m++)
				if ( a[i][j] < a[i][m] || a[i][j] > a[m][j])
					flag = 0;
			if (flag == 1)
				printf("%d 是鞍点。\n",a[i][j]);
		}
	}
	return 0;
}

9.二分查找法

#include 

//折半法查找,二分查找法

int main()
{
	int a[10];

	int lo = 0,hi = 9,mid = lo + (hi - lo)/2;
	int value;

	for (int i = 0;i <10;i++)
	{
		scanf("%d",&a[i]);
		printf("%d\t",a[i]);
	}
	printf("请输入需要查找的值:");
	scanf("%d",&value);
	while(lo < hi)
	{
		if ( value == a[mid])
			break;
		else if ( value > a[mid])
		{
			lo = mid + 1;
		}
		else 
			hi = mid - 1;
		mid = lo + (hi - lo)/2;
	}
	if ( a[mid] != value)
	{
		printf("无此数。\n");
	}
	else
		printf("a[%d] = %d.\n",mid,a[mid]);
	return 0;
}

10.统计字符

#include 

int main()
{
	char c[3][10];
	int character = 0,space = 0,number = 0,other = 0;
	for (int i = 0;i <3;i++)
	{
		gets(c[i]);
	}
	for (int i = 0;i <3;i++)
	{
		for ( int j = 0;j < 10;j++)
		{
			if ( c[i][j] >= 'A' && c[i][j] <= 'Z' || c[i][j] >= 'a' && c[i][j] <= 'z')
			{
				character++;
			}
			else if ( c[i][j] >= '0' && c[i][j] <= '9')
			{
				number++;
			}
			else if ( c[i][j] == ' ')
			{
				space++;
			}
			else
				other++;
			
			printf("%c",c[i][j]);
		}
		printf("\n");
	}
	printf("字母有 %d 个。\n",character);
	printf("数字有 %d 个。\n",number);
	printf("空格有 %d 个。\n",space);
	printf("其他有 %d 个。\n",other);

	return 0;
}

11.输出图案

#include 

int main()
{
	for (int i = 0;i < 5;i++)
	{
		for (int j = 0;j <i;j++)
		{
			printf(" ");
		}
		printf("* * * * *\n");
	}
	return 0;
}

12.翻译电文

#include 

int main()
{
	char c[10];
	for (int i = 0;i < 10;i++)
	{
		scanf("%c",&c[i]);
		if ( c[i] >= 'a' && c[i] <= 'z')
		{
			printf("%3c",'a' + 25 - (c[i] - 'a'));
		}
		else
			printf("%3c",'A' + 25 - (c[i] - 'A'));
	}
	return 0;
}

13.字符串相加

#include 
#include 
int main()
{
	char a[10] = "string1";
	char b[10] = "string2";
	char c[20];

	printf("s1 = %d,s2 = %d\n\n",strlen(a),strlen(b));

	for (unsigned int i = 0;i < strlen(a) + strlen(b);i++)
	{
		if ( i < strlen(a))
		{
			c[i] = a[i];
		}
		else
			c[i] = b[i - strlen(a)];
		printf("%c",c[i]);
	}
	printf("\n");
	return 0;

}

14.字符串比较大小

#include 
#include 
int main()
{
	char a[10],b[10];
	gets(a);
	gets(b);
	//printf("%d\n%d",strlen(a),strlen(b));
	unsigned int len = strlen(a) < strlen(b) ? strlen(a) : strlen(b);
	int flag = 0;
	for (unsigned int i = 0;i<len;i++)
	{
		if ( a[i] > b[i])
		{
			printf("a > b\n%c - %c = %d\n",a[i],b[i],a[i] - b[i]);
			flag = a[i] - b[i];
			break;
		}
		if ( a[i] < b[i])
		{
			printf("a < b\n%c - %c = %d\n",a[i],b[i],a[i] - b[i]);
			flag = a[i] - b[i];
			break;
		}
	}
	if ( flag == 0 )
	{
		if ( strlen(a) > strlen(b))
		{
			printf("a > b,a比b长\n");
		}
		else if( strlen(a) < strlen(b))
			printf("a < b,a比b短\n");
		else
			printf("二者相等。\n");
	}
	return 0;
}

15.字符串拷贝

#include 

int main()
{
	char a[10] = {'1','2','3','4','5','6','\0','7','8','9'};
	char b[20];
	for (int i = 0;i < 10;i++)
	{
		printf("a[%d] = %c\n",i,a[i]);
	}
	printf("\n\n");
	for (int i = 0;i < 10;i++)
	{
		if ( a[i] == '\0')
		{
			break;
		}
		else
			b[i] = a[i];
		printf("b[%d] = %c\n",i,b[i]);	
	}
	

}

你可能感兴趣的:(谭浩强C程序设计第四版课后答案)