第六章
2.用选择法对10个整数排序.
#include <stdio.h>
int main()
{
int i,j,minnum,temp,a[11];
printf("enter data:\n");
for(i = 0; i <= 9; i++)
{
printf("a[%d] = ", i);
scanf("%d", &a[i]);
}
printf("\n");
for(i = 0; i <= 9; i++)
{
printf("%5d", a[i]);
}
printf("\n");
for(i = 0; i <= 9; i++)
{
minnum = i;
for(j = i + 1; j <= 9; j++)
{
if(a[minnum] > a[j])
minnum = j;
}
temp = a[i];
a[i] = a[minnum];
a[minnum] = temp;
}
printf("\n");
for(i = 0; i <= 9; i++)
{
printf("%5d", a[i]);
}
return 0;
}
运行结果
3. 求一个3 X 3的整形矩阵对角线元素之和。
#include<stdio.h>
int main()
{
int array[3][3], sum = 0;
printf("enter data\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
scanf("%d", &array[i][j]);
}
for (int i = 0; i < 3; i++)
sum += array[i][i];
printf("sum = %5d\n", sum);
return 0;
}
运行结果
4. 有一个已经排好序的数组,要求输入一个数后,按原来顺序的规律将它插入数组中
#include<stdio.h>
int main()
{
int a[11] = {1,4,6,9,13,16,19,28,40,100};
int number, end, i;
for(i = 0; i < 10; i++)
printf("%5d",a[i]);
printf("\n");
scanf("%d", &number);
if(number >= a[9])
a[10] = number;
else
{
end = 9;
do{
a[end + 1] = a[end];
end--;
}while(end >= 0 && number < a[end]);
a[end + 1] = number;
}
for(i = 0; i < 11; i++)
printf("%5d", a[i]);
return 0;
}
运行结果
5. 将一个数组中的值按逆序重新存放。例如:原来顺序为8,6,5,4,1。要求改为1,4,5,6,8。
#include<stdio.h>
#define N 5
int main()
{
int a[N], i, temp;
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
for(i = 0; i < N; i++)
printf("%4d", a[i]);
printf("\n");
//逆序存放只需要将前一半依次与后一半数字对调
for(i = 0; i < N/2; i++)
{
temp = a[i];
a[i] = a[N - 1 - i];
a[N - 1 - i] = temp;
}
for(i = 0; i < N; i++)
printf("%4d", a[i]);
printf("\n");
return 0;
}
运行结果
6. 输出一下的杨慧三角(要求输出10行)
#include<stdio.h>
#define N 10
int main()
{
int i, j , a[N][N];
for(i = 0; i < N; i++)
{
a[i][i] = 1;
a[i][0] = 1;
}
for(i = 2; i < N; i++)
{
for(j = 1; j <= i - 1; j++)
{
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
for(i = 0; i < N; i++)
{
for(j = 0; j <= i; j++)
{
printf("%5d", a[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}
运行结果
7. 输出"魔方阵"。所谓魔方阵是指这样的方阵,它的每一行、每一列和对角线之和均相等。
例如:
8 1 6
3 5 7
4 9 2
#include<stdio.h>
int main()
{
int i, j, a[15][15], k, n;
//限定阶数n
while (1)
{
printf("请输入n(1~15):");
scanf("%d", &n);
if (n != 0 && n <= 15 && n % 2 != 0)
break;
else
{
printf("请输入奇数\n");
}
}
//初始化
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
a[i][j] = 0;
}
}
//建立魔方阵
j = n / 2 + 1;
a[1][j] = 1;
for (k = 2; k <= n*n; k++)
{
i = i - 1;
j = j + 1;
if (i<1 && j>n)
{
i = i + 2;
j = j - 1;
}
else if (i<1)
{
i = n;
}
else if (j>n)
{
j = 1;
}
if (a[i][j] == 0)
{
a[i][j] = k;
}
else
{
i = i + 2;
j = j - 1;
a[i][j] = k;
}
}
//输出魔方阵
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
printf("%5d", a[i][j]);
printf("\n");
}
return 0;
}
运行结果
8. 找出一个二维数组中的鞍点,即该位置上的元素在该行上最大,在该列上最小,也可能没有鞍点。!!
#include<stdio.h>
#define N 4
#define M 5
int main()
{
int max, min, rowindex, colindex, flag = 0;
int array[M][N];
printf("请输入%d行%d列的数组:\n", M, N);
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
scanf("%d", &array[i][j]);
}
for (int i = 0; i < M; ++i)
{
// 找到i行上最大的元素,记录该元素在列号colindex
max = array[i][0];
for (int j = 0; j < N; ++j)
{
if (array[i][j] > max)
{
max = array[i][j];
colindex = j;
}
}
// 找max所在列colindex上最小的元素,并记录其所在的行
min = array[0][colindex];
for (int j = 0; j < M; ++j)
{
if (array[j][colindex] < min)
{
min = array[j][colindex];
rowindex = j;
}
}
// 如果最小元素与最小元素相同,并且最小元素也在第i行,则为鞍点
if (max == min && i == rowindex)
{
flag = 1;
printf("鞍点为:%d行%d列的元素%d", rowindex, colindex, max);
break;
}
}
if (0 == flag)
printf("没有鞍点");
return 0;
}
运行结果
9. 有15个数按由大到小顺序存放在一个数组中,输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则输出"无此数"
#include<stdio.h>
#define N 15
int main()
{
int i, number, top, bott, mid, loca, a[N], sign, flag = 1, key;
printf("enter data:\n");
scanf("%d", &a[0]);
i = 1;
while(i < N){
scanf("%d", &a[i]);
if(a[i] >= a[i - 1])
i++;
else
printf("enter this data again:\n");
}
for(i = 0; i < N; i++)
printf("%5d", a[i]);
printf("\n");
while(flag){
printf("input number to look for:");
scanf("%d", &number);
sign = 0;
top = 0;
bott = N - 1;
if((number < a[0]) || (number > a[N -1]))
loca = -1;
while((!sign) && (top <= bott)){
mid = (bott + top)/2;
if(number == a[mid]){
loca = mid;
printf("has found %d, its position is %d\n ", number, loca+1);
//数组位置0是第一个数
sign = 1;
}
else if(number < a[mid])
bott = mid -1;
else
top = mid + 1;
}
if (!sign ||loca == -1)
printf("cannot find %d\n", number);
printf("continue or not (1/0)?");
scanf("%d", &key);
if(key == 0)
flag = 0;
}
return 0;
}
运行结果
13. 编一程序,将两个字符串连接起来,不要用strcat函数
#include<stdio.h>
int main()
{
char s1[80], s2[40];
int index1 = 0, index2 = 0;
printf("请输入字符串s1:");
scanf("%s", s1);
printf("请输入字符串s2:");
scanf("%s", s2);
printf("将s2拼接在s1之后: ");
// 1. 找到s1的末尾
while (s1[index1] != '\0')
index1++;
// 2. 将s2中的字符逐个往s1之后拼接
while (s2[index2] != '\0')
s1[index1++] = s2[index2++];
s1[index1] = '\0';
printf("%s\n", s1);
return 0;
}
运行结果
14. 编写一个程序,将连个字符串s1和s2比较,如果s1 > s2,输出一个整数;若s1 = s2,输出0;若s1 < s2,输出一个负数。不要用strcpy函数。两个字符串用gets函数读入。输出的正数或负数的绝对值应是相比较的两个字符串相对应字符的ASCII码的差值。例如,“A"和“C”相比,由于"A” < “C”,应输出负数,同时由于‘A’与‘C’的ASCII码差值为2,因此应输出"-2"。同理:“And”和"Aid"相比较,根据第2个字符比较结果,“n"比"i"大5,因此应输出"5”
#include <stdio.h>
int main()
{
int i = 0, resu;
char s1[100] = { 0 };
char s2[100] = { 0 };
printf("请输入s1:");
gets(s1);
printf("请输入s2:");
gets(s2);
while((s1[i] == s2[i]) && (s1[i] != '\0'))
i++;
if(s1[i] == '\0' && s2[i] == '\0')
resu = 0;
else
resu = s1[i] - s2[i];
printf("\nresult:%d\n", resu);
return 0;
}
运行结果
15. 编写一个程序,将字符数组s2中的全部字符复制到字符数组s1中,不用strcpy函数。复制时,‘\0’也要赋值过去。’\0’之后的字符不复制
#include<stdio.h>
#include<string.h>
int main()
{
char s1[80], s2[80];
int i;
printf("请输入字符串s2:");
scanf("%s", s2);
printf("将s2拷贝到s1中, s1现在为: ");
for(i = 0; i <= strlen(s2); i++)
s1[i] = s2[i];
printf("s1:%s\n", s1);
return 0;
}
运行结果
例6.7有三个字符串,要求找出其中最大者
#include<stdio.h>
#include<string.h>
int main()
{
char str[3][20];
char string[20];
int i;
for(i = 0; i < 3; i++)
gets (str[i]);
if(strcmp(str[0], str[1]) > 0)
strcpy(string, str[0]);
else
strcpy(string, str[0]);
if(strcmp(str[2], string) > 0)
strcpy(string, str[2]);
printf("\nthe largest string is :\n%s\n", string);
return 0;
}
运行结果