題意:
給兩個名字算出分數,算分方法為
1.A-Z,a-z分別是1-26分,可能會出現其他字元可是不用算分
2.加出來分數每次都讓他每位數相加直到變成個位數為止
ex xyz 是 24+25+26 = 75 = (7+5) = 12 = (1+2) = 3
然後讓分數小除以分數大的算百分比即可
解法:
簡單的字串處理
程式碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX(x,y)(((x)>(y))?(x):(y))
#define MIN(x,y)(((x)<(y))?(x):(y))
int num[256];
char a[321];
char b[321];
int ga,gb;
int grade(int ga){
int tmp;
while(ga>9){
tmp = ga;
ga = 0;
while(tmp>0){
ga += tmp%10;
tmp /= 10;
}
}
return ga;
}
int main(){
int i;
for(i='A';i<='Z';++i)num[i]=i-'A'+1;
for(i='a';i<='z';++i)num[i]=i-'a'+1;
while(gets(a)){
gets(b);
//puts(a);
//puts(b);
for(i=ga=0;a[i]!='\0';++i)ga+=num[a[i]];
for(i=gb=0;b[i]!='\0';++i)gb+=num[b[i]];
ga = grade(ga);
gb = grade(gb);
printf("%.2lf %%\n",(double)(MIN(ga,gb)*100)/(double)(MAX(ga,gb)));
}
return 0;
}
留言列表