code jam Problem C. Recycled Numbers

http://code.google.com/codejam/contest/1460488/dashboard#s=p2

我做模拟题是一向不行的  这道题搞了我m久  发现我对数字真的是非常不敏感~

只是解决了small data而已 large data模拟肯定是不行的 

这道题 有一个点我觉得很巧妙

而且终于将sprintf sscanf strncpy 用了一下 挺好用哒~

#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;
#define MAX 2000000
int vis[MAX];
int m,n;
int count( int num )
{
    int  val, i, sum = 0;
    char tmp[10]={0}, head[10]={0}, str[10];
    sprintf(str, "%d", num);
    int len = strlen(str);
    for( i=0; i<len; i++ )
    {
        if( str[i] == '0' )
            continue;
        strncpy(tmp, str+i, len);
        strncpy(head, str, i);
        strcat(tmp, head);  //head初始化时已经在后面加了'\0',否则要加head[i]='\0'
        sscanf(tmp, "%d", &val);
        // val > num 简化了计算呐!! 就是循环得到的数比num小就忽略 因为肯定已经加过了
        // vis[val]!=num 是避免重复计算 题目要求
        if( val>=m && val<=n && val>num  && vis[val]!=num )
        {
            sum++,  vis[val] = num;
        }
    }
    return sum;
}
int main()
{
    int nCase = 1, i, cas, tot;
    freopen("./new.in", "r", stdin);
    freopen("./new.out", "w", stdout);
    scanf("%d", &cas);
    while( cas-- )
    {
        tot = 0;
        scanf("%d %d", &m, &n);
        memset(vis, -1, sizeof(vis));
        for( i=m; i<=n; i++ )
        {
            tot += count(i);
        }
        printf("Case #%d: %d\n", nCase++, tot);
    }
    return 0;
}


 

posted @ 2013-01-07 16:05  April_Tsui  阅读(276)  评论(0编辑  收藏  举报