数据结构与算法(实战)

中国MOOC很舒服。

模板与泛型

当重复使用某段代码 拷贝代码不利于debug
c++:推出模板 语法template <class T>;class xxx Table
Java

class xxx<T>{
public T a;
public T b;//不能实例化
xxx<Intger> t =new xxx <>();
Arraylist(顺序表,容器类)
 ArrayList<stu> al=new ArrayList<>();
函数与递归

函数的定义

先写调用
定义函数(考虑返回什么  传入的参数是什么)
		Java  : 什么都不能脱离类
					考虑 static(静态成员变量 方法)
		递归(开辟新的内存空间   所以不能滥用(效率,栈溢出))   自己调用自己    小窍诀  靠信念哈哈哈   
		先写调用 1.明确函数要干嘛
						2.寻找递归结束条件
						3.找出等价关系
数组

c语言 定义数组: int array [N]; int *array =malloc();
*(array+1)
Java 数组定义 引用型 int [] array =new int(Type) [10];

二维数组的应用
大炮打蚊子

#include<stdio.h>
int baord[20][20];//全局数组 
int M,N;	//读入 mn 
int bang(int x,int y,int kill){
	if((x>=0&&x<M)&&(y>=0&&y<N)&&baord[x][y]>0){
		baord[x][y]-=kill;
		if(baord[x][y]<=0) return 1; 
			else{
				return 0;
			}
	}else{
		return 0;
	}
}
int main(){
	 scanf("%d%d",&M,&N);
	 getchar(); 
	 for(int i=0;i<M;i++){
	 	for(int j=0;j<N;j++){
	 		baord[i][j]=getchar()=='0'?0:2;
		 }
		 getchar();
	 }
	 int k;
	 scanf("%d",&k);
	 for(int i =0;i<k;i++){
	 	int x,y;
	 	scanf("%d%d",&x,&y);
	 	int count;
		 count =0;
	 	count+=bang(x,y,2);
	 	count+=bang(x+1,y,1);
	 	count+=bang(x-1,y,1);
	 	count+=bang(x,y-1,1);
	 	count+=bang(x,y+1,1);
	 	printf("%d\n",count);
	 }

	return 0;
	
}

import java.util.Scanner;
public class Main
{
	public static void main(String args[])
	{
		Scanner scanner = new Scanner(System.in);
		int n,m;
		n = scanner.nextInt();
		m = scanner.nextInt();
		String [] mapp;
		mapp = new String[m];
		for(int i = 0; i < n; i ++)
			mapp[i] = scanner.next();
		int [][] index;
		index = new int [25][25];
		int cnts = 0;
		for(int i = 0; i < n; i ++)
		{
			for(int j = 0; j < m; j ++)
			{
				if(String.valueOf(mapp[i].charAt(j)).equals("#"))
				{
					index[i][j] = 2;
				}
				else
				{
					index[i][j] = 0;
					cnts++;
				}
			}
		}
		int k;
		k = scanner.nextInt();
		for(int t = 0; t < k; t ++)
		{
			int a,b;
			a = scanner.nextInt();
			b = scanner.nextInt();
			if(index[a][b]!=0)
				index[a][b] = 0;
			if(a+1<n&&index[a+1][b]!=0)
				index[a+1][b]--;
			if(a-1>=0&&index[a-1][b]!=0)
				index[a-1][b]--;
			if(b-1>=0&&index[a][b-1]!=0)
				index[a][b-1]--;
			if(b+1<m&&index[a][b+1]!=0)
				index[a][b+1]--;
			int temp = 0;
			for(int i = 0; i < n; i ++)
				for(int j = 0; j < m; j ++)
					if(index[i][j]==0)
						temp ++;
			System.out.println(temp-cnts);
			cnts = temp;
		}
		scanner.close();
	}
		
	}
 
————————————————
版权声明:本文为CSDN博主「少女情怀总是诗mmm」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qiulianshaonv_wjm/article/details/89151322
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值