c++primer第四版第三章课后习题的几个解答算法

 

//读一组整数到vector 对象,计算并输出每对相邻元素的和


//使用迭代器访问vector 中的元素
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec;
int ival;
//读入数据到vector 对象
cout << "Enter numbers(Ctrl+Z to end):" << endl;
while (cin>>ival)
ivec.push_back(ival);
//计算相邻元素的和并输出
if (ivec.size() == 0) {
cout << "No element?!" << endl;
return -1;
}
cout << "Sum of each pair of adjacent elements in the vector:"
<< endl;
vector<int>::size_type cnt = 0;
for (vector<int>::iterator iter = ivec.begin();
iter < ivec.end()-1;
iter = iter + 2) {
cout << *iter + *(iter+1) << "/t";
++cnt;
if ( cnt % 6 == 0) //每行输出6 个和
cout << endl;
}
if (ivec.size() % 2 != 0) //提示最后一个元素没有求和
cout << endl
<< "The last element is not been summed "
<< "and its value is "
<< *(ivec.end()-1) << endl;
return 0;
}


//读一组整数到vector 对象,计算首尾配对元素的和并输出
//使用迭代器访问vector 中的元素


#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec;
int ival;
//读入数据到vector 对象
cout << "Enter numbers(Ctrl+Z to end):" << endl;
while (cin>>ival)
ivec.push_back(ival);
//计算首尾配对元素的和并输出
if (ivec.size() == 0) {
cout << "No element?!" << endl;
return -1;
}
cout << "Sum of each pair of counterpart elements in the vector:"
<< endl;
vector<int>::size_type cnt=0;
for (vector<int>::iterator first = ivec.begin(),
last = ivec.end() - 1;
first < last;
++first, --last) {
cout << *first + *last << "/t";
++cnt;
if ( cnt % 6 == 0) //每行输出6 个和
cout << endl;
}
if (first == last) //提示居中元素没有求和
cout << endl
<< "The center element is not been summed "
C++ Primer(4 版)习题解答
56
<< "and its value is "
<< *first << endl;
return 0;
}


//读入一段文本到vector 对象,每个单词存储为vector 中的一个元素。
//把vector 对象中每个单词转化为大写字母。
//输出vector 对象中转化后的元素,每8 个单词为一行输出。
//使用迭代器访问vector 中的元素

#include <iostream>

#include <string>
#include <vector>
#include <cctype>
using namespace std;
int main()
{
vector<string> svec;
string str;
//读入文本到vector 对象
cout << "Enter text(Ctrl+Z to end):" << endl;
while (cin>>str)
svec.push_back(str);
//将vector 对象中每个单词转化为大写字母,并输出
if (svec.size() == 0) {
cout << "No string?!" << endl;
return -1;
}
cout << "Transformed elements from the vector:"
<< endl;
vector<string>::size_type cnt = 0;
for (vector<string>::iterator iter = svec.begin();
iter != svec.end(); ++iter) {
for (string::size_type index = 0; index != (*iter).size();
++index)
if (islower((*iter)[index]))
//单词中下标为index 的字符为小写字母
(*iter)[index] = toupper((*iter)[index]);
cout << *iter << " ";
++cnt;
if (cnt % 8 == 0)//每8 个单词为一行输出
cout << endl;
}
return 0;
}

考虑这样的序列1,2,3,5,8,13,21,并初始化一个将该序列数字所对应的位置设
置为1 的bitset<32>对象。然后换个方法,给定一个空的bitset 对象,编写一
小段程序把相应的数位设置为1。 

【解答】
bitset<32>对象的初始化:
bitset<32> bv(0x20212e)
方法二:
bitset<32> bv;
int x = 0, y = 1, z;

z = x + y;
while (z <= 21) {
bv.set(z);
x = y;
y = z;
z = x + y;
}
注意,设置为1 的数位的位编号符合斐波那契数列的规律。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值