全部评论
我ACl18.9%
ac100%,明天贴代码
分享下我的思路 public void first() {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int M = scanner.nextInt();
int[][] nums = new int[N + 2][M + 2];
int result = 0;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
nums[i][j] = scanner.nextInt();
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
if (nums[i][j] < 1) {
continue;
}
int ***Num = nums[i][j];
result += 2;
int up = ***Num - nums[i - 1][j];
int down = ***Num - nums[i + 1][j];
int left = ***Num - nums[i][j - 1];
int right = ***Num - nums[i][j + 1];
if (up > 0) {
result += up;
}
if (down > 0) {
result += down;
}
if (left > 0) {
result += left;
}
if (right > 0) {
result += right;
}
}
}
System.out.println(result);
}
你这读数据的函数没对吧,in.nextLine()是不是把下一行给读进去了?下面的代码应该是没错的
是不是因为没有循环输入。 public class Main1 {
public int getSum(int[][] cube, int M, int N){
int sum = 0;
for(int temi = 1; temi <= M; temi++){
int temsum = 0;
for(int temj = 1; temj <= N; temj++){
int val = cube[temi][temj];
temsum += (val > cube[temi-1][temj])?val-cube[temi-1][temj]:0;
temsum += (val > cube[temi+1][temj])?val-cube[temi+1][temj]:0;
temsum += (val > cube[temi][temj-1])?val-cube[temi][temj-1]:0;
temsum += (val > cube[temi][temj+1])?val-cube[temi][temj+1]:0;
}
sum += temsum;
}
return sum;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Main1 obj = new Main1();
while(sc.hasNext()){
int M, N;
M = sc.nextInt();
N = sc.nextInt();
int[][] cube = new int[M+2][N+2];
for(int temi = 1; temi <= M; temi++){
for(int temj = 1; temj <= N; temj++){
cube[temi][temj] = sc.nextInt();
}
}
int sum = obj.getSum(cube, M, N);
System.out.println(sum+2*M*N);
}
}
}
楼主有输出吗= =
AC 100的代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
while(scanner.hasNext()) {
int N=scanner.nextInt();
int M=scanner.nextInt();
int[][] all=new int[N][M];
for(int i=0;i<N;i++) {
for(int j=0;j<M;j++) {
all[i][j]=scanner.nextInt();
}
}
int top=N*M;
int num=0;
for(int i=0;i<N;i++) {
for(int j=0;j<M;j++) {
if(i>0&&all[i][j]>all[i-1][j]) {
num+=all[i][j]-all[i-1][j];
}
if(j>0&&all[i][j]>all[i][j-1]) {
num+=all[i][j]-all[i][j-1];
}
if(i<N-1&&all[i][j]>all[i+1][j]) {
num+=all[i][j]-all[i+1][j];
}
if(j<M-1&&all[i][j]>all[i][j+1]) {
num+=all[i][j]-all[i][j+1];
}
if(i==0)
num+=all[i][j];
if(j==0)
num+=all[i][j];
if(i==N-1)
num+=all[i][j];
if(j==M-1)
num+=all[i][j];
}
}
int result=top*2+num;
System.out.println(result);
}
}
}
相关推荐
点赞 评论 收藏
分享
11-01 17:40
门头沟学院 护士 点赞 评论 收藏
分享