Smarandache consecutive number Ⅱ (HNUST 1706 字符串转化水题)

本文介绍了一种特殊的数字序列——Smarandache连续数,并提供了一个C语言程序来计算给定范围内这种特殊数的数量。该程序通过模拟方法判断输入数字是否属于Smarandache连续数序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述
把数字1到n连接起来就构成了第n个Smarandache consecutive number。Smarandache的前17个数如下:
1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910
1234567891011
123456789101112
12345678910111213
1234567891011121314
123456789101112131415
12345678910111213141516
1234567891011121314151617
给定一个正整数n,问[1,n]之间含有多少个Smarandache consecutive number? 
输入
多行数据组成。
每行一个整数n,n大于1并且小于第1000个Smarandache consecutive number。
输出
对于每一行的数据n,输出一行,即[1,n]之间Smarandache consecutive number的个数。


样例输入
1
13
1233
样例输出
1
2

3

思路:纯模拟,只需要讨论是一位数还是两位数或者是三位数,然后与标准比较

#include <stdio.h>
#include<string.h>
char str[3000];
int standard[1000],compare[1000];
int main()
{
 
    int itemp=0;
    for(;itemp<1000;itemp++)
     standard[itemp]=itemp+1;
     //freopen("e:\\in.txt","r",stdin);
    while(gets(str)!=NULL)
    {
           //输入字符数组 NULL
       int  num=strlen(str),result=num,ipos=0;
        //测出长度方便讨论
            for(itemp=0;itemp<=num&&itemp<9;itemp++,ipos++)
              compare[ipos]=(int)(str[itemp]-'0');
            if(9<num)
            {
                for(itemp=9;itemp<=num&&itemp<189;itemp+=2,ipos++)
               compare[ipos]=(int)(str[itemp]-'0')*10+(int)(str[itemp+1]-'0');
               result=(num-9)/2+9;
            }
 
            if(189<num)
                {
                    for(itemp=189;itemp<=num;itemp+=3,ipos++)
                compare[ipos]=(int)(str[itemp]-'0')*100+(int)(str[itemp+1]-'0')*10+(int)(str[itemp+2]);
                result=(num-189)/3+99;
                }
                //讨论完成开始比较
                int re=result;
            for(itemp=0;itemp<re;itemp++)
                if(compare[itemp]<standard[itemp])
                   {result=result-1;break;}
            printf("%d\n",result);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值