2019暑期集训第一周tips

1

在逻辑判断语句中如果(或)|| 的左侧语句为 true 则不会再执行右侧语句,右侧语句中对变量的操作也不会进行

2

注意题目数据量,如果数据量大尽量使用scanf(别忘了&)

3

输入多组a,b,如果a=0并且b=0表示输入的结束,不做处理。
不可用

while(cin>>a>>b&&a!=0&&b!=0){}//当a=0或者b=0时循环就退出了

应该使用

while(cin>>a>>b&&(a!=0||b!=0)){}

或者使用

while(cin>>a>>b){
		if(a==0&&b==0)break;
		......
}

4

文件操作

freopen(".in","r",stdin);//从文件输入
freopen(".out"."w",stdout);//输出到文件

5

算阶乘和时,25!末尾有6个0,所以从第25项开始,后面所有的项都不会影响和的末6位数字

6

scanf函数的返回值是成功输入的变量个数。

7

数字和字符串之间的转化

1.使用sprintf()和sscanf()

sprintf() 多组数字连接成变为字符串(以数字形式读入,输出字符串)

#include"bits/stdc++.h"
using namespace std;
int main(){
	int num;
	char str[25];
	string a;//用string实现字符串之间的连接
	while(cin>>num){
		sprintf(str, "%d" , num);//把格式化的数据写入某个字符串中,第一个参数要求是char数组
		a+=str;//string类型和char数组也可以直接用+连接
	}
	cout<<a;
}

sscanf() 用于将字符串转化为数字


#include"bits/stdc++.h"
using namespace std;
int main(){
    char str[]="1234321"; 
    int a; 
    sscanf(str,"%d",&a); //和sprintf一样第一个参数要求是char数组
    cout<<a<<endl;

    char str1[]="123.321"; 
    double b; 
    sscanf(str1,"%lf",&b); 
    cout<<b<<endl;
    return 0;
}

2.使用stringstream只适用于C++中的string类型,否则无法使用输入输出流。

多组数字连接成字符串

#include"bits/stdc++.h"
using namespace std;
int main(){
    double a ;
    string res;
    stringstream ss; 
    while(cin>>a){      
    	ss << a;                                       
	}
	ss>>res;
	cout<<res;
}

字符串转换为数字

#include"bits/stdc++.h"
using namespace std;
  int main(){
    double a ;
    string res= "123.32";
    stringstream ss;  
    ss << res;                  
    ss >> a;
    coout<<a+1;
    return 0;
  }
  //输出结果为124.32

8

memset(a, 0, sizeof(a));
第一个参数是需要赋值的数组
第二个参数是赋值数值,可以是字符,数字0和-1
第三个参数是大小

9

strstr:查找字串(strchr为查找字符)
•在字符串中查找指定字符串的第一次出现
•char *strstr(char *str1, char *str2);//返回值是指针

•找找子串”aa”在哪里?

int main(){
	char a[81]="aabbccaabbccabab", *p;
	p = strstr(a,"aa");//返回的是指针
	while (p!=NULL){
		int pos= p-a;
		printf("%d\n",pos);
		p = strstr(a+pos+2,"aa");//跳过已经找到的"aa",开始位置+2
	}
return 0;
}

10

c++中string字符串的操作:

substr() 求子串函数

#include"bits/stdc++.h"
using namespace std;
int main (){
	string s="hello, world!";
	string ss1 = s.substr(2);
	string ss2 = s.substr(2,3);
	cout<<ss1<<endl<<ss2<<endl;
}
//输出结果:
llo, world!
llo

erase() 删除字符函数(会改变原字符串)

#include"bits/stdc++.h"
using namespace std;
int main (){
	string s="hello, world!";
	string ss1 = s.erase(6);//删除第6个字符开始的所有字符
	string ss2 = s.erase(1,2);
	cout<<s<<endl<<ss1<<endl<<ss2<<endl;
}
//输出结果:
hlo,
hello,
hlo,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值