一个C语言的小算法题(想了好久,没搞出来。。)

Cubeta 2017-01-05 08:24:37
题目描述
把数字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
...全文
934 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-01-09
  • 打赏
  • 举报
回复
引用 8 楼 cutmelon 的回复:
怎么又是同一道题,还是不同的人问的
同班同学嘛。
赵4老师 2017-01-09
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
[quote=引用 5 楼 yangyangsnr 的回复:]

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int n = 1;
    string str;
    string input;
    cin>>input;
    while( 1 )
    {
        char c[1024];
        sprintf( c, "%d", n );
        str += c;
        if( str.size() >= input.size() ) break;
        n++;
    }
    if( str.size() > input.size() || (str.size() == input.size() && str > input ) ) n--;
    cout<<str.size()<<endl;
    cout<<str<<endl;
    cout<<"Total number is:"<<n<<endl;
    getchar();
    getchar();
    return 0;
} 
伙计,1024不够大嘢。[/quote] 后来发现是str += c;跟1024不够大没关系。
赵4老师 2017-01-06
  • 打赏
  • 举报
回复
Talk is cheap, show me the code.

赵4老师 2017-01-06
  • 打赏
  • 举报
回复
//把数字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 S[1000][4000];
int i,j,L,n;
char ln[4000];
int main() {
    for (i=0;i<1000;i++) {
        L=0;
        for (j=1;j<=i+1;j++) {
            L+=sprintf(S[i]+L,"%d",j);
        }
//      printf("%4d %s\n",i+1,S[i]);
    }
    while (1) {
        if (NULL==fgets(ln,4000,stdin)) break;
        if ('\n'==ln[0]) break;
        if ('\n'==ln[strlen(ln)-1]) ln[strlen(ln)-1]=0;
        n=0;
        for (i=999;i>=0;i--) {
            if (strlen(ln)>=strlen(S[i])) {
                if (strcmp(ln,S[i])>=0) n++;
                n+=i;
                break;
            }
        }
        printf("%d\n",n);
    }
    return 0;
}
cutmelon 2017-01-06
  • 打赏
  • 举报
回复
怎么又是同一道题,还是不同的人问的
  • 打赏
  • 举报
回复
char c[1024]; 这个小了,第999个数有2889位。
赵4老师 2017-01-06
  • 打赏
  • 举报
回复
引用 5 楼 yangyangsnr 的回复:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int n = 1;
    string str;
    string input;
    cin>>input;
    while( 1 )
    {
        char c[1024];
        sprintf( c, "%d", n );
        str += c;
        if( str.size() >= input.size() ) break;
        n++;
    }
    if( str.size() > input.size() || (str.size() == input.size() && str > input ) ) n--;
    cout<<str.size()<<endl;
    cout<<str<<endl;
    cout<<"Total number is:"<<n<<endl;
    getchar();
    getchar();
    return 0;
} 
伙计,1024不够大嘢。
孤雲独去闲 2017-01-06
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int n = 1;
    string str;
    string input;
    cin>>input;
    while( 1 )
    {
        char c[1024];
        sprintf( c, "%d", n );
        str += c;
        if( str.size() >= input.size() ) break;
        n++;
    }
    if( str.size() > input.size() || (str.size() == input.size() && str > input ) ) n--;
    cout<<str.size()<<endl;
    cout<<str<<endl;
    cout<<"Total number is:"<<n<<endl;
    getchar();
    getchar();
    return 0;
} 
Cubeta 2017-01-05
  • 打赏
  • 举报
回复
引用 1楼ID870177103 的回复:
先写一个函数取得Smarandache consecutive number第n位的长度(可以建表再查) 然后对于输入src,取得它的长度m,比较它与第m位Smarandache consecutive number的大小 如果大于等于则结果为m,否则为m-1
如何得到第m位的那个字符串?
ID870177103 2017-01-05
  • 打赏
  • 举报
回复
先写一个函数取得Smarandache consecutive number第n位的长度(可以建表再查) 然后对于输入src,取得它的长度m,比较它与第m位Smarandache consecutive number的大小 如果大于等于则结果为m,否则为m-1

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧