实验一算法描述及其程序实现
来源:互联网 发布:四海认证淘宝渔具 编辑:程序博客网 时间:2024/06/05 20:43
(验证型实验2学时)
一.实验目的
(1)巩固程序设计语言基础知识,熟悉文件操作等。
(2)对给定问题,能设计算法并编程实现问题的求解。
二.实验要求
(1)认真填写实验报告,附加源代码(主要代码)和运行记录;
(2)对设计好的算法,测试运行实验数据,检查输出是否正确。
三.实验内容:
问题1.用Euclid算法求两个非负整数m和n的最大公因数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.使用连续检测算法求两个非负整数m和n的大公因数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
- 实验一算法描述及其程序实现
- AAN算法描述及其实现
- AAN算法描述及其实现
- 《算法导论》实验一:快速排序算法及其优化
- 爹地的实验:实验一: 数据结构实验一线性表的基本操作实现及其应用
- 各种排序算法及其java程序实现
- 各种排序算法及其java程序实现
- 各种排序算法及其java程序实现
- 各种排序算法及其java程序实现
- 各种排序算法及其java程序实现
- 各种排序算法及其java程序实现
- 各种排序算法及其java程序实现
- 各种排序算法及其java程序实现 .
- 各种排序算法及其java程序实现
- 各种排序算法及其java程序实现
- 各种排序算法及其Java程序实现
- 各种排序算法及其java程序实现
- 各种排序算法及其java程序实现
- Java 接口
- Ubuntu 14.04 文件服务器--samba的安装和配置
- android面试试题
- HDU 1114 Piggy-Bank(dp完全背包)
- HDU 5701 中位数计数 百度之星初赛
- 实验一算法描述及其程序实现
- [JLOI2011]飞行路线
- java-图书管理系统
- Android之音视频点、直播模块开发
- 欢迎使用CSDN-markdown编辑器
- 2016东软校招笔试面试体验
- stm32f10x知识点整理--Timer(持续更新中)
- Git的安装和使用
- socket()函数