2006---2009年杭电计算机历年研究生复试---笔试编程
生活随笔
收集整理的這篇文章主要介紹了
2006---2009年杭电计算机历年研究生复试---笔试编程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、輸入一個十進制的數,把它轉成八進制。類似的把十進制轉成16進制,把十六進制轉變為十進制等。
2、輸入兩個非常大的整數(完全超出了int、long的表示范圍),這個整數的長度可能超過100位,計算并輸出這兩個數相加的結果。(HDU acm 1002 用string處理比較好)
轉載請標明出處,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <stdio.h> #include <string.h>int main(void) {int temp, i, j, k, len1, len2, m, sum[1010];char a[1010], b[1010], c[1010];scanf("%s %s", a, b);len1 = strlen(a);len2 = strlen(b);if(len1 < len2) //交換兩個字符串,確保第一個字符串的長度不小于第二個字符串{strcpy(c, b);strcpy(b, a);strcpy(a, c);m = len2, len2 = len1, len1 = m;}k = m = 0;//從兩個數的低位開始,手工模擬加法,逐位相加for(i = len1 - 1, j = len2 - 1; i >= 0; --i, --j){if(j >= 0)temp = a[i] - '0' + b[j] - '0' + m;elsetemp = a[i] - '0' + m;sum[k++] = temp % 10;m = temp / 10; //進位}if(m > 0)printf("%d", m);for(i = k - 1; i >= 0; --i)printf("%d", sum[i]);printf("\n");return 0; }
3、“回文串”是一個正讀和反讀都一樣的字符串,比如“level”或者“noon”等等就是回文串。請寫一個程序判斷讀入的字符串是否是“回文”。
轉載請標明出處,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <stdio.h> #include <string.h>int main(void) {char str[100];int len, i, j;scanf("%s", &str);len = strlen(str);for(i = 0, j = len - 1; i < len && j >= i; ++i, --j){if(str[i] != str[j])break;}if(j < i)printf("這個字符串是回文串!\n");elseprintf("這個字符串不是回文串!\n");return 0; }
4、輸入n個數,按從小到大進行排序并輸出。
轉載請標明出處,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <iostream> #include <algorithm> using namespace std;int main(void) {int n, i;scanf("%d", &n);int *p = new int[n];for(i = 0; i < n; ++i)scanf("%d", &p[i]);sort(p, p + n);for(i = 0; i < n; ++i)printf("%d ", p[i]);printf("\n");return 0; }
5、輸入一個長整型的數,從低位起取出奇數位組成一個新的數輸出。
轉載請標明出處,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <stdio.h>int main(void) {long n, k, i, j, newnum;i = j = newnum = 0;scanf("%ld", &n);while(n){k = n % 10;for(j = 0; j < i; ++j)k *= 10;newnum += k;n /= 100;i++; //每位數字上數字的權值}printf("%ld\n", newnum);return 0; }
6、輸入n個字符串,將它們按字母由小到大的順序排列并輸出。
方法一:
轉載請標明出處,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <iostream> #include <string> #include <algorithm> using namespace std;bool cmp(const string& a, const string& b) {return a < b; }int main(void) {int i, n;cin>>n;string *p = new string[n];for(i = 0; i < n; ++i)cin>>p[i];sort(p, p + n, cmp);for(i = 0; i < n; ++i)cout<<p[i]<<endl;delete []p;return 0; } 方法二:
轉載請標明出處,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <iostream> #include <string> using namespace std;int main(void) {int n, i, j;string temp;cin>>n;string *pt = new string[n];for(i = 0; i < n; ++i)cin>>pt[i];for(i = 0; i < n - 1; ++i){for(j = 0; j < n - i - 1; ++j){if(pt[j] > pt[j + 1]){temp = pt[j];pt[j] = pt[j + 1];pt[j + 1] = temp;}}}for(i = 0; i < n; ++i)cout<<pt[i]<<endl;delete []pt;return 0; }
7、輸入兩個正整數,求出這兩個數的最大公約數。
轉載請標明出處,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <stdio.h>int gcd(int m, int n) {int r;while(r = m % n){m = n, n = r;}return n; }int main(void) {int m, n;scanf("%d %d", &m, &n);printf("最大公約數為:%d\n", gcd(m, n));return 0; }
8、“水仙花數”是指一個三位數,它的各位數字的立方和等于其本身,比如:153=1^3+5^3+3^3,輸入一個整數,判斷它是否是水仙花數。
轉載請標明出處,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <stdio.h>int main(void) {int n, a, b, c;scanf("%d", &n);a = n % 10;b = (n / 10) % 10;c = n / 100;if(n == a*a*a + b*b*b + c*c*c)printf("這個數是水仙花數!\n");elseprintf("這個數不是水仙花數!\n");return 0; }
9、完數的定義:如果一個大于1的正整數的所有因子之和等于它的本身,則稱這個數是完數,比如6,28都是完數:6=1+2+3;28=1+2+4+7+14。輸入一個整數,判斷它是否是完數。
轉載請標明出處,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <stdio.h>int main(void) {int i, m, n;scanf("%d", &n);for(i = 1, m = 0; i <= n/2; ++i){if(n % i == 0)m += i;}if(m == n)printf("這個數是完數!\n");elseprintf("這個數不是完數!\n");return 0; }
總結
以上是生活随笔為你收集整理的2006---2009年杭电计算机历年研究生复试---笔试编程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: atoi() 与 itoa()函数用法
- 下一篇: 2010年杭电计算机研究生复试---笔试