您的位置:首页 > 其它

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

2018-01-11 17:02 344 查看
题目描述

把数字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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: