全部评论
我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-14 08:35
河北地质大学华信学院 网络工程师 菜菜咪:1. 可以使用简历网站的模版,美观度会更好一点 2. 邮箱可以重新申请一个,或者用qq邮箱的别名,部分hr可能会不喜欢数字邮箱 3. 项目经历最好分点描述,类似的项目很多,可以参考一下别人怎么写的 4. 自我评价可加可不加,技术岗更看重技术。最后,加油,优秀士兵
点赞 评论 收藏
分享