7-8到底有多二

it2022-05-05  163

import java.text.DecimalFormat; import java.util.Scanner; public class S7_08 { static String str; static int cnt; static int len; static double ci = 1; //倍数 static int ou = 1; public static void main(String[] args) { DecimalFormat df = new DecimalFormat("0.00"); Scanner sc = new Scanner(System.in); str = sc.nextLine(); if(str.charAt(0) == '-') { len = str.length() - 1 ; ci += 0.5; } else { len = str.length(); } if ((str.charAt(str.length() - 1) - '0') % 2 == 0) ou += 1; //是偶数 for (int i = 0; i < str.length(); i++) { if (str.charAt(i) - '0' == 2) { cnt++; } } System.out.println(df.format(cnt*1.0/len*ci*ou*100) + "%"); } } #include<iostream> #include<string> #include<iomanip> using namespace std; int main(){ int len,w; double s,count; string str; count=0; cin>>str; len=str.length(); w=len; for(int i=0;i<len;i++){ if(str[i]=='2'){ count++; } } if(str[0]=='-'){ w--; if(str[w]=='0'||str[w]=='2'||str[w]=='4'||str[w]=='6'||str[w]=='8'){ s=count/w*1.5*2; cout<<setprecision(2)<<fixed<<s*100<<'%'; return 0; } else{ s=count/w*1.5; cout<<setprecision(2)<<fixed<<s*100<<'%'; return 0; } } else if(str[0]=='+'){ w--; if(str[w]=='0'||str[w]=='2'||str[w]=='4'||str[w]=='6'||str[w]=='8'){ s=count/w*2; cout<<setprecision(2)<<fixed<<s*100<<'%'; return 0; } else{ s=count/w; cout<<setprecision(2)<<fixed<<s*100<<'%'; return 0; } } else{ if(str[len-1]=='0'||str[len-1]=='2'||str[len-1]=='4'||str[len-1]=='6'||str[len-1]=='8'){ s=count/w*2; cout<<setprecision(2)<<fixed<<s*100<<'%'; return 0; } else{ s=count/w; cout<<setprecision(2)<<fixed<<s*100<<'%'; return 0; } } return 0; }

最新回复(0)