test 2.1-4
2015-08-26 17:20
190 查看
题目说的两个n位的二进制相加,结果保留到第三个n+1位的二进制里,我假设n=5,把原来两个二进制用line1【5】,line2【5】来表示,数组中每个元素只能是0或者1,程序如下
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> line; int target;
cout << "Enter your target number :"; cin >> target;
cout << "Enter your sequence(alpha to quit) :";
int temp;
while (cin >> temp)
line.push_back(temp);
int n = line.size();
bool mark = false;
for (int i = 0; i < n; i++)
{
if (line[i] == target)
{
cout << i + 1; mark = true;
}
}
if (!mark)
cout << "target=NIL ";
return 0;
}
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> line; int target;
cout << "Enter your target number :"; cin >> target;
cout << "Enter your sequence(alpha to quit) :";
int temp;
while (cin >> temp)
line.push_back(temp);
int n = line.size();
bool mark = false;
for (int i = 0; i < n; i++)
{
if (line[i] == target)
{
cout << i + 1; mark = true;
}
}
if (!mark)
cout << "target=NIL ";
return 0;
}
相关文章推荐
- java算法导论之FloydWarshall算法实现代码
- Red-Black Tree 的Java实现
- 逆序对
- 算法导论之堆
- LCS 最长公共子序列
- 使用Java完成《算法导论》习题2.2-2
- 使用Java完成《算法导论》习题2.3-2
- 使用Java完成《算法导论》习题2.3-4
- 使用Java完成《算法导论》习题2.3-5
- 使用Java完成《算法导论》习题2.3-6
- 使用Java完成《算法导论》习题2.3-7
- 快速排序个人理解
- 笔记一(算法基础)
- Chapter2 Getting Started
- Chapter3 Growth of Functions
- 插入排序和归并排序
- 第2章 算法入门
- 编程资源共享导航(更新)
- 算法导论 第15章 动态规划之钢条切割
- Asymptotic Notation and Recurrences