【33.33%】【codeforces 552B】Vanya and Books

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vanya got an important task — he should enumerate books in the library and label each book with its number. Each of the n books should be assigned with a number from 1 to n. Naturally, distinct books should be assigned distinct numbers.

Vanya wants to know how many digits he will have to write down as he labels the books.

Input
The first line contains integer n (1?≤?n?≤?109) — the number of books in the library.

Output
Print the number of digits needed to number all the books.

Examples
input
13
output
17
input
4
output
4
Note
Note to the first test. The books get numbers 1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?13, which totals to 17 digits.

Note to the second sample. The books get numbers 1,?2,?3,?4, which totals to 4 digits.

【题目链接】:http://codeforces.com/contest/552/problem/B

【题解】

1..9都是1个数字+9
10..99都是2个数字+(99-10+1)*2
100..999都是3个数字+….
字符串表示的数字为x,可以在字符串后面不断加数字9;组成的数字为y;然后记录当前加了几个9->len
则答案递增len*(y-x);
重复上述过程直到x<=数字n,且y>n;
然后剩下的数字都是和n的位数一样的.
则再递增(n-x)*len;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);

int n;
LL ans = 0;
string now;

LL cl(string x)
{
    int len = x.size();
    LL now = 0;
    rep1(i,0,len-1)
        now = now*10+x[i]-'0';
    return now;
}

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    rei(n);
    now = "0";
    int len = 1;
    LL pre = 0;
    while (1)
    {
        LL key = cl(now+"9");
        if (key <= n)
        {
            ans+=(key-pre)*len;
            len++;
            now+="9";
        }
        else
            break;
        pre = key;
    }
    if (pre==n)
        cout << ans <<endl;
    else
    {
       // cout << ans <<endl;
       // cout << len<<endl;
       // cout << pre<<endl;
        ans+=len*(n-pre);
        cout << ans<<endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/7626831.html

内容概要:本文详细介绍了“秒杀商城”微服务架构的设计与实战全过程,涵盖系统从需求分析、服务拆分、技术选型到核心功能开发、分布式事务处理、容器化部署及监控链路追踪的完整流程。重点解决了高并发场景下的超卖问题,采用Redis预减库存、消息队列削峰、数据库乐观锁等手段保障数据一致性,并通过Nacos实现服务注册发现与配置管理,利用Seata处理跨服务分布式事务,结合RabbitMQ实现异步下单,提升系统吞吐能力。同时,项目支持Docker Compose快速部署和Kubernetes生产级编排,集成Sleuth+Zipkin链路追踪与Prometheus+Grafana监控体系,构建可观测性强的微服务系统。; 适合人群:具备Java基础和Spring Boot开发经验,熟悉微服务基本概念的中高级研发人员,尤其是希望深入理解高并发系统设计、分布式事务、服务治理等核心技术的开发者;适合工作2-5年、有志于转型微服务或提升架构能力的工程师; 使用场景及目标:①学习如何基于Spring Cloud Alibaba构建完整的微服务项目;②掌握秒杀场景下高并发、超卖控制、异步化、削峰填谷等关键技术方案;③实践分布式事务(Seata)、服务熔断降级、链路追踪、统一配置中心等企业级中间件的应用;④完成从本地开发到容器化部署的全流程落地; 阅读建议:建议按照文档提供的七个阶段循序渐进地动手实践,重点关注秒杀流程设计、服务间通信机制、分布式事务实现和系统性能优化部分,结合代码调试与监控工具深入理解各组件协作原理,真正掌握高并发微服务系统的构建能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值