1. 请编程实现二维数组的杨辉三角
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n;
scanf("%d", &n);
int a[n][n];
memset(a, 0, sizeof(a));
a[0][0] = 1;
for (int i = 1; i < n; i++)
{
for (int j = 0; j < i + 1; j++)
{
if (0 == j)
{
a[i][j] = 1;
}
else
{
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i + 1; j++)
{
printf("%4d", a[i][j]);
}
puts("");
}
}
2. 请编程实现二维数组计算每一行和以及列和
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n;
scanf("%d", &n);
int a[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("行和:");
for (int i = 0; i < n; i++)
{
int sum = 0;
for (int j = 0; j < n; j++)
{
sum += a[i][j];
}
printf("%4d", sum);
}
puts("");
printf("列和:");
for (int i = 0; i < n; i++)
{
int sum = 0;
for (int j = 0; j < n; j++)
{
sum += a[j][i];
}
printf("%4d", sum);
}
puts("");
return 0;
}
3. 请编程实现二维数组计算第二大值
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n;
scanf("%d", &n);
int a[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
int max = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (max < a[i][j])
{
max = a[i][j];
}
}
}
int sec_max = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (sec_max < a[i][j] && max > a[i][j])
{
sec_max = a[i][j];
}
}
}
printf("第二大:%d\n", sec_max);
return 0;
}
4. 请使用非函数方法实现系统函数strcat,strcmp,strcpy,strlen
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void my_strcmp(char s1[], char s2[])
{
int i = 0;
while (s1[i] == s2[i])
{
if (s1[i] == '\0' || s2[i] == '\0')
{
break;
}
i++;
}
if (s1[i] > s2[i])
{
puts("s1 > s2");
}
else if (s1[i] < s2[i])
{
puts("s1 < s2");
}
else
{
puts("s1 == s2");
}
}
void my_strcpy(char s1[], char s2[])
{
int i = 0;
while (s2[i] != '\0')
{
s1[i] = s2[i];
i++;
}
s1[i] = '\0';
}
int my_strlen(char s[])
{
int i;
for (i = 0; s[i] != '\0'; i++);
return i;
}
void my_strcat(char s1[], char s2[])
{
int i = 0;
while (s1[i] != '\0')
{
i++;
}
int j = 0;
while (s2[j] != '\0')
{
s1[i + j] = s2[j];
j++;
}
s1[i + j] = '\0';
}
int main()
{
char s1[20] = "hello";
char s2[20] = "world";
my_strcmp(s1, s2);
printf("%s len: %d\n", s1, my_strlen(s1));
my_strcpy(s1, s2);
printf("%s\n", s1);
my_strcat(s1, s2);
printf("%s\n", s1);
return 0;
}