题目来源:https://blog.csdn.net/u014552756/article/details/79555984
代码原创
1.身份证号
#include<bits/stdc++.h>
using namespace std;
int main(){
int y = 0;
char p[11] = {'1', '0', 'X', '9', '8', '7', '6', '5', '3', '2'};
int w[17] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
string s;
while(cin >> s){
for(int i = 0; i < 17; i++){
int tmp = s[i] - '0';
y += w[i] * tmp;
}
y %= 11;
if( p[y] == s[17]){
cout << s << " is correct."<<endl;
}else{
s[17] = p[y];
printf("it is supposed to be :");
cout << s << endl;
}
}
return 0;
}
2.二分查找
#include<bits/stdc++.h>
using namespace std;
int bySearch(vector<int> a, int num, int &cnt){
int left = 0;
int right = a.size() - 1;
while(left <= right){
cnt++;
if(left == right){
if(a[left] == num || a[right] == num)
return left;
else
return -1;
}
int mid = (left + right ) / 2;
if(num == a[mid]){
return mid;
}else if(num < a[mid]){
right = mid - 1;
}else{
left = mid + 1;
}
}
return -1;
}
int main(){
vector<int> a;
int tmp;
while(1){
scanf("%d", &tmp);
a.push_back(tmp);
if(getchar() == '\n'){
break;
}
}
cout<<"Please input a number to search : ";
int num;
while(cin >> num){
int cnt = 0;
int pos = bySearch(a, num, cnt);
if(pos != -1){
cout << "Position is " << pos + 1 << ", and compare times are "<<cnt<<endl;
}else{
cout << "Search failed, and compare times are " << cnt <<endl;
}
cout<<"Please input a number to search : ";
}
}
3.成绩单
#include<bits/stdc++.h>
using namespace std;
struct student{
string name;
int score[3];
double average;
};
bool cmp(struct student a, struct student b){
return a.average > b.average;
}
int main(){
int n;
cin >> n;
vector<struct student>list;
for(int i = 0; i < n; i++){
struct student tmp;
cin >> tmp.name >> tmp.score[0]>> tmp.score[1]>> tmp.score[2];
tmp.average = (tmp.score[0] + tmp.score[1] + tmp.score[2])*1.0/3;
list.push_back(tmp);
}
for(int i = 0; i < n; i++){
if(list[i].score[0] < 60 || list[i].score[1] < 60 || list[i].score[2] < 60){
cout << "*name:" << list[i].name << " " << "score: "
<< list[i].score[0]<<" "<<list[i].score[1]<<" "<< list[i].score[2]<<endl;
}
}
sort(list.begin(), list.end(), cmp);
for(int i = 0; i < n; i++){
cout<<"["<<i+1<<"] name: "<<list[i].name<<" "
<< list[i].score[0]<<" "<<list[i].score[1]<<" "<< list[i].score[2]<<endl;
}
return 0;
}