2000年北理复试上机题目
1.输入任意4个字符(如:abcd),并按反序输出(如:dcba)
#include<cstdio> int main() { char a[5]; scanf("%s", a); for (int i = 3; i >= 0; i--)printf("%c", a[i]); return 0; }
2.设a、b、c均是 0 到 9 之间的数字,abc、bcc是两个三位数,且有:abc+bcc=532。求满足条件的所有a、b、c的值。
#include<iostream> using namespace std; int main() { int a,b,c; cout<<"满足条件的 abc 为:"<<endl; for(a=0; a<10; a++) for(b=0; b<10; b++) for(c=0; c<10; c++) if(((a*100+b*10+c)+(b*100+c*10+c))==532) cout<<"a="<<a<<" b="<<b<<" c="<<c<<endl; return 0; }
3.一个数如果恰好等于它的各因子(该数本身除外)子和,如:6=3+2+1,则称其为“完数”;若因子之和大于该数,则称其为“盈数”。求出2到60之间所有“完数”和“盈数”,并以如下形式输出: E: e1 e2 e3 ......(ei为完数) G: g1 g2 g3 ......(gi为盈数)
#include<iostream> using namespace std; int main() { cout<<"E:"; for(int i=2;i<=60;i++) { int s=0; for(int j=1;j<i;j++) { if(i%j==0) { //s=0; s+=j; } } if(s==i) { cout<<i<<" "; } } cout<<endl; cout<<"G:"; for(int k=2;k<=60;k++) { int s=0; for(int j=1;j<k;j++) { if(k%j==0) { s+=j; } } if(s>k) { cout<<k<<" "; } } cout<<endl; }
4.从键盘输入4个学生的数据(包括姓名、年龄和成绩),并存放在文件sf1上。从该文件读出这些数据,按成绩从高到底排序,并输出其中成绩次高者的所有数据。
#include<iostream> #include<fstream> #include<algorithm> using namespace std; struct Student { char name[50]; int age; int score; }st[4]; bool cmp(Student a, Student b) { return a.score < b.score; } int main() { Student s; ofstream out("sf1.txt"); cout << "请输入四名学生的姓名、年龄、成绩:" << endl; for (int i = 0; i < 4; i++) { cin >> s.name >> s.age >> s.score; out << s.name << " " << s.age << " " << s.score << endl; } ifstream in("sf1.txt"); cout << "name " << " age " << "score " << endl; for (int i = 0; i < 4; i++) { in >> st[i].name >> st[i].age >> st[i].score; cout << st[i].name << " " << st[i].age << " " << st[i].score << endl; } sort(st, st + 4, cmp); cout << "name " << " age " << "score " << endl; for (int i = 0; i < 4; i++) cout << st[i].name << " " << st[i].age << " " << st[i].score << endl; return 0; }

浙公网安备 33010602011771号