Created
February 18, 2013 10:13
-
-
Save wfwei/4976396 to your computer and use it in GitHub Desktop.
一个特殊的国家忌讳7这个数字,所有包含7的数字他们都不用, 改用下一个数字,比如7他们用8代替,17用19代替。 给定这个国家的数字,如何编程翻译成我们用的数字。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <math.h> | |
#define MAX 100 | |
/** | |
一个特殊的国家忌讳7这个数字,所有包含7的数字他们都不用, | |
改用下一个数字,比如7他们用8代替,17用19代替。 | |
给定这个国家的数字,如何编程翻译成我们用的数字。 | |
**/ | |
double convert2dec(char *src, int fromBase) { | |
int len = strlen(src); | |
int i, tmp; double res=0; | |
for(i=len-1; i>=0; i--){ | |
tmp = src[i] - '0'; | |
if(tmp > 6){ | |
if(tmp == 7){ | |
printf("Error:input cant cantain digit 7"); | |
return -1; | |
} | |
tmp --; | |
} | |
res += tmp*pow(fromBase, (len-i-1)*1.0); | |
} | |
return res; | |
} | |
int main(){ | |
char a[MAX]; | |
while(scanf("%s", a)!=EOF){ | |
double res = convert2dec(a, 9); | |
printf("%s --> %lf\n", a, res); | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment