#include <stdio.h>int main(void){??? char c;??? int i,n;?scanf("%d", &n);?for(i=0;i<n;i+=1)?{???scanf("%c", &c);??????? if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))??{???if(c >= '0' && c <= '9')????printf("%c\n",c-'0');????? else if(c >= 'a' && c <= 'f')??????????????? printf("%c\n",c-'a'+10);???else if(c >= 'A' && c <= 'F')????printf("%c\n",c-'A'+10);?? }??else???printf("%d\n",c);?}?return 0;}
1 回答
已采納
onemoo
TA貢獻883條經驗 獲得超454個贊
問題出在 scanf("%c", &c) 這一句。準確地說,出在第二次執行這句代碼的時候。
你輸入之后一般會回車吧。這個換行符也會留在輸入緩沖區中,而 %c 會匹配輸入流中的任何一個字符,所以就會讀到上次剩下的換行符'\n'。
解決辦法是在格式化字符串中的 %c 前加一個空格,比如 scanf(" %c...
具體可以看我在另一個問題下的詳細回答?http://www.xianlaiwan.cn/wenda/detail/381183
- 1 回答
- 0 關注
- 1116 瀏覽
添加回答
舉報
0/150
提交
取消
