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

求点赞~~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值