1.请编程实现二维数组的杨慧三角
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
int n;
printf("please enter n:");
scanf("%d",&n);
int arr[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
if(j==0 || j==i)
{
arr[i][j]=1;
}
else
{
arr[i][j]=arr[i-1][j]+arr[i-1][j-1];
}
printf("%d\t",arr[i][j]);
}
puts("");
}
return 0;
}
⒉请编程实现二维数组计算每一行的和以及列和
int main(int argc, const char *argv[])
{
int n,m;
printf("please enter n and m:");
scanf("%d %d",&n,&m);
int arr[n][m];
printf("please enter arr:");
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&arr[i][j]);
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
printf("%d ",arr[i][j]);
}
puts("");
}
//计算每一行的和
int sum1;
for (int i=0;i<n;i++)
{
sum1=0;
for (int j=0;j<m;j++)
{
sum1+=arr[i][j];
}
printf("第%d行的和为%d\n",i+1,sum1);
}
//计算每一列的和
int sum2;
for (int j=0;j<m;j++)
{
sum2=0;
for (int i=0;i<n;i++)
{
sum2+=arr[i][j];
}
printf("第%d列的和为%d\n",j+1,sum2);
}
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;
}