PTAL1-017 到底有多二 (15分)

思路:

  •  就是按照题目的意思,进行分析。先判断是不是负数,是不是偶数,有多少个2,依次处理倍数。
  • 有值得注意的地方是,N是不超过50位的,所以正常的整数以及long long 型也是不满足的,所以要用字符数组或者字符串来处理,与整型差不多~

具体看代码~~

#include<iostream>
#include<iomanip>
#include<string>
#include<algorithm>
using namespace std;
char str[55];//用于存取一个数
int main()
{
	int cnt1 = 0, cnt2 = 0;
	double res = 1;
	cin >> str;
	if (str[0] == '-')//如果这个数是负数
	{
		res = res * 1.5;
		cnt1 = strlen(str) - 1;
	}
	else
	{
		cnt1 = strlen(str);
	}
	if ((str[strlen(str) - 1]) % 2 == 0)
	{
		res = res * 2;//偶数的情况
	}
	for (int i = 0; i < strlen(str); i++)//计数2的个数
	{
		if (str[i] == '2')
			cnt2++;
	}
	res = (double)res*cnt2 / cnt1*100;
	cout << fixed << setprecision(2) << res<<"%";
	system("pause");
	return 0;
}

求点赞~~

猜你喜欢

转载自blog.csdn.net/weixin_43535668/article/details/104403170