zzuli oj 1134 字符串轉換
阿新 • • 發佈:2017-08-19
!= nbsp output pan include style 結果 rip time
題目鏈接:
https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1134
Description
輸入一個以回車結束的字符串,它由數字和字母組成,請過濾掉所有非數字字符,然後將數字字符串轉換成十進制整數後乘以2輸出。
Input
輸入一個以回車結束的字符串,長度不超過100,由數字和字母組成。
Output
將轉換後的整數乘以2輸出,測試數據保證結果在int範圍內。
Sample Input
sg987aa65t498Sample Output
197530996 AC代碼:1 #include<stdio.h> 2#include<string.h> 3 #include<ctype.h> 4 void str_rev(char s[],int l); 5 int main() 6 { 7 char b[101],a[101]; 8 int l,i,j,c[105],f; 9 while(scanf("%s",a) != EOF) 10 { 11 l=strlen(a); 12 for(j=0,i=0;i<l;i++) 13 { 14 if(isdigit(a[i]))15 b[j++]=a[i]; 16 } 17 b[j]=‘\0‘;//字符處理的結尾要加結束符號 18 str_rev(b,j); 19 memset(c,0,sizeof(c)); 20 for(i=0;i<j;i++) 21 { 22 if(isdigit(b[i])) 23 c[i]=(b[i]-‘0‘) * 2; 24 } 25 for(i=0;i<j;i++) 26 {27 if(c[i]>9) 28 { 29 c[i] %= 10; 30 c[i+1]++; 31 } 32 } 33 for(f=0,i=101;i>=0;i--) 34 { 35 if(c[i]||f==1) 36 { 37 printf("%d",c[i]); 38 f=1; 39 } 40 } 41 if(f==0) 42 printf("0");//特殊情況 43 printf("\n"); 44 } 45 return 0; 46 } 47 void str_rev(char s[],int l)//更換長度 48 { 49 int i; 50 char t; 51 for(i=0;i<=(l-1)/2;i++) 52 { 53 t=s[i]; 54 s[i]=s[l-i-1]; 55 s[l-i-1]=t; 56 } 57 s[l]=‘\0‘; 58 }
zzuli oj 1134 字符串轉換