0%

算法练习

创建结构体(有关有员函数和重载输入输出)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
using namespace std;
class student{
private:
int age;
string name;
public:
void get_stu(){//输入数据
cin >> name >> age;
}
void out_stu(){//输出数据
cout << name << " " << age;
}
//定义友元函数
friend max_stu(student s1,student s2);
};
void max_stu(student s1,student s2){
if(s1.age > s2.age) s1.out_stu();
else s2.out_stu();
}
int main(){
student stu[2];
stu[0].get_stu();
stu[1].get_stu();
//使用友元函数
max_stu(stu[0],stu[1]);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
using namespace std;
class student{
private:
string name;
int age;
public:
friend istream& operator >>(istream& input,student& s){
input>>s.name>>s.age;
return input;
}
friend ostream& operator <<(ostream& output,student& s){
output<<s.name<<" "<<s.age;
return output;
}
void out_max(student s[],int n){
int flag=0,max=s[0].age;

for(int i=0;i<n;i++)
{
if(s[i].age>max){
max=s[i].age;
flag=i;
}
}
cout<<s[flag];
}

};

int main(){
int n=3;
student s[n];
for(int i=0;i<n;i++)
cin>>s[i];
cout<<endl;
//找到最大同学并输出
student max;
max.out_max(s,n);
}

简单的递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//f(n)=f(n-1)+f(n+1)
#include<iostream>
using namespace std;
int f(int n){
if(n==1||n==2) return 1;
return f(n-1)+f(n-2);
}
int main(){
for(int i=1;i<=10;i++)
{
cout<<f(i)<<" ";
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//f(n)=f(1)+f(2)+...+f(n);
#include<iostream>
using namespace std;
int f(int n){
if(n == 1) return 1;
else return n+f(n-1);
}
int main()
{
int n;
cin >> n;
cout << f(n);
return 0;
}

简单的进制转化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<bits/stdc++.h>
using namespace std;
//逆置数据串str ,这里使用内置函数,因为重复调用
inline void conversion(string &str){

for(int i = 0;i < str.length()/2;i++){

swap(str[i],str[str.length()-1-i]);
}
}
//将m进制的x转化为10进制并赋值给num
void conversion_M_10(int m,string x,int &num){
//首先调换位置
conversion(x);

//cout << "测试x的值是否被逆置:" << x <<endl;
//转换
for(int i = 0;i < x.length();i++){

if(x[i] >= '0' && x[i] <= '9'){

num = num + (x[i]-'0')*pow(m,i);

}
else{

num = num + (x[i]-'A'+10)*pow(m,i);

}
}

}
//将10进制的num转化为n进制并赋值给s
void conversion_10_n(int n,int num,string &s){

int i=0;char ch;

while(num){

ch = fmod(num,n);
if(ch < 10) ch = ch + '0';
else if(ch>'a'&&a<'z') ch = ch + 'a' - 10;
else if(ch>'A'&&a<'Z') ch = ch + 'A' - 10;
s=s+ch;
num=num/n;
}
conversion(s);

}
int main()
{
int m,n,num = 0;string x1,x2;

cin>>m>>x1;

conversion_M_10(m,x1,num);

cout << num <<endl; cin>>n;

conversion_10_n(n,num,x2);

cout<<x2;
return 0;
}

简单散列排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*输入一个数组的值,求出各个值从小到大排序后的次序。
输入:输入的第一个数为数组的长度,后面的数为数组中的值,以空格分割
输出:各输入的值按从小到大排列的次序。
*/
#include<bits/stdc++.h>
using namespace std;
void sorting_array(int a[],int n){
vector<int>b;//用来存放不重复数组
int temp[n];
//将数组a复制到temp中
memcpy(temp,a,n*sizeof(int));
//排序
sort(temp,temp+n);
//保存不重复的数据
for(int i = 0;i < n;i ++){
if(b.empty()) b.push_back(temp[i]);
else {
if(b.back() != temp[i]) b.push_back(temp[i]);
}
}
//输出数据
//for(int j = 0;j < b.size(); j++) cout<<b[j]<<" ";
//找到次序
for(int i = 0;i < n;i ++)
for(int j = 0;j < b.size(); j++)
{
if(a[i] == b[j]) {
a[i] = j;
}
}
// cout<<endl;
//输出元素
for(int i = 0;i < n;i ++) cout<<a[i]+1<<" ";
}

int main()
{
int n; cin>>n;
int a[n];
for(int i=0;i<n;i++) cin>>a[i];
sorting_array(a,n);

}