553C++笔试笔记2013

目录

1.质数筛

2.加密解密

3.选择排序和桶排序

4.输出一个asc码与8,16,10进制对照的表(iomanip第二次考到了)

5.字符串处理(第二次考到了)

关于atoi和stoi:

 关于strtok和strtok_s:

Strtok()函数详解:

strtok_s()函数

6.Person类


1.质数筛

#include<vector>
#include<iostream>
using namespace std;
#define MAXSIZE 20000
void getPrime(vector<bool>& numbers, vector<int>& primes) {
	for (int i = 2; i < MAXSIZE; i++) {
		if (numbers[i] = true)
			primes.push_back(i);
		for (int j = i; j < MAXSIZE; j += i)
			numbers[j] = false;
	}
}

int main() {
	vector<bool> num(MAXSIZE, true);
	vector<int> pri;
	getPrime(num, pri);
	int count = 0;
	for (vector<int>::iterator it = pri.begin(); it != pri.end(); it++) {
		cout << *it << " ";
		count++;
		if (count == 10) {
			cout << endl;
			count = 0;
		}
	}
	return 0;
}

        质数筛首先默认在该范围内的数字全是质数,是质数的全部入栈并且其倍数(k*i)全部不是质数。依靠该方法可依次排除2的倍数 3的倍数等等自然数,最后剩下的就是质数了。 

2.加密解密

这题感觉有点问题。

3.选择排序和桶排序

#include<iostream>
using namespace std;

void Sort1(int a[], int n) {
	for (int i = 0; i < n; i++) {
		int minPos = i;
		for (int j = i; j < n; j++) 
			if (a[j] < a[minPos])
				minPos = j;
			int temp = a[i];
			a[i] = a[minPos];
			a[minPos] = temp;
		
	}
	for (int i = 0; i < n; i++)
		cout << a[i]<<" ";
	cout << endl;
}
	void Sort2(int a[], int n){
		int count[65536] = { 0 };
		for (int i = 0; i < n; i++)
			count[a[i]]++;
		for (int j = 0; j < 65536; j++)
			for (int k = 1; k <= count[j]; k++)
				cout << j << " ";
	}

	int main() {
		int A[7] = { 4,5,6,3,34,23,76 };
		Sort1(A, 7);
		Sort2(A, 7);

}

这边有一个问题,这边其实就哈希了一下,跟网上搜到的桶排序好像完全没关系,附上链接4

桶排序与哈希桶排序 - 简书 (jianshu.com)https://www.jianshu.com/p/4c35cd8364f1

 

4.输出一个asc码与8,16,10进制对照的表(iomanip第二次考到了)

#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

void printD(ofstream& os) {
	int count = 0;
	os << "Decimal:\n";
	for (int i = 33; i <= 126; i++) {
		os << setw(4) << i << " ";
		count++;
		if (count == 10){
			count = 0;
			os << endl;
		}
	}
	os << endl;
}

void printO(ofstream& os) {
	int count = 0;
	os << "Octal:\n";
	for (int i = 33; i <= 126; i++) {
		os << setw(4) <<oct<<setfill('0') << i << " ";
		count++;
		if (count == 10) {
			count = 0;
			os << endl;
		}
	}
	os << endl;
}
void printH(ofstream& os) {
	int count = 0;
	os << "Hexadecimal:\n";
	for (int i = 33; i <= 126; i++) {
		os << setw(4) << hex << setfill('0') << i << " ";
		count++;
		if (count == 10) {
			count = 0;
			os << endl;
		}
	}
	os << endl;
}

void printASC(ofstream& os) {
	int count = 0;
	os << "ASC2:\n";
	for (int i = 33; i <= 126; i++) {
		os  << char(i) << " ";
		count++;
		if (count == 10) {
			count = 0;
			os << endl;
		}
	}
	os << endl;
}



int main() {
	ofstream os("D:/word.txt");
	if (!os) {
		cerr << "file cannot be opened!";
		exit(EXIT_FAILURE);
	}
	printD(os);
	printO(os);
	printASC(os);
	printH(os);

}

建立了文件和键盘的os流,即往文件中输出 

Decimal:
  33   34   35   36   37   38   39   40   41   42 
  43   44   45   46   47   48   49   50   51   52 
  53   54   55   56   57   58   59   60   61   62 
  63   64   65   66   67   68   69   70   71   72 
  73   74   75   76   77   78   79   80   81   82 
  83   84   85   86   87   88   89   90   91   92 
  93   94   95   96   97   98   99  100  101  102 
 103  104  105  106  107  108  109  110  111  112 
 113  114  115  116  117  118  119  120  121  122 
 123  124  125  126 
Octal:
0041 0042 0043 0044 0045 0046 0047 0050 0051 0052 
0053 0054 0055 0056 0057 0060 0061 0062 0063 0064 
0065 0066 0067 0070 0071 0072 0073 0074 0075 0076 
0077 0100 0101 0102 0103 0104 0105 0106 0107 0110 
0111 0112 0113 0114 0115 0116 0117 0120 0121 0122 
0123 0124 0125 0126 0127 0130 0131 0132 0133 0134 
0135 0136 0137 0140 0141 0142 0143 0144 0145 0146 
0147 0150 0151 0152 0153 0154 0155 0156 0157 0160 
0161 0162 0163 0164 0165 0166 0167 0170 0171 0172 
0173 0174 0175 0176 
ASC2:
! " # $ % & ' ( ) * 
+ , - . / 0 1 2 3 4 
5 6 7 8 9 : ; < = > 
? @ A B C D E F G H 
I J K L M N O P Q R 
S T U V W X Y Z [ \ 
] ^ _ ` a b c d e f 
g h i j k l m n o p 
q r s t u v w x y z 
{ | } ~ 
Hexadecimal:
0021 0022 0023 0024 0025 0026 0027 0028 0029 002a 
002b 002c 002d 002e 002f 0030 0031 0032 0033 0034 
0035 0036 0037 0038 0039 003a 003b 003c 003d 003e 
003f 0040 0041 0042 0043 0044 0045 0046 0047 0048 
0049 004a 004b 004c 004d 004e 004f 0050 0051 0052 
0053 0054 0055 0056 0057 0058 0059 005a 005b 005c 
005d 005e 005f 0060 0061 0062 0063 0064 0065 0066 
0067 0068 0069 006a 006b 006c 006d 006e 006f 0070 
0071 0072 0073 0074 0075 0076 0077 0078 0079 007a 
007b 007c 007d 007e 

5.字符串处理(第二次考到了)

#include<string>
#include<iostream>
#include<iomanip>
using namespace std;

int main() {
	char s[] = "(025)87234865-987";
	char* temp;
	char* p=NULL;
	temp = strtok_s(s + 1, ")",&p);
	int area = atoi(temp);//转换类型

	temp = strtok_s(NULL, "-", &p);
	int telephone = atoi(temp);

	temp = strtok_s(NULL, "\0", &p);
	int divide = atoi(temp);

	cout << setw(3) << setfill('0') << area << " " << telephone << " " << divide << endl;

	return 0;
}

复看的时候发现,这题没必要转换成int型。

关于atoi和stoi:

atoi函数头文件是<cstdlib>,是c函数;而stoi头文件是<string>是c++的函数。

也正因为这一点,atoi的参数是const char*类型,假设我们定义的是string类型,想用atoi的话,就会报错了。而stoi在这方面做了重载,无论是const char*还是string,都是可以的。

 关于strtok和strtok_s:

Strtok()函数详解:

该函数包含在"string.h"头文件中

函数原型:

char* strtok (char* str,constchar* delimiters );

函数参数:
  str:在第一次被调用的时间str是传入需要被切割字符串的首地址;在后面调用的时间传入NULL
  delimiters:表示切割字符串(字符串中每个字符都会 当作分割符)。

函数返回值:
  当s中的字符查找到末尾时,返回NULL;
  如果查不到delimiter所标示的字符,则返回当前strtok的字符串的指针

strtok_s()函数

char *__cdecl strtok_s(char *_String, const char *_Delimiter, char **_Context)

多了一个参数,这个参数用于保存现在的切割位置

代码:

int main() {
	char s[] = "(025)87234865-987";
	cout << s<< endl;
	char* temp;
	char* p=NULL;
	temp = strtok_s(s + 1, ")",&p);
	int area = atoi(temp);//转换类型
	cout << p << endl;

	temp = strtok_s(NULL, "-", &p);
	int telephone = atoi(temp);
	cout << p << endl;

	temp = strtok_s(NULL, "\0", &p);
	int divide = atoi(temp);

	cout << setw(3) << setfill('0') << area << " " << telephone << " " << divide << endl;

	return 0;

	
}
(025)87234865-987
87234865-987   //p
987            //p
025 87234865 987

 

6.Person类

看过前面的小伙伴应该懂了,别无二致。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值