我是编程的新手,我正在尝试在UVA在线判断上解决929-Number Maze并且我编写了这段代码并且它在代码块中运行良好但是当我在uva-online上提交此代码时判断它显示运行时错误,我只是无法弄清楚我做错了什么。
#include <stdio.h>
#include <stdlib.h>
int min(int x, int y);
int minCost(int **cost, int m, int n) {
int i, j;
int tc[m][n];
tc[0][0] = cost[0][0];
for (i = 1; i < m; i++) {
tc[i][0] = tc[i - 1][0] + cost[i][0];
}
for (j = 1; j < n; j++) {
tc[0][j] = tc[0][j - 1] + cost[0][j];
}
for (i = 1; i < m; i++) {
for (j = 1; j < n; j++) {
tc[i][j] = min(tc[i - 1][j], tc[i][j - 1]) + cost[i][j];
}
}
return tc[m - 1][n - 1];
}
int min(int x, int y) {
if (x < y)
return x;
else
return y;
}
int main() {
int t, a, b, M, N, k;
scanf("%d", &t);
while (t--) {
scanf("%d", &M);
scanf("%d", &N);
int **c = (int **)malloc(sizeof(int*) * M);
for (k = 0; k < N; k++)
*(c + k) = (int *)malloc(sizeof(int) * M);
for (a = 0; a < M; a++) {
for (b = 0; b < N; b++) {
scanf("%d", &c[a][b]);
}
}
int res = minCost(c, M, N);
printf("%d", res);
}
return 0;
}
2
4
5
0 3 1 2 9
7 3 4 9 9
1 7 5 5 3
2 3 4 2 5
1
6
0 1 2 3 4 5
24
15
答案 0 :(得分:1)
*(c+k) = (int*)malloc(sizeof(int)*N);
这应该是N
答案 1 :(得分:1)
一个问题在
var $closeButton = $("<img>");
$(document).ready(function() {
$("#admin").click(function (event) {
var $overlay = $("<div id='overlay'> </div>");
var $closeButton = $("<img class='classMe' id='closeButtonID' src='https://s23.postimg.org/ouup1ib6z/close_button.png'></img>");
$("body").append($overlay);
$overlay.append($closeButton);
alert("jQuery worked");
});
$closeButton.click(function() {
alert("hiiii");
});
});
应该是
package listdisplay.example.com.photoedit;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.FileDescriptor;
import java.io.IOException;
public class MainActivity_photoedit extends AppCompatActivity {
private static final int RORC =0;
ImageView iview;
Button button,button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_photoedit);
iview= (ImageView) findViewById(R.id.imageView);
button =(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent,RORC);
}
}
);
}
@Override
public void onActivityResult(int requestCode,int resultCode,Intent resultData){
if(requestCode==RORC && resultCode==RESULT_OK){
Uri uri =null;
if(resultData!=null){
uri=resultData.getData();
try {
Bitmap bitmap= getBitmapFromUri(uri);
iview.setImageBitmap(bitmap);
}
catch (IOException e){
e.printStackTrace();
}
}
}
}
private Bitmap getBitmapFromUri(Uri uri) throws IOException{
ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri,"r");
FileDescriptor fileDescriptor= parcelFileDescriptor.getFileDescriptor();
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
parcelFileDescriptor.close();
return bitmap;
}
}
答案 2 :(得分:0)
欢迎来到精彩的编程世界。
仔细的代码审查会显示此错误:
循环测试和malloc
参数中的初始化循环不正确,应该是:
for (k = 0; k < M; k++) {
c[k] = malloc(sizeof(int) * N);
}
发布后,您的程序会调用未定义的行为。它可能偶然产生预期的结果(在您的系统上观察到),但是当您访问超出分配大小的数组时(如在uva-online裁判站点上观察到的那样),它也可能产生运行时错误。