南阳理工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[1000010];
char c[11];
int main()
{
    int i,j,m,n;
    int k,x,y;
    memset(a,0,sizeof(a));
    for(i=0;i<1000001;i++)
    {
        sprintf(c,"%d",i);
        if (strstr(c,"521")!=NULL)
            a[i]++;
        if (strstr(c,"1")!=NULL&&strstr(c,"2")!=NULL&&strstr(c,"5")!=NULL)
            a[i]++;
    }
    k = 1;
    /*for (i=510;i<522;i++)
    printf("%d\n",a[i]);*/
    while (scanf("%d%d",&m,&n)!=EOF&&(m!=0||n!=0))
    {
        y = 0;x = 0;
        for (j=m;j<=n;j++)
        {
            if (1==a[j])
                x++;
            if (2==a[j])
            {
                y++;
                x++;
            }
        }
        printf("Case %d:%d %d\n",k,x,y);
        k++;
    }
    return 0;
}
</span>

 

 

 



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

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

 


 

posted @ 2015-04-03 15:40  小小暮雨  阅读(155)  评论(0编辑  收藏  举报