【Day4】坚持!!!

  • 1.将字符串char*转化为整数的函数atoi(ascii to integer)
  • 2.不要对string中的内容用循环直接进行修改操作,最好用迭代器
  • 3.C中string转char*的函数 str.c_str()及其本质探索
  • 4.万能头文件#include <bits/stdc++.h>
  • 5.对字符串替换数字时特别易错的是要替换为‘1’而不是 1
  • 6.将字符串转化为小数的atof(ascii to float)以及转化的小数的精度的控制
  • 7.使用gap方法定义任意位数上的进制问题
  • 8.暑假一定要刷熟练的算法

    贪心,递归,动态规划(背包)

    图,树

    网络流

  • 9.string类特别奇怪的一个错误(cin >> str覆盖实效)及原因分析

1.对于一个char*类型的字符数组,如果里面全都是整型数字字符,只需要调用atoi即可

atoi (表示 alphanumeric to integer)是把字符串转换成整型数的一个函数,应用在计算机程序和办公软件中。
  函数说明:
  参数nptr字符串,如果第一个非空格字符(前面可以有空格,但不能全是空格)存在,是数字或者正负号则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。否则,返回零。
  包含在头文件stdlib.h中。
  函数说明:
  参数nptr字符串,如果第一个非空格字符(前面可以有空格,但不能全是空格)存在,是数字或者正负号则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。否则,返回零。
  包含在头文件stdlib.h中。
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
	char str[] ="today20180716";
	int num = atoi(str);
	cout << num;
	return 0;
}

#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
	char str[] ="2018.0716";
	int num = atoi(str);
	cout << num;
	return 0;
}

#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
	char str[] ="       20180716";
	int num = atoi(str);
	cout << num;
	return 0;
}

2.不能对string变量里面的元素用自己的方法直接修改

int s_to_i(string a){
	int size = a.size() , sum = 0;
	for(int i = 0 ; i < size ; i++){
		sum += 10*sum + (int)(a[i] - '0'); 
	}	 
	return sum;
}
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
int s_to_i(string);
int main(){
	string str1,str2;
	vector <int> num;
	int times = 0;
	while(cin >> str1 >> str2){
		int size = str1.size();
		string str1_max = str1;
		cout << "str1_max = " << str1_max << endl;
		string::iterator it = str1_max.begin();
		for( ; it != str1_max.end() ; it++){
			if((*it) == '?'){
				(*it) = 9;
			}
		}
//		num.push_back(pow(10,i));//记录下有差异的数量级 
		if(str1_max > str2){//有可能存在str1>str2的情况 
			cout << "str1_max =" << str1_max << endl;
			int temp_num = s_to_i(str1_max);
			cout <<"max_num = " << str1_max << endl; 
		}
		else{// 不存在str1 > str2的情况 
			cout << '0' << endl; 
		} 
	}
	return 0;
}
int s_to_i(string a){
	int size = a.size() , sum = 0;
	for(int i = 0 ; i < size ; i++){
		sum += 10*sum + (int)(a[i] - '0'); 
	}	 
	return sum;
}

3.C++中字符串转指针的函数str.c_str()本质就是返回这段空间的首地址

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str=" today20180716";
	cout <<(int)(str.c_str()) << endl;
	char* p = &str[0];
	cout << (int)(p);
	return 0;
}

输出结果为

可见是同一个东西

4.万能头文件 

#include <bits/stdc++.h>

5.字符数组处理易错点

这里经常容易将'9'写为9

6.在c++中要引入头文件和setprecision(len)来控制输出位数

因为在C语言中printf的话可以用%lf来实现,你已经告诉了系统你输出几位。
但是在C++中,cout默认精度是6位。需要设置精度才能输出你想要的位数。
设置精度方法是cout<<fixed <<setprecision(n),n为需要输出的位数C语言中printf的话可以用%lf来实现,你已经告诉了系统你输出几位。
但是在C++中,cout默认精度是6位。需要设置精度才能输出你想要的位数。
设置精度方法是cout<<fixed <<setprecision(n),n为需要输出的位数
 

#include <bits/stdc++.h>
using namespace std;
int main(){
	char str[] ="      8555.071553346";
	double num = atof(str);
	cout << fixed << setprecision(4) << num;
	return 0;
}

7.使用gap方法解决非等进制问题

如果不同数位之间进制不同,可以按照下面这种代码定义一个gap数组来控制进制

#include <bits/stdc++.h> 
//编码思想解决 
using namespace std;
vector <int> power;
vector <int> gap; //通用进位器写法,可以改变gap来决定每位的进制(此题可以不用) 
void change(int*,int);
int main(){
	char str1[20],str2[20];
	int times = 0;//用于记录有多少种满足要求 
	while(cin >> str1 >> str2){
		int size = strlen(str1);
		int current_power = size - 1;
		char str1_max[20];
		strcpy(str1_max,str1);
		for(int i = 0 ; i < size ; i++){
			if(str1[i] == '?'){
				str1_max[i] = '9';
				power.push_back(pow(10,current_power));
				gap.push_back(10); 
			}
			current_power--;
		}
		int max_num = atoi(str1_max);
		int com_num = atoi(str2);
		if(max_num > com_num){//存在str1 > str2的情况 
			int size_of_vec = gap.size();
			int code[size_of_vec];
			memset(code,0,sizeof(code));
			int temp_num = 0; 
			int up = pow(9,size_of_vec);
            for(int i = 0 ; i < up ; i++){
            	temp_num = max_num; 
				for(int j  = 0 ; j < size_of_vec ; j++){
            		temp_num -= code[i] * power[i];	
            	}
            	if(temp_num > com_num){
            		times++; 
            	}
            	change(code , size_of_vec);
            }
            cout << times << endl;
            times = 0;
		}
		else{// 不存在str1 > str2的情况 
			cout << '0' << endl; 
		} 
		power.clear();
		gap.clear();
	}
	return 0;
}
void change(int* p , int len){
	for(int i = 0 ; i < len - 1; i++){
		p[0]++;
		if(p[i] >= gap[i]){
			p[i] = 0;
			p[i+1]++; 
		} 
	}
}
 

9.string类特别奇怪的一个错误(cin >> str覆盖实效)及原因分析

写一道题遇到一个这样的简单的结构:

#include <iostream>
using namespace std;
int main(){
	string order;
	while(cin >> order){
		cout << order << endl;
	}
	return 0;
}

其完整版是这样的:

#include <iostream>
#include <string>
using namespace std;
int main(){
	int m1 = 0 , m2 = 0 , r1 = 0, r2 = 0 , r3 = 0;
	string order;
	while(cin >> m1 >> m2 >> order){
		int size = order.size();
		for(int i = 0 ; i < size ; i++){
			switch(order[i]){
				case 'A':{
					r1 = m1;
					break;
				} 
				case 'B':{
					r2 = m2;
					break;
				} 
				case 'C':{
					m1 = r3;
					break;
				}
				case 'D':{
					m2 = r3;
					break;
				}
				case 'E':{
					r3 = r1 + r2;
					break;
				}
				case 'F':{
					r3 = r1 - r2;
					break;
				}
			}
		}
		cout << m1 << "," << m2 << endl;
		order.clear();//开始没有这一行,有一组数据没有过
	}
	return 0;
}

这个地方有很大的问题,就是开始没有加上order.clear()的时候,我觉得逻辑很对,但却有1组数据(共5组)没有过

然而加上order.clear()的时候,我就过了,按理说string是会在cin>>的过程中自动清空原来的string的

目前无法解释,当我想到后,我会在这里记下

——————————————————

这个问题我现在已经想明白了:

就是因为在字符串的比较方法中,应该使用“”而不是''即使字符串中仅有一个字符

猜你喜欢

转载自blog.csdn.net/chenhanxuan1999/article/details/81059505