寒假作业2024.2.14

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;
}

2.请编程实现二维数组计算每一行的和以及列和

复制代码
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>
#include <math.h>
#include <unistd.h>
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 Max1=arr[0][0];
	int Max2=arr[0][0];
	for (int i=0;i<n;i++)
	{
		for (int j=0;j<m;j++)
		{
			if (arr[i][j]>Max1)
			{
				Max2=Max1;
				Max1=arr[i][j];
			}
			else if (arr[i][j]>Max2 && arr[i][j]<Max1) 
			{
				Max2=arr[i][j];
			}
		}
	}
	printf("第二大值为%d\n",Max2);
	return 0;
}

4.请使用非函数方法实现系统函数strcat,strcmp,strcpy,strlen

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
void cat();
void cmp();
void cpy();
void len();
int main(int argc, const char *argv[])
{
	printf("字符串连接strcat\n");
	cat();	
	printf("字符串比较strcmp\n");
	cmp();	
	printf("字符串复制strcpy\n");
	cpy();	
	printf("字符串计算长度strlen\n");
	len();
	return 0;
}
//字符串连接
void cat()
{
	char str1[100];
	char str2[100];
	printf("请输入第一个字符串:");
	gets(str1);
	printf("请输入第二个字符串:");
	gets(str2);

	int i,j;
	for(i=0;str1[i]!='\0';i++);
	for(j=0;str2[j]!='\0';j++)
	{
		str1[i+j]=str2[j];
	}
	str1[i+j]='\0';
	puts(str1);
}
//字符串比较
void cmp()
{
	char str1[100];
	char str2[100];
	printf("请输入第一个字符串:");
	gets(str1);
	printf("请输入第二个字符串:");
	gets(str2);
	int i=0;
	while(str1[i]==str2[i])
	{
		if(str1[i]=='\0')
			break;
		i++;
	}
	if(str1[i]-str2[i]>0)
	{
		puts("str1>str2");
	}
	else if(str1[i]-str2[i]<0)
	{
		puts("str1<str2");
	}
	else if(str1[i]-str2[i]==0)
	{
		puts("str1==str2");
	}
}
//字符串复制
void cpy()
{
	char str1[100];
	char str2[100];
	printf("请输入第一个字符串:");
	gets(str1);
	printf("请输入第二个字符串:");
	gets(str2);

	int i;
    for(i=0;str2[i]!='\0';i++)
    {
        str1[i]=str2[i];
    }
    str1[i]='\0';
    puts(str1);
}
//字符串计算长度
void len()
{
	char str1[100];
	char str2[100];
	printf("请输入第一个字符串:");
	gets(str1);
	printf("请输入第二个字符串:");
	gets(str2);
	int i,j;
 	for(i=0;str1[i]!='\0';i++);
	printf("字符串str1的长度为%d\n",i);
	for(j=0;str2[j]!='\0';j++);
	printf("字符串str2的长度为%d\n",j);
}
相关推荐
hhzz21 分钟前
Tiger AI Platform平台中增加人脸识别功能
图像处理·人工智能·算法·计算机视觉·大模型
从零开始的代码生活_1 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
TsingtaoAI1 小时前
3D高斯泼溅技术发展及其在具身智能领域的应用综述
人工智能·算法·ai·具身智能·高斯泼溅
atunet2 小时前
关于图算法中的连通分量检测与最小割问题7
算法
心运软件2 小时前
银行客户流失预测(Python 完整实战)
算法
邪神与厨二病2 小时前
牛客周赛 Round 153
python·算法
qizayaoshuap4 小时前
# ❌ 井字棋 — 鸿蒙ArkTS Minimax AI算法与博弈系统设计
人工智能·算法·华为·harmonyos
皓月斯语5 小时前
B3842 [GESP202306 三级] 春游 题解
数据结构·c++·算法·题解
atunet5 小时前
树状结构在查询优化中的作用与实现细节7
算法
徐凤年_5 小时前
rog_map参数理解
算法