一样的代码,为什么java不行,c++就可以。求求求 -.-
  • 板块P3397 地毯
  • 楼主xiaoqi666
  • 当前回复9
  • 已保存回复9
  • 发布时间2023/1/3 23:34
  • 上次更新2023/10/24 05:39:02
查看原帖
一样的代码,为什么java不行,c++就可以。求求求 -.-
437005
xiaoqi666楼主2023/1/3 23:34

用c++一次就过,java不是TLE就是MLE,真的很离谱。。求大佬解释 java代码如下:

package aaa;

import java.util.Scanner;

public class t15 {
	static int b[][] = new int[1010][1010];
	static int n,m;
	static void insert(int x1,int y1,int x2,int y2,int c)
	{
		b[x1][y1] += c;
		b[x2+1][y1] -= c;
		b[x1][y2+1] -= c;
		b[x2+1][y2+1] += c;
	}
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		n = in.nextInt();
		m = in.nextInt();
		while(m > 0)
		{
			int x1,y1,x2,y2;
			x1 = in.nextInt();
			y1 = in.nextInt();
			x2 = in.nextInt();
			y2 = in.nextInt();
			insert(x1,y1,x2,y2,1);
			m --;
		}
		for(int i = 1;i <= n;i ++)
		{
			for(int j = 1;j <=n;j ++)
			{
				b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i-1][j-1];
				System.out.printf("%d ",b[i][j]);
			}
			System.out.println();
			
		}
		
		
				
	}

}

c++代码如下:

#include<iostream>
using namespace std;
const int N = 1010;
int a[N][N];
void insert(int x1,int y1,int x2,int y2,int c)
{
	a[x1][y1] += c;
	a[x2 + 1][y1] -= c;
	a[x1][y2 + 1] -= c;
	a[x2 + 1][y2 + 1] += c; 
}
int main()
{
	int n,m;
	cin >> n >> m;
	while(m --)
	{
		int x1,y1,x2,y2;
		cin >> x1 >> y1 >> x2 >> y2;
		insert(x1,y1,x2,y2,1);
	}
	for(int i = 1;i <= n;i ++)
		{
			for(int j = 1;j <=n;j ++)
			{
				a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i-1][j-1];
				cout << a[i][j] << " ";
			}
			cout << endl;
			
		}
		return 0;
 } 
2023/1/3 23:34
加载中...