机试根据测试用例的通过个数来给分,一共3道题:
| 分数 | 评分细则 | |
| 第一题 | 60 | 20分/用例 * 3个用例 |
| 第二题 | 100 | 20分/用例 * 5个用例 |
| 第三题 | 160 | 20分/用例 * 8个用例 |
三道题的大意如下:
1、输入一个大于10的整数,如 n ( n>=2 ) 位数,输出该数的低 n-1 位。
输入:4321
输出:321
2、以1990年1月1日为参考点,根据“三天打渔两天晒网”的规则,判断任意给定的日期是处于打渔(Fishing)还是晒网(Sleeping)的状态。
输入1990 1 4
输出:Sleeping
3、判断目标串与模式串的匹配,包括通配符(?-单个任意字符,*-若干任意字符)。若匹配,返回目标串与模式串第一次匹配时,目标串首个字符对应模式串的位置;否则返回-1。
输入: ?^_^*
ass^_^fds
输出:2
代码如下:
1 //Test1: Output the lower n-1 digits of the n-digit input integer where n>=2 2 3 #include<iostream> 4 #include<windows.h> 5 #include<string> 6 7 using namespace std; 8 9 int main() 10 { 11 string num; 12 13 cin>>num; 14 15 for( int i=1; i<num.length(); i++ ) 16 cout<<num[i]; 17 18 system("pause"); 19 }
1 //Test2: Fishing or Sleeping 2 3 #include<iostream> 4 #include<windows.h> 5 using namespace std; 6 7 bool isLeap( int year ) 8 { 9 if( year%100==0 ||( year%4 && year%400!=0 ) ) 10 return true; 11 12 return false; 13 } 14 15 int main() 16 { 17 int year, month, day; 18 19 cin>>year>>month>>day; 20 21 int dayOfMonth[12] = {31, 28, 31,30,31, 22 30,31,31,30,31,31,30 }; 23 24 int i; 25 int numOfDays = 0; 26 for( i = 1990; i<year; i++ ) 27 if( isLeap(i) == true ) 28 numOfDays += 366; 29 else 30 numOfDays += 365; 31 32 if(isLeap(year) == true) 33 dayOfMonth[1] = 29; 34 35 for( i = 0; i < month-1; i++ ) 36 numOfDays += dayOfMonth[i]; 37 38 numOfDays += day; 39 40 int temp = numOfDays%5; 41 42 if( temp <3 ) 43 cout<<"Fishing"; 44 else 45 cout<<"Sleeping"; 46 47 system("pause"); 48 49 }
1 //Test3: pattern match, wilecards ? and * included 2 3 #include<iostream> 4 #include<string> 5 #include<windows.h> 6 7 using namespace std; 8 9 10 int main() 11 { 12 string target; 13 string pattern; 14 15 cin>>target>>pattern; 16 17 18 int i; 19 int j; 20 21 int a; 22 int b; 23 int match = -1; 24 bool flag = false; 25 26 for( i = 0; i <= pattern.length()-target.length(); i++ ) 27 { 28 b = i; 29 30 for( j = 0; j<target.length(); j++ ) 31 if( pattern[b] == target[j] || target[j]=='?' ) 32 { 33 34 b++; 35 } 36 else if( target[j]=='*' ) 37 { 38 flag = true; 39 break; 40 } 41 else 42 break; 43 44 if( j == target.length() || flag==true ) 45 { 46 match = i; 47 break; 48 } 49 50 } 51 52 cout<<match; 53 54 system("pause"); 55 56 }
注意:
- 实际机试时,不允许使用第三方类库。上面代码使用windows.h头文件,只是为了调试方便。
- 要学会Visual Studio调试方法。
- 要注意边界条件。
- 以上代码可能有潜在问题,欢迎指正。
浙公网安备 33010602011771号