初试悲催折戟 不过某种意义上也不是太坏的事 反正我不急着工作 这一年多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; }
#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:打牌
//北邮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; }
//北邮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; }