实验1

实验文件1

image
reverse是直接修改原数据,reverse_copy是在不修改原数据的情况下返回一组反转数据。
rotate算法是通过不断循环左移,使指定元素成为首位元素。三个参数分别是区间起点,运行后指定为首位的元素,区间终点。

实验文件2

image
generate算法作用是用指定函数的返回值填充指定区间。
minmax_element可以通过一次遍历数组,直接得出最大值和最小值。
作用是每次调用返回一个0到100之间的随机值,与generate_random_number相比,lambda表达式更适合于临时使用。

实验文件3

image
func函数的作用是将字母循环后移一位
tolower是将大写转换为小写,toupper是将小写转换为大写
transform的4个参数分别是:
1.函数输入起点
2.函数输入终点
3.函数输出起点
4.所要执行的函数
区别是最后的结果会储存在s1里

实验文件4

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool is_palindrome(const std::string &s);
bool is_palindrome_ignore_case(const std::string &s);

int main() {
using namespace std;
string s;

// 多组输入,直到按下Ctrl+Z结束测试
while(cin >> s) {
cout << boolalpha
<< "区分大小写: " << is_palindrome(s) << "\n"
<< "不区分大小写: " << is_palindrome_ignore_case(s) << "\n\n";
}
}

// 函数is_palindrome定义

bool is_palindrome(const string &s) {
string t=s;
reverse(t.begin(),t.end());
return (s==t);
}

// 函数is_palindrome_ignore_case定义
bool is_palindrome_ignore_case(const string &s) {

std::string t=s;
for (char &c:t)
c=static_cast(tolower(static_cast(c)));
std::string q=s;
for (char &c:q)
c=static_cast(tolower(static_cast(c)));
reverse(t.begin(),t.end());

return (q==t);
}
image
改为用getline(cin,s)来输入

实验文件5

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
std::string dec2n(int x, int n = 2);

int main() {
int x;
while(std::cin >> x) {
std::cout << "十进制: " << x << '\n'
<< "二进制: " << dec2n(x) << '\n'
<< "八进制: " << dec2n(x, 8) << '\n'
<< "十二进制: " << dec2n(x, 12) << '\n'
<< "十六进制: " << dec2n(x, 16) << '\n'
<< "三十二进制: " << dec2n(x, 32) << "\n\n";
}
}

// 函数dec2n定义
std::string dec2n(int x, int n ){
string s;
char c;
int k=0;
if(x==0)s='0';
while(x!=0){
k=x%n;
if(k<10)c='0'+k;
else c='0'+k+7;
s.push_back(c);
x=x/n;
}
reverse(s.begin(),s.end());
return s;
}
image

实验文件6

#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;
int main() {
int i=0,j=0;
char c;
for(; i<27; i++) {
if(i==0) {
cout<<" ";
for(j=0; j<26; j++) {
c='a'+(i+j)%26;
cout<<right<<setw(2)<<c;
}
cout<<endl;
} else {
cout<<right<<setw(2)<<i;
for(j=0; j<26; j++) {
c='A'+(i+j)%26;
cout<<right<<setw(2)<<c;

}
cout<<endl;
}
}
}
image

实验文件7

#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;
int main() {
srand(time(0));
int i,j,k,t;
int m;
double a=0,b;
int n=0;
for(;n<10;n++){
k=rand()%3;
i=rand()%10;
j=rand()%10;
if(k==0){
t=i+j;
cout<<i<<'+'<<j<<'=';
}
if(k==1){
while(i<j){
i=rand()%10;
j=rand()%10;
}
t=i-j;
cout<<i<<'-'<<j<<'=';
}
if(k==2){
t=ij;
cout<<i<<'
'<<j<<'=';}
if(k==3){
while(i%j!=0){
i=rand()%10;
j=rand()%10;
}
t=i/j;
cout<<i<<'/'<<j<<'=';
}
cin>>m;
if(m==t)a++;
}
b=a/10;
cout<<"正确率为"<< fixed << setprecision(2) << b * 100 << endl;
return 0;
}
image
image

posted @ 2026-10-21 23:53  星祈sajou  阅读(4)  评论(0)    收藏  举报