南阳理工ACM975--关于521

时间:2023-03-09 02:20:26
南阳理工ACM975--关于521

http://acm.nyist.net/JudgeOnline/problem.php?pid=975

这是我的源码。一直超时,一直超时。

还有itoa函数函数的使用。可以改成sprintf(str, " %d" , num);

但是杭电的ACM complier不接受itoa函数,原因是itoa是c++的扩展函数,当然不能指望任何编译器都可以编译了~~

注:

课件内容:

itoa不是ansi函数, 能将整数转换为字符串而且与ANSI标准兼容的方法是使用sprintf()函数
 int num = 100;

char str[25];

sprintf(str, " %d" , num);

<span style="font-size:14px;">
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int a[];
char c[];
int main()
{
int i,j,m,n;
int k,x,y;
memset(a,,sizeof(a));
for(i=;i<;i++)
{
sprintf(c,"%d",i);
if (strstr(c,"")!=NULL)
a[i]++;
if (strstr(c,"")!=NULL&&strstr(c,"")!=NULL&&strstr(c,"")!=NULL)
a[i]++;
}
k = ;
/*for (i=510;i<522;i++)
printf("%d\n",a[i]);*/
while (scanf("%d%d",&m,&n)!=EOF&&(m!=||n!=))
{
y = ;x = ;
for (j=m;j<=n;j++)
{
if (==a[j])
x++;
if (==a[j])
{
y++;
x++;
}
}
printf("Case %d:%d %d\n",k,x,y);
k++;
}
return ;
}
</span>

后才才知道。我代码中的第二次循环是导致代码超时的缘故。让我纠结好久!!!

<span style="font-size:14px;">
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int a[][]={};
char c[];
int main()
{
int i,j,m,n;
int k;
int w = ,p = ;
memset(a,,sizeof(a));
for(i=;i<;i++)
{
sprintf(c,"%d",i);
if (strstr(c,"")!=NULL&&strstr(c,"")!=NULL&&strstr(c,"")!=NULL)
{
w++;
if (strstr(c,"")!=NULL)
p++; }
a[][i]+=w;
a[][i]+=p;
}
k = ;
/*for (i=510;i<522;i++)
printf("%d\n",a[i]);*/
while (scanf("%d%d",&n,&m)!=EOF&&(m!=||n!=))
{
printf("Case %d:%d %d\n",k,a[][m]-a[][n-],a[][m]-a[][n-]);
k++;
}
return ;
} </span>