首页 > 代码库 > C语言学习_test1

C语言学习_test1

1.从键盘上输入5个数,输出最大的、最小的元素以及他们的下标

#include<stdio.h>

int main(void)
{
int i, j, k, max, min;
int a[10] = {0};
printf("input number:");
for(i = 0; i < 5; i++)
scanf("%d", &a[i]);
max = min = a[0];
j = k = 0;


for(i = 0; i < 5; i++)
{
if(max < a[i])
{
max = a[i];
j = i;
}
else if(min > a[i])
{
min = a[i];
k = i;
}
}
printf("%d %d %d %d\n", j, max, k, min);
return 0;
}

2.从键盘上输入16个整数,保存在4*4的二维数组中输出偶数行和偶数列的元素。

#include <stdio.h>


int main(void)
{
int a[5][5] = {0};
int i, j;
printf("input numbers:");
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
scanf("%d", &a[i][j]);
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
{
if(i % 2 == 0 && j % 2 == 0)
printf("%d\n", a[i][j]);
}


return 0;
}

3.在有序数组中,输入一个数,若这个很数存在,则输出数及其位置,若不存在,则输出信息。

#include <stdio.h>
#define M 10


int main(void)
{
int a[M] = {-1,6,8,10,15,20,21,25,29,55};
int n;//查找的数
int low, high, mid;
int found;
low = 0;
high = M - 1;
found = 0;
printf("input n:");
scanf("%d", &n);
while(low <= high)
{
mid = (low + high) / 2;
if(n == a[mid])
{
found = 1;
break;
}
else if(n < a[mid])
high = mid - 1;
else
low = mid + 1;
}
if(found == 1)
printf("index of %d is %d", n, mid);
else
printf("there is not %d", n);
return 0;
}