1. title (a)
Code:
#include <iostream> #include <iomanip> #include <math.h> #include <string.h> using namespace std; int main () { int w[17] = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 }; char check[] = { '1','0','x','9','8','7','6','5','4','3','2' }; char s[18] = {'0'}; cout << "Enter the ID number:" << endl; cin >> s; if( strlen(s)!=18 ){ cout << "Enter error ID number" << endl; } int b [18] = {0}; // input ID number is converted to an int for(int i=0; i<18; i++){ b [i] = s [i] - '0'; // character to type int } int sum = 0; // sum before 17 for(int i=0; i<17; i++){ sum += b[i]*w[i]; } // find x, y int x = sum%11; int y = check[x] - '0'; if( y==b[17] ){ cout << s << ":正确" << endl; }else{ for(int i=0; i<17; i++){ cout << s[i] <<""; } if(check[x]=='x') cout << 'x' << ":错误" << endl; else cout << y << ":错误" << endl; } return 0; }
2. The title (b)
Code:
#include <iostream> #include <iomanip> #include <math.h> #include <string.h> using namespace std; int count = 0; int half_find(int a[], int l, int r, int find){ /* int mid = l + (r-l)/2; if (l == r) return -1; // not found if (find == a [mid]) return mid; // exactly the midpoint if( find>a[mid] ) return half_find(a, mid+1, r, find); if( find<a[mid] ) return half_find(a, 0, mid, find); */ // non-recursive int left = l; int right = r; int mid; while( left<=right ){ count++; mid = left + (right-left)/2; if( find==a[mid] ){ return mid; }else if( find>a[mid] ){ left = mid + 1; }else{ right = mid - 1; } } /* while( left<right ){ count++; mid = left + (right-left)/2; if( find==a[mid] ){ return mid; }else if( find>a[mid] ){ left = mid + 1; }else{ right = mid; } } */ return -1; // not found } int halffind(int a[], int len, int find){ return half_find(a, 0, len-1, find); } int main () { int a[10] = {-36, -25, 0, 12, 14, 29, 35, 47, 76, 100}; int find; cout << "Find numbers:" << endl; cin >> find; int index = halffind(a, 10, find); if( -1==index ) cout << "not found" << endl; else cout << find << "are" << index + 1 << "number, as the number of lookups" << count << endl; return 0; }
3. questions (c)
Code:
#include<iostream> using namespace std ; // definition of the structure typedef struct { char name[20]; float score1; float score2; float score3; int total; }Student; //交换 void swap1(Student s[], int i, int j){ Student temp = s[i]; s[i] = s[j]; s[j] = temp; } void find_fail(Student s[],int n) { //输出不及格的人数 for (int i = 0; i < n; i++) { if (s[i].score1 < 60 || s[i].score2 < 60 || s[i].score3 < 60) cout << "*[" << s[i].name << "] " << s[i].score1 << " " << s[i].score2 << " " << s[i].score3 << endl ; } cout << endl ; } //按照平均成绩的高低输出 void printByAverage(Student s[],int n) { //按照基本的冒泡排序法按照成绩排序 for (int i = 0; i < n - 1; i++){ for (int j = 0; j < n-1-i; j++){ if(s[j].total>s[j+1].total)//逆序 swap1(s,j,j+1); } } //输出 for(int k = n-1; k >=0; k--) { cout << s[k].name << " "; cout << s[k].score1 << " "; cout << s[k].score2 << " "; cout << s[k].score3 << endl; } } int main() { cout << "输入学生的人数:"; int n = 0; cin >> n; Student s[100];//假定最多输入 100 个学生的成绩 cout << "请依次输入学生的姓名,第一门课的成绩,第二门课的成绩,第三门课的成绩以及总分:" << endl ; for (int i = 0; i < n; i++) { cin >> s[i].name; cin >> s[i].score1; cin >> s[i].score2; cin >> s[i].score3; cin >> s[i].total; } find_fail(s, n); printByAverage(s, n); return 0; }