L1-017 到底有多二

L1-017 到底有多二

一个整数“犯二的程度”定义为该数字中包含2的个数与其位数的比值。如果这个数是负数,则程度增加0.5倍;如果还是个偶数,则再增加1倍。例如数字-13142223336是个11位数,其中有3个2,并且是负数,也是偶数,则它的犯二程度计算为:3/11×1.5×2×100%,约为81.82%。本题就请你计算一个给定整数到底有多二。

输入格式:
输入第一行给出一个不超过50位的整数N。

输出格式:
在一行中输出N犯二的程度,保留小数点后两位。

输入样例:
-13142223336
输出样例:
81.82%


#include<stdio.h>

int main() {

	char number[52]={'\0'};//一个50位的负数要用掉51个字符再加上后面的\0
	int manyTwo=0,tmp;
	float numMultiple=1.0;//正数程度倍数
	gets(number);
	
	for(tmp=0;number[tmp]!='\0';tmp++)
	{
		if(number[tmp]=='2')
			manyTwo++;
	}
	int count=tmp;

	if(number[0]=='-')
	{
		numMultiple+=0.5;
		count=tmp-1;
	}
	if((number[tmp-1]-'0')%2==0)
		numMultiple*=2;
	printf("%.2f%%",(float)manyTwo/(float)count*100*numMultiple);

}

 
#include <stdio.h>

int main() {
    char ch = (char)getchar();
    int negative = 0, even = 0, twos = 0, length = 0;
    if (ch == '-'){
        negative = 1;
        ch = (char)getchar();
    }
    while (ch != '\n') {
        if ((ch - '0') % 2 == 0) {
            even = 1;
            if ((ch - '0') == 2)
                twos++;
        } else
            even = 0;
        length++;
        ch = (char)getchar();
    }
    printf("%.2f%%\n",(float)twos/(float)length*(negative?1.5:1)*(even?2:1)*100);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值