实验报告八

1.排列对称串

很多字串,有些是对称的,有些是不对称的,请将那些对称的字串按从小到大的顺序输出。字串先以长度论大小,如果长度相同,再以ASCI码值为大小标准。

输入数据中含有一些字串(1≤串长s256)。

根据每个字串,输出对称的那些串,并且要求按从小到大的顺序输出。  Sample Input;

123321

123454321

123

321

sdfsdfd

121212

\\dd\\

Sample Output:  123321

\\dd\\

123454321

勒让德多项式

数学poly函数的展开式也称为关于x的n阶勒让德多项式,它的递推公式为:1n=0 polyn(x)=xn=1((2n-1)*x*polyn-1(x)-(n-1)*polyn-2(x))/nn>1给定x,请你计算n阶勒让德多项式的值。

输入数据中含有一些浮点数x(0<x<1)。

Output:

对于每个x,分别计算2阶、3阶、4阶、5阶、6阶的勒让德多项式的值,其每个值的精度为6位小数。输出时,先列出x的值,保留3位小数精度,然后每输出一个阶值之前,都空出2格,由此一字排开,形成一张多项式表,见样本输出格  式,其中标题行上第一个x对准小数点后第一位,后面的每个字母p对准下列的小数点位置。

Sample Input:

0.2 0.3 0.35

Sample Output

0.200

p2x

p3(x)

p4(x'

p5(G)

p6(x)

-0.440000  -0.280000

0.232000

0.307520

-0.080576

0.300 -0.365000 -0.382500

0.072938

0.345386

0.129181

0.350-0.316250 -0.417812-0.0187230.3224550.222511  

Lot

Time Limit: 2000 ms Memory Limit: 65536 KB

Out of N soldiers, standing in one line, it is required to choose several to send them scouting

In order to do that, the following operation is performed several times: if the line consists of more than three soldiers then all soldiers, standing on even positions, or all soldiers, standing on odd positions, are  taken away. The above is done until three or less soldiers are left in the line. They are sent scouting, Find, how many different groups of three scouts may be created this way

Note: Groups with less than three number of soldiers are not taken into consideration  0<N<=10 000000

Input

The input file contains the number N  Process to the end of file

Output

The output file must contain the solution - the amount of variants

Sample Input

10

4

Sample Output

 0

0

  • 实验内

1.

第一种

#include<iostream>  

#include<string>

#include<vector>

#include<algorithm>

using namespace std;

bool change(const string& c1, const string& c2)

{

return c1.length() != c2.length() ? c1.length() < c2.length() : c1 < c2;

}

int main()

{

int n;

cout << "请输入个数:";

cin >> n;//字符串个数

vector<string>a;

while (n--)

{

string w1;

cin >> w1;

bool m = true;

int i = 0, j = w1.size() - 1;

while (i < j)

{

if (w1[i] != w1[j])

{

m = false; break;//判断是否为倒置字符串

}

i++; j--;

}

m = false; break;

if (m) a.push_back(w1);//倒置字符串存入向量

}

sort(a.begin(), a.end(), change);//包含在头文件#include<algorithm>,默认从小到大排序

for (int k = 0; k < a.size(); k++)

{

cout << a[k] << endl;//输出

}

return 0;

}

第二种

#include <iostream>

#include <string>

#include <algorithm>

#include <vector>  

using namespace std;

bool change(const string& c1, const string& c2)

{

return c1.length() != c2.length() ? c1.length() < c2.length() : c1 < c2;

}

int main()

{

string w1, w2;

w1.reserve(100); //预留空间

w2.reserve(100);

vector<string>a;

int n;

cout << "请输入个数:";

cin >> n;//个数

for (int i = 1; i <= n; i++) {

cin >> w2;

w1= w2;

reverse(w1.begin(), w1.end());//逆序输出,reverse(逆序开始的地址,逆序结束的地址)

if (w1 == w2)

{

a.push_back(w2);

}

}

sort(a.begin(), a.end(), change);//在给定范围使用change

for (int i = 0; i < a.size(); i++)

{

cout << a[i] << endl;

}

return 0;

}

2.

#include <iostream>

#include<iomanip>

using namespace std;

double Legendre(double a, int b)

{

    double w;  //n阶勒让德多项式

    if (b == 0)

        return 1.0;

    else if (b == 1)

        return a;

    else if (b > 1)

    {

        w = ((2 * b - 1) * a * Legendre(a, b - 1) / b - (b - 1) * Legendre(a, b - 2) / b);  //递归

        return w;

    }

    else

        return 0;

}

int main() {

double n;  //输入所求数字

while (cin >> n) {

cout << setw(3) << "x" << setw(13) << "p2(x)" << setw(13) << "p3(x)" << setw(12) << "p4(x)"

<< setw(14) << "p5(x)" << setw(13) << "p6(x)" << endl;

for (int i = 1; i <= 6; i++)

{

int g = i;

double z = Legendre(n, g);

cout << setprecision(6) << z << setw(13);

}

cout << endl;

}

return 0;

}

3.

#include<iostream>

using namespace std;

long long change(long long x) //如果定义为double函数会出现报错,至少为long

{

long long result;

if (x == 1 || x == 2)

 result = 0; //C[1]=C[2]=0

else if (x == 3)

{

result = 1;//C[3]=1

}

else

{

result=(change(x / 2) + change(x - x / 2));//分成两半

}

return result;

}

int main()

{

long long n,sum;

while (cin >> n)

{

sum=change(n);

cout << sum<<endl;

}

}

本次实验中最有挑战性的是第一题,我在使用STL的过程中预先对string预留了空间,sort函数的使用极其便利,能对限定范围进行函数排序处理,reverse能对字符串进行倒置处理,reserve则能预留空间,二者功能不同。其他部分都比较顺利。勒让德多项式的输出版面我是对照六位小数进行设计(以0.357的输出结果为例)。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值