2017美团实习生的题

  • 统计位数

统计n以内的正整数一共多少位数字,不统计前导零

例如:n为13时,12345678910111213,共17位,则输出17

输入:

2 数据个数

13 数据n(1 <= n <= 10^9)

4

输出:

17

4

#include <iostream>
#include<vector>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;

int main(){
	int T;
	cin>>T;
	while (T--) {
		long long ans = 0;
		long long  n;  //不用long long 会出错
		cin>>n;
		int cache = n;
		int a = 1;
		int weishu = 1;
		while (cache/10 != 0) {   
			cache /= 10;    //压缩循环
			ans += a * 9 * weishu;
			a *= 10;
			weishu ++;
		} 
		ans += (n - a +1 ) * weishu;
		printf("%lld\n", ans);
	}
	return 0;
}

数据小的情况下可以把输入的数据当做字符,直接统计字符串长度

#include <iostream>
#include<vector>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;

int main()
{
	int n,m;
	int len;
	cin>>n;
	while(n)
	{
		cin>>m;
		string s;
		for(int i=1;i<=m;i++)
		{
			s=s+to_string(i);
		}
		len=s.length();
		cout<<len<<endl;
		n--;
	}

  system("pause");
  return 0;
}

  • 经典的gcd(最大公约数

给你A数组,询问1 <= i <= n , 1 <= j<= m

其中, ,输入四个整数,N(数组长度),n, m ,p 数量级为10^5

输入:

样例110 1 2 10

样例210 2 2 10

输出

20

        33

样例2 解释:数组A:10 3 6 9 2 5 8 1 4 7A[gcd(1,1)]+A[gcd[1,2]]+Agcd[2,1]+A[gcd[2,2]] = A[1]+A[1]+A[1]+A[2]=33

#include<cstdio>
int gcd(int a,int b) {
	return b==0?a:gcd(b,a%b);
}

int main(){
	int N, n, m, p;
	while (scanf("%d%d%d%d", &N, &n, &m, &p) == 4) {
		int a[N+1];
		a[1] = p;
		for (int i=2; i<=N; i++) a[i] = (a[i-1] + 153) % p;
		long long ans = 0;
		for (int i=1; i<=n; i++) {
			for (int j=1; j<=m; j++) {
				ans += a[ gcd(i, j) ];
			}
		}
		printf("%lld", ans);
	}
	return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值