1 条题解
-
0
C :
#include<stdio.h> #include<string.h> int e[]={1,12,14,16,19,31,32,39}; char s[]="HCNOFPSK"; int main() { int n,i,j,sum,c,k,t; char a[100]; scanf("%d%*c",&n); while(n--) { gets(a); sum=0; for(i=0;i<strlen(a);i++) { k=0; t=i; for(j=0;j<8;j++) { if(a[i]==s[j]) c=e[j]; } while(a[i]>='0'&&a[i]<='9') { k=k*10+a[i]-'0'; i++; } i=t; if(!k) sum+=c; else sum+=c*(k-1); } printf("%d\n",sum); } return 0; }
Java :
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = Integer.parseInt(in.nextLine()); Map<Character, Integer> num = new HashMap<Character, Integer>(); num.put('H', 1); num.put('C', 12); num.put('N', 14); num.put('O', 16); num.put('F', 19); num.put('P', 31); num.put('S', 32); num.put('K', 39); while(n-->0){ String str = in.nextLine(); char tem = 0; int out = 0; for(int i=0;i<str.length();i++){ char c = str.charAt(i); if(c>'0' && c<='9'){ if(tem != 0)out += num.get(tem)*(c-'1'); } else{ if(tem != 0)out += num.get(tem); tem = c; } } if(tem != 0)out += num.get(tem); System.out.println(out); } } }
Python :
# coding=utf-8 def zl(x): if x=="H": return 1 elif x=="C": return 12 elif x=="N": return 14 elif x=="O": return 16 elif x=="F": return 19 elif x=="P": return 31 elif x=="S": return 32 elif x=="K": return 39 a=int(input()) for i in range(a): b=input() j=0 n=0 y=0 while j!=len(b): if j==len(b)-1: if b[j]<"1" or b[j]>"9": n=n+zl(b[j]) elif b[j]>="A" and b[j]<="Z" and (b[j+1]<"1" or b[j+1]>"9"): n=n+zl(b[j]) elif b[j]>="A" and b[j]<="Z" and b[j+1]>="1" and b[j+1]<="9": y=int(b[j+1])#此处变量y不能省略,int(j+1)不能直接带入下一行计算公式 n=n+zl(b[j])*y j+=1 print(n)
- 1
信息
- ID
- 1455
- 时间
- 1000ms
- 内存
- 32MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者