初試悲催折戟 不過某種意義上也不是太壞的事 反正我不急着工作 這一年多A些機試題 對於我這個轉專業到CS的編程經驗極度匱乏的人來說也不乏裨益
這幾天爲了找專業課的上課地點 疲於奔命 週末難得清閒 先A了一套北郵的題 朋友赫陽今年初試分數很高 祝福他拿下北郵吧!也希望這些解題報告能對他有微薄的幫助
九度OJ
題目1173:查找
題目描述:
輸入數組長度 n
輸入數組 a[1...n]
輸入查找個數m
輸入查找數字b[1...m]
輸出 YES or NO 查找有則YES 否則NO 。
輸入:
輸入有多組數據。
每組輸入n,然後輸入n個整數,再輸入m,然後再輸入m個整數(1<=m<=n<=100)。
輸出:
如果在n個數組中輸出YES否則輸出NO。
樣例輸入:
5
1 5 2 4 3
3
2 5 6
樣例輸出:
YES
YES
NO
//北郵2010網院:題目1173:查找
#include <fstream>
#include <memory.h>
#include <algorithm>
#include <iostream>
using namespace std;
int a[100], b[100];
int searchA( int x, int low, int high, int a[], bool &found ){
int mid;
found = 0;
while( low <= high ){
mid = (low+high)/2;
if( x == a[mid] ){
found = 1;
return mid;
}
else if( x < a[mid] )
high = mid - 1;
else low = mid + 1;
}
return -1;
};
int main()
{
int i, j, k, n, m;
int remain, result;
bool found;
ifstream cin("BUPT_1173.txt");//
while( cin >> n ){
for( i=0; i<n; i++ )
cin >> a[i];
cin >> m;
for( i=0; i<m; i++ )
cin >> b[i];
sort(a,a+n);
for( i=0; i<m; i++ ){
result = searchA( b[i], 0, n-1, a, found );
//cout << "i=" << i << " mid=" << result << " " << found << endl;
if( result == -1 )
cout << "NO\n";
else cout << "YES\n";
}
}
system("pause");
return 0;
}
九度OJ 題目1174:查找第K小數
題目描述:
查找一個數組的第K小的數,注意同樣大小算一樣大。
如 2 1 3 4 5 2 第三小數爲3。
輸入:
輸入有多組數據。
每組輸入n,然後輸入n個整數(1<=n<=1000),再輸入k。
輸出:
輸出第k小的整數。
樣例輸入:
6
2 1 3 5 2 2
3
樣例輸出:
3
#include <fstream>
#include <memory.h>
#include <algorithm>
#include <iostream>
using namespace std;
int a[1000], b[1000];
int main()
{
int i, j, k, n, m;
int remain, result;
bool found;
ifstream cin("BUPT_1174.txt");//
while( cin >> n ){
for( i=0; i<n; i++ )
cin >> a[i];
cin >> k;
sort(a,a+n);
b[0] = a[0];
for( i=1,j=0; i<n; i++ )
if( a[i] != b[j] ){
j++;
b[j] = a[i];
}
//for( i=0; i<=j; i++ )
// cout << b[i] << " ";
cout << b[k-1] << endl;
}
system("pause");
return 0;
}
九度OJ 題目1175:打牌
題目描述:
牌只有1到9,手裏拿着已經排好序的牌a,對方出牌b,用程序判斷手中牌是否能夠壓過對方出牌。
規則:出牌牌型有5種
[1]一張 如4 則5...9可壓過
[2]兩張 如44 則55,66,77,...,99可壓過
[3]三張 如444 規則如[2]
[4]四張 如4444 規則如[2]
[5]五張 牌型只有12345 23456 34567 45678 56789五個,後面的比前面的均大。
輸入:
輸入有多組數據。
每組輸入兩個字符串(字符串大小不超過100)a,b。a字符串代表手中牌,b字符串代表處的牌。
輸出:
壓過輸出YES 否則NO。
樣例輸入:
12233445566677
33
樣例輸出:
YES
//北郵2010網院:題目1175:打牌
#include <fstream>
#include <memory.h>
#include <algorithm>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
int a[1000], b[1000];
int card[10]; //card[0]捨棄
int main()
{
int i, j, k, n, m;
string s, t;
int slen, tlen;
ifstream cin("BUPT_1175.txt");//
while( cin >> s ){
memset(card,0,sizeof(card));
slen = s.length();
for( i=0; i<slen; i++ )
card[s[i]-48]++;
//for( i=1; i<=9; i++ )//
// cout << card[i] << " ";//
cin >> t;
tlen = t.length();
bool sucess = 0;
k = t[0]-48;
if( tlen != 5 ){ //前四種出牌模式
for( i=k+1; i<=9; i++ )
if( card[i]>=tlen ){
cout << "YES" << endl;
sucess = 1;
break;
}
if( sucess == 0 )
cout << "NO" << endl;
}
else{ //出牌模式爲長線
int count = 1;
if( k == 5 ){
cout << "NO" << endl;
//continue;
}
else{
for( i=k+1; i<=5; i++ ){ //5是最後一個線的起點
for( j=i; j<=i+4; j++ )
count *= card[j];
if( count != 0 ){
cout << "YES" << endl;
sucess = 1;
break;
}
}
if( sucess == 0 )
cout << "NO" << endl;
}// else
}
}
system("pause");
return 0;
}
九度OJ 題目1176:樹查找
時間限制:1 秒
內存限制:32 兆
特殊判題:否
提交:215
解決:85
題目描述:
有一棵樹,輸出某一深度的所有節點,有則輸出這些節點,無則輸出EMPTY。該樹是完全二叉樹。
輸入:
輸入有多組數據。
每組輸入一個n(1<=n<=1000),然後將樹中的這n個節點依次輸入,再輸入一個d代表深度。
輸出:
輸出該樹中第d層得所有節點,節點間用空格隔開,最後一個節點後沒有空格。
樣例輸入:
4
1 2 3 4
2
樣例輸出:
2 3
//北郵2010網院:題目1176:樹查找
//(1<=n<=1000)
#include <fstream>
#include <iostream>
using namespace std;
int a[1001], b[50]; //b[0]捨棄 b[i]存儲第i行首元素下標 b[11]爲虛構行首
int main()
{
int i, j, k, n, m, d;
//ifstream cin("BUPT_1176.txt");//
while( cin >> n ){
for( i=1; i<=n; i++ ){ //捨棄a[0]
//cin >> a[i];
a[i] = i;
}
cin >> d;
if( d > 10 ){ //1000個元素是10行
cout << "EMPTY\n";
continue;
}
b[1] = 1;
for( i=2; i<=11; i++ ){
b[i] = b[i-1] * 2;
//cout << b[i] << endl; //b[11]=1024
}
if( n >= b[d] ){
if( n < b[d+1] ){
for( i=b[d]; i<=n-1; i++ )
cout << a[i] << " ";
cout << a[n] << endl;
}
else{
for( i=b[d]; i<=b[d+1]-2; i++ )
cout << a[i] << " ";
cout << a[b[d+1]-1] << endl;
}
}
else cout << "EMPTY\n";
}
system("pause");
return 0;
}