C/C++ - waiting...'s Blog
cout作为变量名
// cout作为变量名_C++.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
int cout;
cout = 50;
std::cout << "cout = " << cout << std::endl;
return 0;
}
//
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
int cout;
cout = 50;
std::cout << "cout = " << cout << std::endl;
return 0;
}
二分查找
二分查找函数:
-
// 对于传入的以升序排列数组参数a,它以,则在下标为from和to之间查找值为num的数,找的则返回其下标;否则,返回-1
-
int binary_search(const int a[], int from, int to, int num)
-
{
-
if (from > to)
-
return -1;
-
-
int mid = (from + to) / 2;
-
if (a[mid] == num)
-
return mid;
-
else if (a[mid] < num)
-
return binary_search(a, mid + 1, to, num);
-
else
-
return binary_search(a, from, mid - 1, num);
-
}
完整代码:
-
#include <iostream>
-
#include <ctime>
-
#include <cstdlib>
-
using namespace std;
-
-
// 设置随机整数的种子
-
void rand_seed()
-
{
-
unsigned int seed = static_cast<unsigned int>(time(NULL));
-
srand(seed);
-
}
-
-
// 返回一个介于lower与upper之间的一个随机整数
-
int randInt(int lower, int upper)
-
{
-
return lower + rand() % (upper - lower + 1);
-
}
-
-
void print(const int a[], const int length)
-
{
-
for (int i = 0; i < length; i++)
-
cout << a[i] << " ";
-
cout << endl;
-
}
-
-
// 对于传入的以升序排列数组参数a,它以,则在下标为from和to之间查找值为num的数,找的则返回其下标;否则,返回-1
-
int binary_search(const int a[], int from, int to, int num)
-
{
-
if (from > to)
-
return -1;
-
-
int mid = (from + to) / 2;
-
if (a[mid] == num)
-
return mid;
-
else if (a[mid] < num)
-
return binary_search(a, mid + 1, to, num);
-
else
-
return binary_search(a, from, mid - 1, num);
-
}
-
-
int main()
-
{
-
const int length = 20;
-
const int lower = 1; // 随机数上限
-
const int upper = 20; // 随机数下限
-
int a[length];
-
-
rand_seed();
-
a[0] = 11;
-
for (int i = 1; i < length; i++)
-
a[i] = a[i - 1] + randInt(lower, upper);
-
-
print(a, length);
-
-
int num; // 待查找的数字
-
while (cin >> num)
-
{
-
int temp_index = binary_search(a, 0, length, num);
-
if (temp_index != -1)
-
cout << "a[" << temp_index << "] = " << a[temp_index] << endl;
-
else
-
cout << "未找到" << endl;
-
}
-
return 0;
-
}
顺序查找
-
#include <iostream>
-
#include <ctime>
-
#include <cstdlib>
-
using namespace std;
-
-
// 设置随机整数的种子
-
void rand_seed()
-
{
-
unsigned int seed = static_cast<unsigned int>(time(0));
-
srand(seed);
-
}
-
-
// 返回一个介于lower与upper之间的一个随机整数
-
int randInt(int lower, int upper)
-
{
-
return lower + rand() % (upper - lower + 1);
-
}
-
-
void print(const int a[], const int length)
-
{
-
for (int i = 0; i < length; i++)
-
cout << a[i] << " ";
-
cout << endl;
-
}
-
-
// 如果待查找数字存在于数组中,则返回其下标;否则返回-1
-
int linear_search(const int a[], const int length, int num)
-
{
-
for (int i = 0; i < length; i++)
-
{
-
if (a[i] == num)
-
return i;
-
}
-
return -1;
-
}
-
-
int main()
-
{
-
const int length = 20;
-
const int lower = 1; // 随机数上限
-
const int upper = 1000; // 随机数下限
-
int a[length];
-
int num; // 待查找的数字
-
-
for (int i = 0; i < length; i++)
-
a[i] = randInt(lower, upper);
-
-
print(a, length);
-
-
cout << "请输入要查找的数字:";
-
while (cin >> num)
-
{
-
int num_index = linear_search(a, length, num);
-
if (num_index != -1)
-
cout << "a[" << num_index << "] = " << a[num_index] << endl;
-
else
-
cout << "未找到" << endl;
-
cout << "请输入要查找的数字:";
-
}
-
-
return 0;
-
}
产生介于某一返回的随机数的函数
-
// 返回一个介于lower与upper之间的随机数
-
-
#include <cstdlib>
-
-
int rand_int(int lower, int upper)
-
{
-
return lower + rand() % (upper - lower + 1);
-
}
选择排序
-
#include <iostream>
-
#include <cstdlib>
-
#include <ctime>
-
using namespace std;
-
-
// 设置随机整数的种子
-
void rand_seed()
-
{
-
unsigned int seed = static_cast<unsigned int>(time(0));
-
srand(seed);
-
}
-
-
// 返回一个介于lower与upper之间的一个随机整数
-
int randInt(int lower, int upper)
-
{
-
return lower + rand() % (upper - lower + 1);
-
}
-
-
// 交换两个数的值
-
void swap(int &a, int &b)
-
{
-
int temp = a;
-
a = b;
-
b = temp;
-
}
-
-
void print(const int a[], int length)
-
{
-
for (int i = 0; i < length; i++)
-
cout << a[i] << " ";
-
cout << endl;
-
}
-
-
// 函数_选择排序_升序排列
-
void selection_sort(int a[], int length)
-
{
-
int i, j, temp_index;
-
for (i = 0; i < length - 1; ++i)
-
{
-
temp_index = i; // 存储未排序区中最大值的索引
-
for (j = i + 1; j < length; ++j)
-
if (a[j] < a[temp_index]) // 大于号:降序排列, 小于号:升序排列
-
temp_index = j;
-
// 交换已排序区的最后一个数字 和 未排序区的最大的数字
-
swap(a[temp_index], a[i]);
-
}
-
}
-
-
int main()
-
{
-
const int length = 20; // 数组长度
-
const int lower = 1; // 随机数上限
-
const int upper = 1000; // 随机数下限
-
int a[length];
-
-
// 初始化随机数种子
-
rand_seed();
-
-
// 初始化数组
-
for (int i = 0; i < length; i++)
-
a[i] = randInt(lower, upper);
-
-
// 输出数组
-
cout << "升序排列前的数组:" << endl;
-
print(a, length);
-
-
// 选择排序
-
selection_sort(a, length);
-
-
// 输出数组
-
cout << "升序排列后的数组:" << endl;
-
print(a, length);
-
-
return 0;
-
}
运行结果:
atoi_字符数组转换为整数函数
-
#include <iostream>
-
#include <cstdlib>
-
using namespace std;
-
-
int main()
-
{
-
char year1[]= "2009";
-
int intYear = atoi(year1);
-
cout << intYear << endl;
-
string year2 = "2010";
-
intYear = atoi(year2.c_str());
-
cout << intYear << endl;
-
return 0;
-
}
vector_平行向量
-
#include <iostream>
-
#include <string>
-
#include <vector>
-
using namespace std;
-
-
int main()
-
{
-
vector<string> names;
-
vector<double> prices;
-
vector<int> scores;
-
-
bool more = true;
-
string name;
-
double price;
-
int score;
-
string remainder, answer;
-
while (more)
-
{
-
cout << "Please enter the name: ";
-
cin >> name;
-
names.push_back(name);
-
cout << "Please enter the price: ";
-
cin >> price;
-
prices.push_back(price);
-
cout << "Please enter the score: ";
-
cin >> score;
-
scores.push_back(score);
-
getline(cin, remainder);
-
cout << "Do you want enter more information(y/n): ";
-
getline(cin, answer);
-
if (answer != "y" && answer != "Y")
-
more = false;
-
}
-
-
for (unsigned int i = 0; i < names.size(); i++)
-
cout << names[i] << " " << prices[i] << " " << scores[i] << endl;
-
return 0;
-
}
1+2+……+10^n
-
//题目描述:
-
//传说中数学王子高斯小时候利用规律很快的将1+2+……+100算出来
-
//现在有一个类似的问题,就是要求1+2+……+10^n.
-
//你能很快地算出来么?
-
//
-
//输入:
-
//多个case,每个case只有一个自然数 n (0<=n<=10000)
-
//
-
//输出:
-
//上面那个问题的结果
-
//
-
//样例输入:
-
//1
-
//2
-
//
-
//样例输出:
-
//55
-
//5050
-
-
-
#include "stdafx.h"
-
#include <iostream>
-
using namespace std;
-
-
int main()
-
{
-
int n;
-
void calculate(int n);
-
while (cin >> n)
-
calculate(n);
-
return 0;
-
}
-
-
void calculate(int n)
-
{
-
if (n == 0)
-
{
-
cout << "1" << endl;
-
return;
-
}
-
-
cout << "5";
-
-
int num_0 = n;
-
while (--num_0 > 0)
-
cout << "0";
-
-
cout << "5";
-
-
num_0 = n;
-
while (--num_0 > 0)
-
cout << "0";
-
-
cout << endl;
-
}
A + B Problem
-
//题目描述:
-
//A + B Problem
-
//
-
//输入:
-
//多组测试数据,第一行有两个小于1e9的整数,
-
//对这两个数求和,最后当EOF标志的时候结束。
-
//
-
//输出:
-
//对这两个数求和的结果
-
//
-
//样例输入:
-
//2 3
-
//1 9
-
//
-
//样例输出:
-
//5
-
//10
-
-
#include <iostream>
-
using namespace std;
-
-
int main()
-
{
-
int a, b;
-
while (cin >> a && cin >> b)
-
cout << a + b << endl;
-
return 0;
-
}