一样的代码,为什么java不行,c++就可以。求求求 -.-

P3397 地毯

xiaoqi666 @ 2023-01-03 23:34:57

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

by KKKZOZ @ 2023-01-04 00:36:05

加了快读快写,这下能过了

你用Java时每道题都用这个快读快写模板吧,大部分题都能没问题

import java.io.*;

public class Main {

    static BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static PrintWriter cout = new PrintWriter(bw);
    static StreamTokenizer st = new StreamTokenizer(buf);

    public static int nextInt() throws IOException {
        st.nextToken();
        return (int) st.nval;
    }

    public static long nextLong() throws IOException {
        st.nextToken();
        return (long) st.nval;
    }

    public static String nextString() throws IOException {
        st.nextToken();
        return st.sval;
    }

    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) throws IOException {
        n = nextInt();
        m = nextInt();
        while(m > 0)
        {
            int x1,y1,x2,y2;
            x1 = nextInt();
            y1 = nextInt();
            x2 = nextInt();
            y2 = 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];
                cout.print(b[i][j]+" ");
            }
            cout.println();
            cout.flush();

        }

    }

}

by KKKZOZ @ 2023-01-04 00:45:11

用Java写还容易遇到栈溢出,比如层数较深的dfs时,具体体现为10个点能过七八个,剩下一两个点怎么都过不了

如果遇到了,你可以去看看那道题的Java提交情况,看看别人100分的main函数是不是极其的特别,你照着他们的把main函数改改就行


by KKKZOZ @ 2023-01-04 01:02:20

栈溢出的那几个点一般都是RE,(不是MLE或者TLE),当你觉得算法怎么也错不了的时候(10个点能过9个点),大概率栈溢出了


by xiaoqi666 @ 2023-01-04 14:27:08

@KKKZOZ 好的,谢谢大佬,非常感谢。


by zzf12345666 @ 2023-01-31 00:57:44

@KKKZOZ tql


by BaiChuan_Nayuta @ 2023-03-11 16:31:24

@KKKZOZ tql!!感谢!


by joeyouca1 @ 2023-08-07 11:50:46

@KKKZOZ 你好,请问怎么看别人关于某题的Java提交情况?感谢感谢


by KKKZOZ @ 2023-08-07 23:10:30

@Joe_hh 在那道题的提交记录里面可以按照语言或者用户ID进行筛选


by joeyouca1 @ 2023-08-08 08:33:53

@KKKZOZ orz


by DauntlessHSM @ 2024-03-29 20:22:25

@KKKZOZ 感谢!


|