Panasonic Programming Contest (AtCoder Beginner Contest 195)-------C(思维)

C - Comma Editorial / 


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 300300 points

Problem Statement

When Takahashi writes an integer, he uses a comma every third digit from the right. For example, 12345671234567 is written as 1,234,567, and 777777 is written as 777.

How many commas will be used in total when he writes each integer from 11 through NN once?

Constraints

  • 1≤N≤10151≤N≤1015
  • NN is an integer.

Input

Input is given from Standard Input in the following format:

NN

Output

Print the total number of commas.


Sample Input 1 Copy

Copy

1010

Sample Output 1 Copy

Copy

11

No comma is used in writing 999999 or smaller numbers. One comma is used in writing each of the numbers from 10001000 through 10101010.

Thus, 1111 commas are used in total.


Sample Input 2 Copy

Copy

27182818284590

Sample Output 2 Copy

Copy

107730272137364

 

思路:

暴力枚举超时

优化:

枚举1000的次方数i

如果超过就 n  - pow(1000,i)+  1  就可以了

想一想为什么?

 

官方题解:

We count the number for each position of comma.

The comma between the third- and fourth- least significant digit is written once for every number more than or equal to 1000. Therefore, the number of such commas is N−999 if N≥1000, otherwise 0.

Similarly, the comma between the sixth- and seventh- least significant digit is written once for every number more than equal to 1000000. Therefore, the number of such commas is N−999999if N≥1000000, otherwise 0.

The overall number of commas can be counted similarly.

我的code

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define N 100003
#define mod 100000
#define pb push_back
#define x first
#define y second
#define ull unsigned long long
#define ll long long
using namespace std;
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
	ll n ;
	cin >> n;
	ll res = 0;
	for(int i = 1;;i ++)
	{
		//		1,000,000
		//		1,000
		//		10
		if(n >= pow(1000,i) && n < pow(1000,i+1))
		res += (n - pow(1000,i) + 1) * i;
		else if(n >= pow(1000,i) && n >= pow(1000,i+1))
		res += (pow(1000,i + 1) - 1 - pow(1000,i) + 1) * i;
		else
		break;
	}
	cout << res << endl;
    return 0;	
}


官方解答的code

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define N 100003
#define mod 100000
#define pb push_back
#define x first
#define y second
#define ull unsigned long long
#define ll long long
using namespace std;
int a[N];
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
	ll n ;
	cin >> n;
	ll res = 0;
	for(int i = 1;;i ++)
	{
		if(n >= pow(1000,i))
		res += n - pow(1000,i) + 1;
		else
		break;
	}
	cout << res << endl;
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值