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

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

输入格式:

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

输出格式:

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

输入样例:

-13142223336

输出样例:

81.82%

我感觉这个题有毒,因为一直有一个测试点过不去所有总是拿14分。但百度了一下和通过代码的思路差不太多,只是他考虑了结果的四舍五入,然后我就也考虑了一下还是行不通,找了半天 果断放弃。以下是14分的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main()
{
    char n[100];
    scanf("%s",n);
    float bs=1;
    int x;
    float result;
    if(n[0]=='-')
    {
        bs=bs*1.5/1.0;
        char s;
        s=n[strlen(n)-1];
        if(s=='0'||s=='2'||s=='4'||s=='6'||s=='8')
        {
            bs=bs*2/1.0;
        }
    }
    int num=0;
    if(n[0]=='-')
    {
        for(int i=1; i<strlen(n)-1; i++)
        {
            if(n[i]=='2')
            {
                num++;
            }
        }
        result = 1.0*num*bs*100/(strlen(n)-1);
        x=(int)(result*100+0.5);
        printf("%.2f%%",x/100.0);
    }
    else
    {
        for(int i=0; i<strlen(n)-1; i++)
        {
            if(n[i]=='2')
            {
                num++;
            }
        }
        result = 1.0*num*bs*100/(strlen(n));
        int x=(int)(result*100+0.5);
        printf("%.2f%%",x/100.0);
    }
    return 0;
}


这里是15分的代码:

#include <stdio.h>
#include <stdlib.h>
 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
 
int main(int argc, char *argv[]) {
    char a[55],z=0,key=0;
    float bei = 1.0,result;
    int ou =1,x;
    scanf("%s",a);
    if(a[0]=='-'){
        bei=1.5;
        z=1;
    }
    for(;a[z]!='\0';z++){
        if(a[z]=='2'){
            key++;
        }
    }
    if(a[z-1]%2==0){
        ou=2;
    }
    if(bei==1.5){
        z-=1;
    }
    result = 1.0*key*bei*ou*100/z;
    x=(int)(result*100+0.5);
    printf("%.2f%%",x/100.0);
    return 0;
}
--------------------- 
作者:萌鼠喝酸奶 
来源:CSDN 
原文:https://blog.csdn.net/qq_28484355/article/details/51590233 
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值