11
法一:遞歸法:
#include <stdio.h>void fun(int N){ if(N) { fun(N/8); PRintf("%d",N%8); }}int main(){ int N; while(scanf("%d",&N)!=EOF) { if(N==0) //注意N為0時輸出其自身,同非遞歸法 printf("0"); else fun(N); printf("/n"); } return 0;}法二:非遞歸法:#include <stdio.h>#define maxn 101void fun(int N){ int i,j=0; int a[maxn]; if(N==0) printf("0"); else { while(N) { a[j++]=N%8; N/=8; } for(i=j-1;i>=0;i--) printf("%d",a[i]); } printf("/n");}int main(){ int N; while(scanf("%d",&N)!=EOF) fun(N); return 0;}程序截圖:
2. (2008年北京大學圖形實驗室計算機研究生機試真題)首字母大寫題目描述:對一個字符串中的所有單詞,如果單詞的首字母不是大寫字母,則把單詞的首字母變成大寫字母。在字符串中,單詞之間通過空白符分隔,空白符包括:空格(' ')、制表符('/t')、回車符('/r')、換行符('/n')。輸入:輸入一行:待處理的字符串(長度小于100)。輸出:可能有多組測試數(shù)據(jù),對于每組數(shù)據(jù),輸出一行:轉(zhuǎn)換后的字符串。樣例輸入:if so, you already have a google account. you can sign in on the right.樣例輸出:If So, You Already Have A Google Account. You Can Sign In On The Right.
源代碼:
#include <stdio.h>#include <string.h>#define maxlen 105int main(){ char str[maxlen]; int i; while(gets(str)!=NULL) { if(str[0]>='a' && str[0]<='z') str[0]-=32; for(i=0;i<strlen(str);i++) //考慮第一個單詞前有非字母的情況 { if(str[i]==' ' || str[i]=='/t' || str[i]=='/r' || str[i]=='/n') { if(str[i+1]>='a' && str[i+1]<='z') str[i+1]-=32; } } puts(str); } return 0;}程序截圖:
3. (2008年華中科技大學計算機研究生機試真題)最長&最短文本題目描述: 輸入多行字符串,請按照原文本中的順序輸出其中最短和最長的字符串,如果最短和最長的字符串不止一個,請全部輸出。輸入:輸入包括多行字符串,字符串的長度len,(1<=len<=1000)。輸出:按照原文本中的順序輸出其中最短和最長的字符串,如果最短和最長的字符串不止一個,請全部輸出。樣例輸入:helloshesorryhe樣例輸出:hehellosorry
源代碼:
#include <stdio.h>#include <string.h>#define maxlen 1001int main(){ char str[maxlen][maxlen]; //二維字符串 同時保存字符串序號及串內(nèi)容 int len[maxlen]; int i,j,max,min; i=max=min=0; while(scanf("%s",str[i])!=EOF) //注意退出輸入時按兩次Ctrl+Z { len[i]=strlen(str[i]); if(len[i]>len[max]) max=i; if(len[i]<len[min]) min=i; i++; } for(j=0;j<i;j++) { if(len[j]==len[min]) printf("%s/n",str[j]); } for(j=0;j<i;j++) { if(len[j]==len[max]) printf("%s/n",str[j]); } return 0;}程序截圖:
新聞熱點
疑難解答