我正在尝试运行代码作为codechef的解决方案来解决这个问题:** https://www.codechef.com/problems/SIMDISH
虽然它正在编译并在我的cmd上提供正确的输出,但它在Codechef的提交上显示运行时错误。我搜索了这个问题,发现我的代码没有任何问题。在我无法使用的问题中存在一个约束:1≤T≤200,其中T是我的代码中的变量'pairs'
public class SimpleController : Controller
{
public class FileViewModel
{
public byte[] Content { get; set; }
public string Extension { get; set; }
public string FileName { get; set; }
}
[HttpPost]
[ValidateInput(false)]
public FileStreamResult covertopdf(string Htmlcontent)
//public FileStreamResult covertopdf(file fo)
{
var result = ExecuteAction(() =>
{
var fileViewmodel = new FileViewModel
{
Content = ConvertHtmlToPdf(Htmlcontent),
//Content= ConvertHtmlToPdf(fo.cont),
Extension = "application/pdf",
FileName = "Policy Information.pdf"
};
return fileViewmodel;
}, "covertopdf");
// return result;
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
// Content is the file
Stream stream = new MemoryStream(result.Content);
return new FileStreamResult(stream, "application/pdf")
{
};
}
public T ExecuteAction<T>(Func<T> action, string method)
{
try
{
return action.Invoke();
}
catch (Exception ex)
{
return default(T);
}
}
protected byte[] ConvertHtmlToPdf(string html, string header = null, string footer = null, bool isPageNumberInFooter = false)
{
// Create ABCpdf Doc object
var doc = new Doc();
if (header == null && footer == null)
doc.Rect.Inset(20, 20);
else
doc.Rect.String = "0 70 600 760"; /*padding from left, padding from bottom, width from left, height from bottom*/
// Add html to Doc
//html = "<html><head></head><body></body></html>";
int theId = doc.AddImageHtml(html);
// Loop through document to create multi-page PDF
while (true)
{
if (!doc.Chainable(theId))
break;
doc.Page = doc.AddPage();
theId = doc.AddImageToChain(theId);
}
var count = doc.PageCount;
/*****************Footer area******************/
if (footer != null)
{
var newfooter = "";
doc.Rect.String = "40 20 580 50";
for (int i = 1; i <= count; i++)
{
doc.PageNumber = i;
if (isPageNumberInFooter)
{
newfooter = footer.Replace("PageNumber", "Page " + i.ToString() + " of " + count.ToString());
int id = doc.AddImageHtml(newfooter);
while (true)
{
if (!doc.Chainable(id))
break;
id = doc.AddImageToChain(id);
}
}
else
doc.AddText(footer);
}
}
/*****************Footer area******************/
// Flatten the PDF
for (int i = 1; i <= doc.PageCount; i++)
{
doc.PageNumber = i;
doc.Flatten();
}
var pdf = doc.GetData();
doc.Clear();
// Get PDF as byte array. Couls also use .Save() to save to disk
return pdf;
}
}
`
我还检查了其他解决方案,其中一个被接受与我的相似。因此无法解决问题。检查一下,它被接受,和我的相似:
#include <stdio.h>
#include <string.h>
#define INGREDIENT 4
int main()
{
int pairs;
int c[INGREDIENT]={0};
char ingredient1[INGREDIENT][11];
char ingredient2[INGREDIENT][11];
scanf("%d", &pairs);
int i,j,k;
for (i = 0; i < pairs; i++)
{
for (j = 0; j <INGREDIENT; j++)
{
scanf("%s", ingredient1[j]);
}
for (j = 0; j <INGREDIENT; j++)
{
scanf("%s", ingredient2[j]);
}
for (k = 0; k <4; k++)
{
for (j = 0; j < 4; j++)
{
if(strcmp(ingredient1[k], ingredient2[j]) == 0)
++c[i];
}
}
}
for (i = 0; i < pairs; i++)
{
if (c[i]>=2)
{
printf("similar\n");
}
else
printf("dissimilar\n");
}
return 0;
}
答案 0 :(得分:0)
感谢朋友,我发现问题是我已宣布
int c[INGREDIENT]={0}; where INGREDIENT = 4
但我试图超越4个元素的限制,每次变量'pair'大于4
for (i = 0; i < pairs; i++)
{
if (c[i]>=2)
需要使用,可能是一个malloc函数。或者可能是calloc,因为我想将数组c初始化为0 ::
int* c;
c = (int*) calloc(pairs, sizeof(int));
也以:
结束free(c);