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;
}

猜你喜欢

转载自blog.csdn.net/helloworld0529/article/details/102925187
今日推荐