p1590失踪的7更新

#在这里插入图片描述

原始思路:三组数据没过:把含7的减去

#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
	ll n, t;
	cin >> t;
	while (t)
	{
		t--;
		cin >> n;
		ll cnt = 0;
		for (ll j = 1; j <= n; j++)
		{
			ll i = j;
			if (i % 10 == 7)
			{
				cnt++;
				//cout << j << endl;
			}
			else
			{
			while (i)
			{

				//i = i / 10;每次while循环都会/10,只有第一次判断了i % 10 == 7,后边的都没判断;相当于i/100
				if (i % 10 == 7)
				{
					cnt++;
					//cout << j << endl;
					break;
				}
				else i = i / 10;
			}
			}

		}
		cout << n - cnt << endl;
	}

	return 0;
}
//i = i / 10;每次while循环都会/10,只有第一次判断了i % 10 == 7,后边的都没判断;相当于i/100

更新:AC:权位方法

思想:每位相互独立,权重是可能出现的个数对应的方次。把每一位不含7的方案数算出来,因为每一位是相互独立的,所以这些方案数加起来就是答案
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
	int t;
	cin >> t;
	
	while (t--)
	{
		ll n , cnt = 0;
		cin >> n;
		ll i, temp;
		for (i = 1; n; n /= 10, i *= 9)
		{
			temp = n % 10;
			if (temp >= 7)temp--;
			cnt += temp * i;
		}
		cout << cnt << endl;
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值