C++重修期末考试复习题
来源:互联网 发布:eia原油库存数据预测 编辑:程序博客网 时间:2024/04/25 20:04
CH4作业:
1、求一个3×4矩阵中的最大元素。(自己定义二维数组实现)
2、判断用户输入的C风格字符串是否为“回文”。 “回文”是指从左向右读和从右向左读都是一样的字符串,例如12321、madam。(用数组操作实现)
3、按一定的规则可以将一个字符串经过加密转换为一个新的字符串。(用指针操作实现)
加密的简单方法是:当为‘a’~‘y’中的小写字母时,用后一个字母代替前一个字母,例如,‘a’加密后为‘b’,其中‘z’加密后为‘a’;为其他字符时不变。例如:
原字符串: This is a secret code!
加密后的字符串: Tijt jt b tfdsfu dpef!
A1:
#include <iostream>
using namespace std;
int Findmax(int m[3][4])
{
int max = m[0][0];
for(int i=0;i<3;i ++)
{
for(int j=0;j<4;j++)
{
if(m[i][j]>max)
max=m[i][j];
}
}
return max;
}
int main()
{
int a[3][4]=
{
{1,2,3},
{4,5,6},
{7,8,9}
};//二维数组居然是以分号结尾的,天!
cout<<Findmax(a)<<endl;
return 0;
}
A2与A3:
#include <iostream>
#include <string>using namespace std;
bool isRever(string s)
{
int k=s.length();
for(int i=0;i < k/2;i ++)
{
if(s[i]!=s[k-1-i])//注意用数组时反转要从length-1的位置开始!!否则会发生数组越界的情况
return 0;
}
return 1;
}
void Addsecret(char str[])
{
char *p = str;
for(*p;*p!=NULL;*p ++)
{
if((*p)>='a' &&(*p)<='z')
*p = (*p)+1;//注意一定要加括号,因为优先级的关系,不然会默认p+1报错,发生越界
cout<<*p;
}
}
int main()
{
string temp;
cin>>temp;
cout<<isRever(temp);
char buffer[20];
while(cin>>buffer)
{
Addsecret(buffer);
}
return 0;
}
CH5上机作业:
1、根据cos(x)的麦克劳林展开式可求当x<1时x余弦的近似值,展开式如下:
如果取前n项作为累计和的近似值,则第n+1项就是误差项。设计程序求cos(0.3)的近似值,要求误差小于0.0001。
【第1题提示】利用后项与前项的关系逐步推进;注意符号项的变化。
2、调用随机函数rand(),产生10个取值小于1000的整数,统计其中升序数、降序数的个数输出到屏幕。
升序数指高位数字不大于低位数字的数,例如3,456,446,22;降序数指高位数字大于低位数字的数,例如54,831。而263,834之类则即非升序数也非降序数。
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double x = 0.3;
double error = 1.0;
int n=1;
double sum=0.0;
while(error>=0.0001)
{
if(n%2==0)
sum -= error;
else
sum += error;
error = abs(error*0.09/((2*n-1)*(2*n)));
n ++;
}
cout<<sum<<endl;
cout<<error<<endl;
return 0;
}
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
#define MIN 0
#define MAX 1000
bool isUpnumber(int num)
{
while(num!=0)
{
int t = num%10;
num /=10;
if(t < num%10)
return false;
}
return true;
}
bool isDownnumber(int num)
{
while(num!=0)
{
int t = num%10;
num /=10;
if(t > num%10)
return false;
}
return true;
}
int main()
{
int i,upnumber=0,downnumber=0;
srand((unsigned)time(NULL));
int s[10];
for(i=0;i<10;i++)
{
s[i]=MIN+rand()%(MAX+MIN-1);
if(isDownnumber(s[i]))
downnumber ++;
if(isUpnumber(s[i]))
upnumber ++;
cout<<s[i]<<" ";
}
cout<<"升序数个数:"<<upnumber<<endl;
cout<<"降序数个数:"<<downnumber<<endl;
return 0;
}
作业6:
1、编写一个小程序,从标准输入读入一系列 string 对象,寻找连续重复出现的单词。程序应该找出满足以下条件的单词的输入位置:该单词的后面紧跟着再次出现自己本身。跟踪重复次数最多的单词及其重复次数。输出重复次数的最大值,若没有单词重复则输出说明信息。例如,如果输入是:
how, now now now brown cow cow
则输出应表明“now”这个单词出现了三次。
2、给出两个 int 型的 vector 对象,编写程序判断一个对象是否是另一个对象的前缀。如果两个 vector 对象的长度不相同,假设较短的 vector 对象长度为n,则只对这两个对象的前面 n 个元素做比较。例如,对于 (0, 1, 1, 2) 和 (0,1, 1, 2, 3, 5, 8) 这两个 vector,你的程序应该返回 true。
3、用筛选法求100之内的所有素数,并将这些素数输出。
A1:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string preWord,currWord;
string repWord;
int Curcnt=0,Maxcnt=1;
while(cin>>currWord)
{
if(preWord==currWord)
{
Curcnt ++;
}
else Curcnt=1;
if(Maxcnt<Curcnt)//更新重复的字符串
{
Maxcnt=Curcnt;
repWord=currWord;
}
preWord=currWord;
}
if(Maxcnt!=1)
{
cout<<"重复字符串为:"<<repWord<<" "<<Maxcnt<<endl;
}
return 0;
}
用迭代器实现更新(貌似不可以,诶)这段代码有点问题)
#include<iostream>
#include<vector>
#include<string>
using namespace std;
bool equal(string a,string b)
{
int i=a.length(),j=b.length();
if(i!=j) return 0;
else
{
for(i=0;i<j;i ++)
{
if(a[i]!=a[i])
return 0;
else
return 1;
}
}
}
int main()
{
string input;
string temp;
int repeat=0;
vector<string> list;
vector<string>::iterator iter;
while(cin>>input)
{
list.push_back(input);
}
//追踪出现最多次数的字符串和次数
iter=list.begin();
while(iter <= list.end())//因为iter+1要小于end,假设字符串为how how how what what
{
while(equal(*iter,*(iter+1)))
{
repeat ++;
iter ++;//这里加完后就等于end-1了
}
if(repeat!=0)
{
cout<<*iter<<endl;
cout<<repeat<<endl;
}
repeat = 0;//对最后2个是否重复的情况需要额外的判断!
//iter ++;
}
return 0;
}
A2:
#include<iostream>
#include<vector>
using namespace std;
bool isPrefix(vector<int> f_list,vector<int> l_list)
{
vector<int>::iterator iter1=f_list.begin();
vector<int>::iterator iter2=l_list.begin();
int size1,size2;
size1=f_list.size();
size2=l_list.size();
if(size1<size2)
{
while(iter1!=f_list.end())
{
if(*iter1 != *iter2)
return 0;
iter1 ++;
iter2 ++;
}
return 1;
/*for(int cnt=0;cnt<size1;cnt ++)
{
if(f_list[cnt]!=l_list[cnt])
return 0;
}
return 1;*/
}
else
{
while(iter2!=l_list.end())
{
if(*iter1 != *iter2)
return 0;
iter1 ++;
iter2 ++;
}
return 1;
/*for(int cnt=0;cnt<size2;cnt ++)
{
if(f_list[cnt]!=l_list[cnt])
return 0;
}
return 1;*/
}
}
int main()
{
vector<int> list1;
vector<int> list2;
int input;
cout<<"读取第一个vector函数"<<endl;
cin>>input;
while(input != 111)
{
list1.push_back(input);
cin>>input;
}
vector<int>::iterator iter1,iter2;
cout<<"读取第二个vector函数"<<endl;
cin>>input;
while(input != 111)
{
list2.push_back(input);
cin>>input;
}
if(isPrefix(list1,list2))
cout<<"Y"<<endl;
else cout<<"N"<<endl;
return 0;
}
A3:求素数
#include<iostream>
#include<math.h>
using namespace std;
bool isnum(double num)
{
for(int k = 2;k <= sqrt(num);k ++)
{
if((int)num % k==0)
return 0;
}
return 1;
}
int main()
{
for(int a=1;a<=100;a ++)
{
if(isnum(a))
cout<<a<<" ";
}
cout<<endl;
}
作业7——1:输入输出流
1、调用rand()产生10个取值[0,25000]之间的整数,将这10个数及其反序数中的奇数输出到文件reverse.txt中。要求求反序数用函数实现。
2、设计程序验证歌德巴赫猜想:任意一个不小于6的偶数均可表示为2个素数之和。程序要求:读入一个不小于6的偶数,求出其所有两个素数之和的组合,例如:16=3+13=5+11(11+5及13+3不重复计入)。请将该偶数及其素数组合以写入文本文件goldbach.txt中,以16为例,输出格式为:
16
3,13
5,11
要求恰当使用函数,使程序结构清晰,可读性好。(若时间充裕,请再将goldbach.txt内容读出,输出到显示器。)
A1:
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<time.h>
using namespace std;
#define MIN 0
#define MAX 25000
//输出产生的随机数与反序数中的奇数,注意生成的txt文件就在默认cpp的文件夹路径下面
int isReve(int num)
{
int rev =0;
while(num >0)
{
int temp=num%10;
rev =rev*10+temp;
num /=10;
}
return rev;
}
int main()
{
ofstream ofs("test.txt",ofstream::out);
int s[10];
for(int i=0;i<10;i ++)
{
s[i]=MIN+rand()%(MAX+MIN-1);
ofs<<s[i]<<"\t";
if((isReve(s[i]))%2==1)
{
int temp=isReve(s[i]);
ofs<<temp<<"\t";
}
}
return 0;
}
A2:
#include<iostream>
#include<fstream>
#include<istream>
#include<math.h>
using namespace std;
bool isnum(double num)
{
for(int k = 2;k <= sqrt(num);k ++)
{
if((int)num % k==0)
return 0;
}
return 1;
}
int main()
{
ifstream fin("test.txt",ifstream::in);
ofstream ofs("goldbach.txt",ofstream::out);
int data;
fin>>data;
for(int num=0;num<data/2;num ++)
{
if(isnum(num) && isnum(data-num))
ofs<<num<<"and"<<data-num<<endl;
}
return 0;
}
作业7——2:递归与递推
1.递推:
#include<iostream>
using namespace std;
int fab[20];
int main()
{
fab[0]=1;
fab[1]=2;
for(int i=2;i < 20;i ++)
{
fab[i]=fab[i-1]+fab[i-2];
}
for(int j=0;j < 20;j ++)
{
cout<<fab[j]<<" ";
}
return 0;
}
2.递归(第二个太简单)
#include<iostream>
using namespace std;
double Lenger(double x,int n)
{
double result;
if(n==0)
return 1;
if(n==1)
return x;
result = ((2*n-1)*x*Lenger(x,n-1)-(n-1)*Lenger(x,n-2))/n;
return result;
}
int main()
{
int n=4;
double x;
cin>>x;
cout<<"result="<<Lenger(x,n)<<endl;
return 0;
}
- C++重修期末考试复习题
- 计算机网络技术期末考试复习题
- C语言复习题
- c语言考试复习题
- Linux-C复习题
- C语言复习题1.1
- C语言复习题1.2
- C语言复习题1.3
- C语言复习题1.4
- C程序设计进阶_期末考试
- 期末考试
- 期末考试
- 期末考试
- 期末考试????
- 期末考试
- 期末考试......
- 期末考试
- 期末考试
- java基础---面向对象浅析
- iOS 之Core Animation中的CALayer
- 黑马程序员--java高级教程---反射
- iOS 之core Animation中的多种动画效果
- 10个鲜为人知的WordPress函数(涨见识了)
- C++重修期末考试复习题
- string函数实现
- iOS 之摇一摇
- 聊天软件悄然兴起 手机短信遇冷“失宠”
- iOS 之XMPP原理
- System.exit(0)和System.exit(1)区别
- 信息检索和网络数据挖掘领域论文
- 后台执行Linux命令脚本的方法
- 巧做卡拉OK遮罩字幕