1.题(一)
代码:
#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 << "输入身份证号:" << endl; cin >> s; if( strlen(s)!=18 ){ cout << "身份证号输入错误" << endl; } int b[18] = {0};//将输入的身份证号都转化为int型 for(int i=0; i<18; i++){ b[i] = s[i] - '0';//字符转为int型 } int sum = 0;//前17位求和 for(int i=0; i<17; i++){ sum += b[i]*w[i]; } //求出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.题(二)
代码:
#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;//没有找到 if( find==a[mid] ) return mid;//恰好是中点 if( find>a[mid] ) return half_find(a, mid+1, r, find); if( find<a[mid] ) return half_find(a, 0, mid, find); */ //非递归 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;//没找到 } 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 << "查找数字:" << endl; cin >> find; int index = halffind(a, 10, find); if( -1==index ) cout << "没有找到" << endl; else cout << find << "是第" << index+1 << "个数,查找次数为" << count << endl; return 0; }
3.题(三)
代码:
#include<iostream> using namespace std ; //定义结构体 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; }