Smarandache consecutive number Ⅱ (HNUST 1706 字符串转化水题)
2018-01-11 17:02
381 查看
题目描述
把数字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;
}
把数字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;
}
相关文章推荐
- Smarandache consecutive number Ⅱ (HNUST 1706 字符串转化水题)
- Smarandache consecutive number Ⅱ (HNUST 1706 字符串转化水题)
- Smarandache consecutive number Ⅱ (HNUST 1706 字符串转化水题)
- Smarandache consecutive number Ⅱ (HNUST 1706 字符串转化水题)
- Smarandache consecutive number Ⅱ (HNUST 1706 字符串转化水题)
- Smarandache consecutive number Ⅱ (HNUST 1706 字符串转化水题)
- Smarandache consecutive number Ⅱ (HNUST 1706 字符串转化水题)
- viewHolder.tzgg_number.setText()没有将整形转化为字符串。
- valueAsNumber将字符串转化成数字,number属性
- POJ 1350 Cabric Number Problem(我的水题之路——字符串和数字间的转换)
- oracle将带有逗号的数字字符串转化为可计算的number类型
- js中字符串类型转化toString、parseInt、parseFloat、Number
- oracle 将字符串转化为数值型to_number()
- json对象与json字符串之间的转化
- java字符串大小写转化
- c++的字符串拼接、截取;整型转化成字符串
- 再谈怎样以最简单的方法将泛型为String类型的集合或String类型的数组转化为逗号间隔字符串形式
- oracle数据库中使用sql将clob转化为字符串
- 将字符串转化为tm结构体的数据
- 将流转化为字符串的方法