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;
}


 

转载于:https://www.cnblogs.com/crisxyj/archive/2013/01/07/2849550.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值