1. 程式人生 > >L1-017. 到底有多二 C語言

L1-017. 到底有多二 C語言

一個整數“犯二的程度”定義為該數字中包含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>
#include<stdlib.h>
int main()
{
  char
num[51]={'\0'}; int i=0,length=0; double result=0,t=0; scanf("%s",num); while(num[i]!='\0') { if(num[i]=='2') { t++; } i++; } if(num[0]=='-') { length=i-1; if(num[length]=='2'||num[length]=='4'||num[length]=='6'||num[length]=='8'||num[length]=='0') { result=t/length*1
.5*2*100; } else result=t/length*1.5*100; } else { length=i; if(num[length-1]=='2'||num[length-1]=='4'||num[length-1]=='6'||num[length-1]=='8'||num[length-1]=='0') { result=t/length*2*100; } else result=t/length*100; } printf("%.2lf%%",result); }