• 题解 [51nod1385] 凑数字


    题面

    解析

    首先设(n)(l)位,

    那么对于前(l-1)位,(0)~(9)都是要选上的,

    而对于最高位上的数(x),(1)~(x-1)也是要选上的.

    到这里就有了(10*(l-1)+x-1)

    而我们还要考虑最高位的数(x)能不能省(比如说样例就能省).

    设一个数(sum)(l)位,每一位都为(x),

    如果省掉的话,我们能表示的数就一定小于(sum),

    因为(sum)有一位一定表示不出.

    因此我们只需要判断(n)(sum)的大小关系即可.

    code:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #define filein(a) freopen(a".cpp","r",stdin)
    #define fileout(a) freopen(a".cpp","w",stdout);
    using namespace std;
    
    inline int read(){
    	int sum=0,f=1;char c=getchar();
    	while((c<'0'||c>'9')&&c!=EOF){if(c=='-') f=-1;c=getchar();}
    	while(c>='0'&&c<='9'&&c!=EOF){sum=sum*10+c-'0';c=getchar();}
    	return sum*f;
    }
    
    const int N=100001;
    int n,m;
    char s[N];
    
    int main(){
    	cin>>s;n=strlen(s);
    	int ans=(n-1)*10+s[0]-'1',ok=1;
    	for(int i=0;i<n-1;i++){
    		if(s[i+1]<s[i]){ok=0;break;}
    		else if(s[i+1]>s[i]) break;
    	}
    	ans+=ok;
    	printf("%d
    ",ans);
    	return 0;
    }
    
    
  • 相关阅读:
    二分查找法
    AES算法工具类
    SHA加密算法工具类
    使用SQL创建唯一索引
    springboot 启动类CommandLineRunner(转载)
    MD5加密算法工具类
    Android 通过Socket 和服务器通讯
    android 网络连接判断
    android 文件上传,中文utf-8编码
    github打不开问题
  • 原文地址:https://www.cnblogs.com/zsq259/p/11409336.html
Copyright © 2020-2023  润新知