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

求點贊~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章