PTA groups Programming Ladder Race - Practice in the end how two sets L1-017

L1-017 in the end how two

Questions asked

An integer " committed two degree " 2 ratio of the number of its digits is defined as the number contained. If this number is negative, the degree of increase of 0.5 times; if still even, then doubled. Such as digital -13142223336is 11 bits, 2 of which there are three, and is negative, is an even number, it is made of two degree calculation: 3/11 × 1.5 × 2 × 100%, of about 81.82 percent. This question will ask you to calculate how much a given integer in the end two.
Input format:
input of the first line of a given integer of not more than 50 bits N.
Output formats:
Output in line Nextent guilty of two, to two decimal places.
Sample input:

-13142223336

Sample output:

81.82%

code show as below:

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
	string str;
	cin>>str;
	double res=1.0;
	int num=0;
	for(int i=0;i<str.size();++i)
	{
		if(str[i]=='2')
			num++;
	}
	if(str[0]=='-')
	{
		res*=1.5;
		if(str[str.size()-1]%2==0)
			res*=2;
		res=res*num*100/(str.size()-1);
	}
	else
	{
		if(str[str.size()-1]%2==0)
			res*=2;
		res=res*num*100/str.size();
	}
	cout<<setiosflags(ios::fixed)<<setprecision(2)<<res<<"%"<<endl;	
	return 0;
}
Published 82 original articles · won praise 12 · views 9983

Guess you like

Origin blog.csdn.net/Slatter/article/details/103965837