实验一算法描述及其程序实现

来源:互联网 发布:四海认证淘宝渔具 编辑:程序博客网 时间:2024/06/05 20:43


(验证型实验2学时)

.实验目的

1巩固程序设计语言基础知识,熟悉文件操作等。

2对给定问题,能设计算法并编程实现问题的求解。


.实验要求

1认真填写实验报告,附加源代码(主要代码)和运行记录;

2对设计好的算法,测试运行实验数据,检查输出是否正确。

.实验内容:

问题1.Euclid算法求两个非负整数mn的最大公因数GCD(m,n)和最小公倍数。

1)用流程图描述Euclid算法

(2)用伪代码描述Euclid算法:

//使用Euclid算法求GCD(m,n)

//输入:两个不全为0的非负整数m,n//输出:GCD(m,n)while n¹0,dor¬mmod nm¬nn¬rreturn m

(3)编程实现此算法.

#include <iostream>using namespace std;//Greatest common factor int gcd(int m, int n){    int t;    if(m < n)    {t = m;m = n;n = t;    }    while(n != 0)    {t = m % n;m = n;n = t;    }    return m;}//Least common multiple int lcm(int m, int n){    return m * n / gcd(m,n);}int main(){    int m , n;    cout << "\tplease input m: ";    cin >> m;    cout << "\tplease input n: ";    cin >> n;    cout << "\tgcd= ";    cout << gcd(m,n) << endl;    cout << "\tlcm= ";    cout << lcm(m,n) << endl;    return 0;}





问题2.使用连续检测算法求两个非负整数mn的大公因数GCD(m,n)

1用流程图描述描述连续检测算法:

2)用伪代码描述连续检测算法:

//使用连续检测算法求GCD(m,n)

//输入:两个不全为0的非负整数m,n

//输出:GCD(m,n)


t¬min(m,n)

while t>1, do

if m|t do

ifn|t

return t

t¬t-1

return 1

(3)编程实现此算法。

#include <iostream>using namespace std;//judge 'm','n'int min(int m, int n){    return (m <= n ? m : n);}//gcd Greatest common factor int gcd(int m, int n){    int t;    t = min(m,n);    while(t > 1)    {if(m % t == 0 && n % t == 0)    return t;t--;    }}int main(){    int m, n;    cout << "\tplease input m= ";    cin >> m;    cout << "\tplease input n= ";    cin >> n;    cout << "\tgcd= " << gcd(m,n) << endl;    return 0;}





问题3.在一个n元数组中按顺序查找是否包含元素K


(1)设计解决此问题的算法,并用伪代码描述;

(2)编程实现此算法。


#include <iostream>#include <cstdio>using namespace std;int main(){    string a;    char K;    int n=0;    cout << "\tinput n\t";    cin >> n;   // <100    cout << "\tinput K\t";    cin >> K;    //input a[]    int i = 0;    cout << "\tinput a[]\t";    cin >> a;    //find K    for (int i=0; i < n; i++)    {if (K == a[i])  {      cout << "\tcontains 'K'" << endl;    break;  }else if(i == n-1)    cout << "\tNO contains 'K'" << endl;    }      return 0;}



问题4.在一个n元数组中查找元素最大值。

(1)设计解决此问题的算法,并用伪代码描述;

(2)编程实现此算法。

#include <iostream>#include <vector>using namespace std;int main(){    vector<int> ivec;    int tmp, max; //input 'tmp', max    cout << "\tplease input vector 'ivec':\n";    while(cin >> tmp){ //input number the end of the "Ctel + D"        ivec.push_back(tmp);    }    for(int i = 1; i < ivec.size(); ++i){if(ivec[0] < ivec[i])    //int t = ivec[0]; //save    ivec[0] = ivec[i];    //ivec[i] = t;    }    cout << "\t max= " << ivec[0] << endl;    return 0;  }




问题5.在一个n元数组中验证元素是否互不相同。

(1)设计解决此问题的算法,并用伪代码描述;

(2)编程实现此算法。

#include <iostream>#include <vector>using namespace std;int main(){    vector<int> ivec;    int tmp;     cout << "\tplease input vector 'ivec':\n";    while(cin >> tmp){ //input number the end of the "Ctel + D"        ivec.push_back(tmp);    }        cout << "\tIs different, then don't output; " <<endl;    cout << "\tThere are the same, the output " <<endl;    for(int i = 0; i < ivec.size(); ++i){for(int j = i + 1; j < ivec.size(); ++j){    if(ivec[i] == ivec[j]){cout << "\t\tHave the same" << endl;break;    }}        }    return 0;  }


0 0
原创粉丝点击