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;
}

二分查找

 二分查找函数:

  1. // 对于传入的以升序排列数组参数a,它以,则在下标为from和to之间查找值为num的数,找的则返回其下标;否则,返回-1
  2. int binary_search(const int a[], int from, int to, int num)
  3. {
  4.         if (from > to)
  5.                 return -1;
  6.  
  7.         int mid = (from + to) / 2;
  8.         if (a[mid] == num)
  9.                 return mid;
  10.         else if (a[mid] < num)
  11.                 return binary_search(a, mid + 1, to, num);
  12.         else
  13.                 return binary_search(a, from, mid - 1, num);
  14. }

 

完整代码: 

  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. // 设置随机整数的种子
  7. void rand_seed()
  8. {
  9.         unsigned int seed = static_cast<unsigned int>(time(NULL));
  10.         srand(seed);
  11. }
  12.  
  13. // 返回一个介于lower与upper之间的一个随机整数
  14. int randInt(int lower, int upper)
  15. {
  16.         return lower + rand() % (upper - lower + 1);
  17. }
  18.  
  19. void print(const int a[], const int length)
  20. {
  21.         for (int i = 0; i < length; i++)
  22.                 cout << a[i] << " ";
  23.         cout << endl;
  24. }
  25.  
  26. // 对于传入的以升序排列数组参数a,它以,则在下标为from和to之间查找值为num的数,找的则返回其下标;否则,返回-1
  27. int binary_search(const int a[], int from, int to, int num)
  28. {
  29.         if (from > to)
  30.                 return -1;
  31.  
  32.         int mid = (from + to) / 2;
  33.         if (a[mid] == num)
  34.                 return mid;
  35.         else if (a[mid] < num)
  36.                 return binary_search(a, mid + 1, to, num);
  37.         else
  38.                 return binary_search(a, from, mid - 1, num);
  39. }
  40.  
  41. int main()
  42. {
  43.         const int length = 20;
  44.         const int lower = 1// 随机数上限
  45.         const int upper = 20// 随机数下限
  46.         int a[length];
  47.  
  48.         rand_seed();
  49.         a[0] = 11;
  50.         for (int i = 1; i < length; i++)
  51.                 a[i] = a[i - 1] + randInt(lower, upper);
  52.  
  53.         print(a, length);
  54.  
  55.         int num;  //  待查找的数字
  56.         while (cin >> num)
  57.         {
  58.                 int temp_index = binary_search(a, 0, length, num);
  59.                 if (temp_index != -1)
  60.                         cout << "a[" << temp_index << "] = " << a[temp_index] << endl;
  61.                 else
  62.                         cout << "未找到" << endl;
  63.         }
  64.         return 0;
  65. }

顺序查找

 

  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. // 设置随机整数的种子
  7. void rand_seed()
  8. {
  9.         unsigned int seed = static_cast<unsigned int>(time(0));
  10.         srand(seed);
  11. }
  12.  
  13. // 返回一个介于lower与upper之间的一个随机整数
  14. int randInt(int lower, int upper)
  15. {
  16.         return lower + rand() % (upper - lower + 1);
  17. }
  18.  
  19. void print(const int a[], const int length)
  20. {
  21.         for (int i = 0; i < length; i++)
  22.                 cout << a[i] << " ";
  23.         cout << endl;
  24. }
  25.  
  26. // 如果待查找数字存在于数组中,则返回其下标;否则返回-1
  27. int linear_search(const int a[], const int length, int num)
  28. {
  29.         for (int i = 0; i < length; i++)
  30.         {
  31.                 if (a[i] == num)
  32.                         return i;
  33.         }
  34.         return -1;
  35. }
  36.  
  37. int main()
  38. {
  39.         const int length = 20;
  40.         const int lower = 1// 随机数上限
  41.         const int upper = 1000// 随机数下限
  42.         int a[length];
  43.         int num;  //  待查找的数字
  44.  
  45.         for (int i = 0; i < length; i++)
  46.                 a[i] = randInt(lower, upper);
  47.  
  48.         print(a, length);
  49.  
  50.         cout << "请输入要查找的数字:";
  51.         while (cin >> num)
  52.         {
  53.                 int num_index = linear_search(a, length, num);
  54.                 if (num_index != -1)
  55.                         cout << "a[" << num_index << "] = " << a[num_index] << endl;
  56.                 else
  57.                         cout << "未找到" << endl;
  58.                 cout << "请输入要查找的数字:";
  59.         }
  60.  
  61.         return 0;
  62. }

 

 

产生介于某一返回的随机数的函数

 

  1. // 返回一个介于lower与upper之间的随机数
  2.  
  3. #include <cstdlib>
  4.  
  5. int rand_int(int lower, int upper)
  6. {
  7.         return lower + rand() % (upper - lower + 1);
  8. }

 

 

选择排序

 

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. // 设置随机整数的种子
  7. void rand_seed()
  8. {
  9.         unsigned int seed = static_cast<unsigned int>(time(0));
  10.         srand(seed);
  11. }
  12.  
  13. // 返回一个介于lower与upper之间的一个随机整数
  14. int randInt(int lower, int upper)
  15. {
  16.         return lower + rand() % (upper - lower + 1);
  17. }
  18.  
  19. // 交换两个数的值
  20. void swap(int &a, int &b)
  21. {
  22.         int temp = a;
  23.         a = b;
  24.         b = temp;
  25. }
  26.  
  27. void print(const int a[], int length)
  28. {
  29.         for (int i = 0; i < length; i++)
  30.                 cout << a[i] << " ";
  31.         cout << endl;
  32. }
  33.  
  34. // 函数_选择排序_升序排列
  35. void selection_sort(int a[], int length)
  36. {
  37.         int i, j, temp_index;
  38.         for (i = 0; i < length - 1; ++i)
  39.         {
  40.                 temp_index = i;  // 存储未排序区中最大值的索引
  41.                 for (j = i + 1; j < length; ++j)
  42.                         if (a[j] < a[temp_index])  // 大于号:降序排列, 小于号:升序排列
  43.                                 temp_index = j;
  44.                 // 交换已排序区的最后一个数字 和 未排序区的最大的数字
  45.                 swap(a[temp_index], a[i]);
  46.         }
  47. }
  48.  
  49. int main()
  50. {
  51.         const int length = 20// 数组长度
  52.         const int lower = 1// 随机数上限
  53.         const int upper = 1000// 随机数下限
  54.         int a[length];
  55.  
  56.         // 初始化随机数种子
  57.         rand_seed();
  58.  
  59.         // 初始化数组
  60.         for (int i = 0; i < length; i++)
  61.                 a[i] = randInt(lower, upper);
  62.  
  63.         // 输出数组
  64.         cout << "升序排列前的数组:" << endl;
  65.         print(a, length);
  66.  
  67.         // 选择排序
  68.         selection_sort(a, length);
  69.  
  70.         // 输出数组
  71.         cout << "升序排列后的数组:" << endl;
  72.         print(a, length);
  73.  
  74.         return 0;
  75. }

运行结果:

atoi_字符数组转换为整数函数

 

 

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.         char year1[]= "2009";
  8.         int intYear = atoi(year1);
  9.         cout << intYear << endl;
  10.         string year2 = "2010";
  11.         intYear = atoi(year2.c_str());
  12.         cout << intYear << endl;
  13.         return 0;
  14. }

vector_平行向量

 

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.         vector<string> names;
  9.         vector<double> prices;
  10.         vector<int> scores;
  11.  
  12.         bool more = true;
  13.         string name;
  14.         double price;
  15.         int score;
  16.         string remainder, answer;
  17.         while (more)
  18.         {
  19.                 cout << "Please enter the name: ";
  20.                 cin >> name;
  21.                 names.push_back(name);
  22.                 cout << "Please enter the price: ";
  23.                 cin >> price;
  24.                 prices.push_back(price);
  25.                 cout << "Please enter the score: ";
  26.                 cin >> score;
  27.                 scores.push_back(score);
  28.                 getline(cin, remainder);
  29.                 cout << "Do you want enter more information(y/n): ";
  30.                 getline(cin, answer);
  31.                 if (answer != "y" && answer != "Y")
  32.                         more = false;
  33.         }
  34.  
  35.         for (unsigned int i = 0; i < names.size(); i++)
  36.                 cout << names[i] << " " << prices[i] << " " << scores[i] << endl;
  37.         return 0;
  38. }

1+2+……+10^n

 

  1. //题目描述:
  2. //传说中数学王子高斯小时候利用规律很快的将1+2+……+100算出来
  3. //现在有一个类似的问题,就是要求1+2+……+10^n.
  4. //你能很快地算出来么?
  5. //
  6. //输入:
  7. //多个case,每个case只有一个自然数 n (0<=n<=10000)
  8. //
  9. //输出:
  10. //上面那个问题的结果
  11. //
  12. //样例输入:
  13. //1
  14. //2
  15. //
  16. //样例输出:
  17. //55
  18. //5050
  19.  
  20.  
  21. #include "stdafx.h"
  22. #include <iostream>
  23. using namespace std;
  24.  
  25. int main()
  26. {
  27.         int n;
  28.         void calculate(int n);
  29.         while (cin >> n)
  30.                 calculate(n);
  31.         return 0;
  32. }
  33.  
  34. void calculate(int n)
  35. {
  36.         if (n == 0)
  37.         {
  38.                 cout << "1" << endl;
  39.                 return;
  40.         }
  41.  
  42.         cout << "5";
  43.        
  44.         int num_0 = n;
  45.         while (--num_0 > 0)
  46.                 cout << "0";
  47.  
  48.         cout << "5";
  49.  
  50.         num_0 = n;
  51.         while (--num_0 > 0)
  52.                 cout << "0";
  53.  
  54.         cout << endl;
  55. }

 

A + B Problem

 

  1. //题目描述:
  2. //A + B Problem
  3. //
  4. //输入:
  5. //多组测试数据,第一行有两个小于1e9的整数,
  6. //对这两个数求和,最后当EOF标志的时候结束。
  7. //
  8. //输出:
  9. //对这两个数求和的结果
  10. //
  11. //样例输入:
  12. //2 3
  13. //1 9
  14. //
  15. //样例输出:
  16. //5
  17. //10
  18.  
  19. #include <iostream>
  20. using namespace std;
  21.  
  22. int main()
  23. {
  24.         int a, b;
  25.         while (cin >> a && cin >> b)
  26.                 cout << a + b << endl;
  27.         return 0;
  28. }




Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee