# 2018年复旦大学机试题

## 一、计算机学院

1、

8
10 3 8 8 3 2 2 2

2

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100000000;
int hashtable[maxn] = {0};
int main(){
int n, x;

scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%d", &x);
hashtable[x]++;
}
int k = 0, ans = hashtable[0];
for(int i = 0; i < maxn; i++){
if(hashtable[i] > ans){
ans = hashtable[i];
k = i;
}
}
printf("%d", k);
return 0;

}



2、

10x-2x-8=4x+7+x

x=5

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
string str, num;

int strtonum(string s){		//用于将字符串转换成int型数值
int ans = 0;
for(int i = 0; i < s.length(); i++){
ans = ans * 10 + s[i] - '0';
}
return ans;
}

int main(){
getline(cin, str);
int denghaopos = str.find("=");
int cox = 0, con = 0;   //x系数，常系数
int flag = 1;       //初试默认系数为正
for(int i = 0; i <= denghaopos; i++){        //等号左边
if(str[i] >= '0' && str[i] <= '9'){
num += str[i];
}else if(str[i] == 'x'){
if(str[i - 1] == '+' || str[i - 1] == '-' || i - 1 == -1) num += '1';
cox += strtonum(num) * flag;
num.clear();
}else if(str[i] == '+'){
if(num.empty()){
flag = 1;
}else{
con += strtonum(num) * flag;
num.clear();
flag = 1;
}
}else if(str[i] == '-'){
if(num.empty()){
flag = -1;
}else{
con += strtonum(num) * flag;
num.clear();
flag = -1;
}
}else if(str[i] == '='){
if(num.empty()){
flag = 1;
}else{
con += strtonum(num) * flag;
num.clear();
flag = 1;
}
}
}
int cox1 = 0, con1 = 0;     //等号左边的X系数、常系数
for(int i = denghaopos + 1; i < str.length(); i++){ //等号右边
if(str[i] >= '0' && str[i] <= '9'){
num += str[i];
}else if(str[i] == 'x'){
if(str[i - 1] == '+' || str[i - 1] == '-' || str[i - 1] == '=') num += '1';
cox1 += strtonum(num) * flag;
num.clear();
}else if(str[i] == '+'){
if(num.empty()){
flag = 1;
}else{
con1 += strtonum(num) * flag;
num.clear();
flag = 1;
}
}else if(str[i] == '-'){
if(num.empty()){
flag = -1;
}else{
con1 += strtonum(num) * flag;
num.clear();
flag = -1;
}
}
if(i == str.length() - 1 && str[i] >= '0' && str[i] <= '9'){
con1 += strtonum(num) * flag;
num.clear();
}
}
int coxend = cox - cox1, conend = con - con1;
if(coxend == 0 && conend == 0){
printf("infinite solutions");
return 0;
}else if(coxend == 0 && conend != 0){
printf("no solution");
return 0;
}
int ans = (-1) * conend / coxend;
printf("x=%d", ans);
}



3、

6

13

n = 1时，只有1种情况；
n = 2 时，有2种情况；
n = 3时，地板大小2 * 3，将情况分为22和21两部分，分别对应n=1和n=2的情况。。。列出方程d[i] = d[i - 1] + d[i - 2]；i = 3,4,5,6…

#include <cstdio>
const int maxn = 100010;

int func(int n){
if(n == 1){
return 1;
}else if(n == 2){
return 2;
}else{
return func(n - 1) + func(n - 2);
}
}

int main(){
int n;
int d[maxn];
scanf("%d", &n);
int ans = func(n);
printf("%d", ans % 999983);
}



## 二、工研院

1、

4 5
3 4 7 3
4 6 3 2 6

2 5

#include <cstdio>
#include <set>
#include <iostream>
using namespace std;

set<int> S1, S2, S3;
bool hashtable[10000010] = {false};
int main(){
int n, m, x, num = 0;
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++){
scanf("%d", &x);
hashtable[x] = true;
S1.insert(x);
}
for(int i = 0; i < m; i++){
scanf("%d", &x);
if(hashtable[x] != false) num++;        //用于求交集
S2.insert(x);
}
for(set<int>::iterator i = S1.begin(); i != S1.end(); i++){ //求并集
S3.insert(*i);
}
for(set<int>::iterator i = S2.begin(); i != S2.end(); i++){	//求并集
S3.insert(*i);
}
int num_bing = 0;
for(set<int>::iterator i = S3.begin(); i != S3.end(); i++){
num_bing++;
}
printf("%d %d", num, num_bing);
return 0;
}



2、

7

41

#include <cstdio>
#include <cmath>
int main(){
int n, ans = 0;
scanf("%d", &n);
for(int i = 1; i <= n; i++){        //1~n之间的所有数
for(int j = 1; j <= i; j++){   //计算这些数的约数并相加
if(i % j == 0){
ans += j;
}
}

}
printf("%d", ans);
return 0;
}



3、

0 0 5 5
0 2 2 0

1 1

#include <cstdio>

int main(){ //y = kx + b
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);//第一条线
double k1 = (d - b) / (c - a);
double b1 = b - a * k1;
scanf("%d %d %d %d", &a, &b, &c, &d);//第二条线
double k2 = (d - b) / (c - a);
double b2 = b - a * k2;
if (k1 == k2)
printf("平行或重合");
else {
double x = (b2 - b1) / (k1 - k2), y = k1*x + b1;
printf("%.2lf %.2lf", x, y);
}
return 0;

}


10-18

10-12
06-30
05-19 3960
05-19 2356
03-07 711
02-23 76
03-07 326
04-18 70
04-13 8838
04-11 5107
04-14 1万+
03-15 4629
07-09 208