初始化数组为0
int flag[N+1][N+1];
memset(flag,false,sizeof(flag)); //memset(flag,0,sizeof(flag));
sscanf 读取格式化的字符串中的数据
int main()
{
int day, year;
char weekday[20], month[20], dtm[100];
strcpy( dtm, "Saturday March 25 1989" );
sscanf( dtm, "%s %s %d %d", weekday, month, &day, &year );
printf("%s %d, %d = %s\n", month, day, year, weekday );
return(0);
}
March 25, 1989 = Saturday
typedef struct
{
char date[100];
int grade;
}Record;
bool isValid(char* str)
{
int y,m,d;
sscanf(str,"%d/%d/%d",&y,&m,&d);
if(y<1996||y>2100)
...
return valid;
}
int main()
{
Record res[MAX];
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>res[i].date>>res[i].grade;
sort(res,res+n,cmp);
for(int i=0;i<n;i++)
{
if(isValid(res[i].date))
cout<<res[i].date<<" "<<res[i].grade<<endl;
}
return 0;
}
string
string s;
cin>>s;
//计算识别码
int sum = 0,j=1;
for(int i = 0;i<(int)s.length()-1;i++)//注意这里i<(int)s.length()-1,不要忘了-1
{
if(s[i]!='-')
{
sum+=(s[i]-'0')*j;
j++;
}
}
char数组
void mystrlwr(char a[])
{
int len = strlen(a);
//cout<<len<<endl;
for(int i=0;i<len;i++)
{
if(a[i]>='A' && a[i]<='Z')
a[i]=a[i]-'A'+'a';
}
}
void lowerStr(char *ps)
{
while(*ps)
{
if('A'<=*ps && *ps <='Z')
*ps+='a'-'A';
ps++;
}
}
int main()
{
char key[N+1],s[N+1],lowerKey[N+1],lowerS[N+1];
int flag,n;
//输入数据
cin>>key>>flag>>n;
//获得key的小写字符串,放在变量lowerKey中
strcpy(lowerKey,key);
lowerStr(lowerKey);
//循环处理
for(int i=1;i<=n;i++)
{
cin>>s;
if(flag==0){//大小写不敏感
strcpy(lowerS,s);
lowerStr(lowerS);
if(strstr(lowerS,lowerKey))
cout<<s<<endl;
}else{ //大小写敏感
if(strstr(s,key))
cout<<s<<endl;
}
}
return 0;
}
strcpy(lowerS,s) //s给lowers
strcpy(lowerS,s)//在s中找lowers,返回0或1
string.substr(beginindex,len)
void main()
6 {
7 string s("12345asdf");
8 string a=s.substr(0,5);//12345 从0位开始长度为5
9 cout<<a<<endl;
10 }
int转char
int sum = 0;
char c;
c=sum%11;
if(c==10)
c='X';
else
c=c+'0';
map
哈希原理映射
int valflag[2*N+1];
int n,v,minus,sum=0;
//变量初始化,清零
memset(valflag,0,sizeof(valflag));
//输入数据,判断负值是否已经存在,统计,标记
cin>>n;
for(int i=0;i<n;i++)
{
//输入数据
cin>>v;
//类似哈希原理
//值映射:从-1000<=v<=1000 映射为 0<=v<=2000
minus =-v+N; //该值对应的相反值 v的相反数的映射为 -v+1000
v+=N; //该值的映射 v的映射为V+1000
//判断负值是否已经存在
if(valflag[minus]==1) //如果该值得相反数存在
sum++;
//标记
valflag[v]=1; //将该值记录近数组
}
cout<<sum<<endl;
return 0;
int main()
{
int n;int no;
memset(noCount,0,sizeof(noCount));
//输入数据
cin>>n;
for(int i=0;i<n;i++)
{
cin>>no;
noCount[no]++;
//输出结果
if(i!=0)
cout<<" ";
cout<<noCount[no];
}
return 0;
}
动态规划
采用动态规划思想,每一次决策都基于前一次决策的最优解。
队列
2017-12-2
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
int main()
{
int n,k;
queue<int> q;
cin>>n>>k;
//队列初始化
for(int i=1;i<=n;i++)
q.push(i);
//模拟出局过程(队列先进先出)
int no=0,head;
while(!q.empty())
{
head=q.front();
cout<<"head="<<head<<endl;
q.pop();
no++;
if(no%k==0 || no % 10 == k)//k的倍数或末尾数为k淘汰
;
else
q.push(head);
}
cout<<head<<endl;
return 0;
}
优先队列
/* 统计排序问题
* 先统计后排序
* 使用STL包装类
*/
#include<iostream>
#include<map>
#include<queue>
using namespace std;
struct node
{
int key;
int kCount;
bool operator<(const node& n)const{
if(kCount==n.kCount)
return n.key<key;
else
return kCount<n.kCount;
}
};
int main()
{
priority_queue<node> q;
map<int,int> m;
int n,v;
//输入数据,构建map
cin>>n;
for(int i=0;i<n;i++){
cin>>v;
m[v]++;
}
//构建优先队列进行排序(优先队列默认是大根堆)
node keyval;
for(map<int,int>::iterator it = m.begin();it!=m.end();it++){
keyval.key=it->first;
keyval.kCount=it->second;
q.push(keyval);
}
//输出结果
while(!q.empty()){
keyval=q.top();
q.pop();
cout<<keyval.key<<" "<<keyval.kCount<<endl;
}
return 0;
}
跟日期相关
2015-03-3
#include<iostream>
using namespace std;
//月份天数
int monthdays[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
//计算闰年
int leapyear(int year){
return ((year%4==0 && year%100!=0)||year%400 ==0)?1:0;
}
int main()
{
int a,b,c,y1,y2,weekd,d,y;
//输入数据
cin>>a>>b>>c>>y1>>y2;
//计算1850年到起始年的
int days=0;
for(int i=1850;i<y1;i++)
days+=365+leapyear(i);
//计算年月日并输出
for(int i=y1;i<=y2;i++){
//计算是否是闰年
y=leapyear(i);
//计算从1850年开始到i年a月1日的天数
int days2=days;
for(int i=1;i<a;i++)
days2+=monthdays[y][i];
//计算i年a月1日的前一天为星期几
weekd=1+days2%7;
//计算i年的a月第b个星期c是当月的几号
d=(b-1)*7+((weekd>=c)?(c+7-weekd):(c-weekd));
//输出结果
if(d>monthdays[y][a])
cout<<"none"<<endl;
else{
cout<<i<<"/";
if(a<10)
cout<<"0";
cout<<a<<"/";
if(d<10)
cout<<"0";
cout<<d<<endl;
}
//为计算下一年做准备:计算从1850到下一年开始的天数
days+=365+leapyear(i);
}
return 0;
}
2015-09-2
#include<iostream>
using namespace std;
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
//闰年计算函数
int leapyear(int year)
{
return ((year%4==0 && year%100!=0)||year%400==0)?1:0;
}
int main()
{
int y,d,month=0,day,i;
//输入数据
cin>>y>>d;
//计算月与日
days[1]==leapyear(y);
i=0;
while(d>0){
if(d<=days[i]){
month=i+1;
day=d;
break;
}else{
d-=days[i];
i++;
}
}
//输出结果
cout<<month<<endl;
cout<<day<<endl;
return 0;
}
//求数位之和
while(n){
sum+=n%10;
n=n/10;
}
int isprime(int n)
{
for(int i=2;i*i<=n;i++){
if(n%i==0)
return 0;
}
return 1;
}
文件读取操作
#include<iostream>
#include<algorithm>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
typedef struct txt{
string name;
int p_num;
int p_score;
}txt;
int main()
{
txt stu[10000];
string s;
int i,j,n=0,t,tt;
ifstream infile("C:\\Users\\z18y\\Desktop\\Code-repository\\Score.txt",ios::in);
ofstream outfile("C:\\Users\\z18y\\Desktop\\Code-repository\\ordered.txt");
while(!infile.eof()){
infile>>stu[n].name>>stu[n].p_num>>stu[n].p_score;
n++;
}
cout<<"排名:"<<endl;
//冒泡排序,小的在后面
for(i=0;i<n;i++){
for(j=0;j<n-i;j++){
if(stu[j].p_num<=stu[j+1].p_num){
t=stu[j].p_num;
stu[j].p_num=stu[j+1].p_num;
stu[j+1].p_num=t;
}
if(stu[j].p_score<stu[j+1].p_score){
t=stu[j].p_score;
stu[j].p_score=stu[j+1].p_score;
stu[j+1].p_score=t;
s=stu[j].name;
stu[j].name=stu[j+1].name;
stu[j+1].name=s;
}
}
}
for(i=0;i<n;i++){
cout<<i+1<<" "<<stu[i].name<<" "<<stu[i].p_num<<" "<<stu[i].p_score<<endl;
outfile<<stu[i].name<<" "<<stu[i].p_num<<" "<<stu[i].p_score<<endl;
}
infile.close();
infile.clear();
outfile.close();
outfile.clear();
return 0;
}
//分组计算问题
//首先尽可能以5瓶一组来买,余下部分尽可能3瓶一组来买,最后剩下部分按10元一瓶来买
int main()
{
int n;
int group1,group2,group3;
cin>>n;
group1=n/PRICE/FIVE;
group2=(n-group1*FIVE*PRICE)/PRICE/THREE;
group3=(n-group1*FIVE*PRICE-group2*THREE*PRICE)/PRICE;
cout<<group1*7+group2*4+group3;
return 0;
}
2018-03-2
小球碰撞
#include<iostream>
#include<stdlib.h>
using namespace std;
const int L =1000;
const int N =100;
int pos[N+1],step[N+1];//存储小球的舒适位置和运动方向
int main()
{
int n,l,t;
cin>>n>>l>>t;
for(int i=0;i<n;i++){
cin>>pos[i];
//开始向右走,到达两端则回头
step[i]=1;
// if(pos[i]==l || pos[i]==0)
// step[i]=-step[i];
}
for(int i=0;i<t;i++){
//时间过了一秒,即每个球走一步
for(int j=0;j<n;j++){
pos[j]+=step[j];
//到达两端即回头
if(pos[j]==l || pos[j]==0)
step[j]=-step[j];
}
//判断这一时刻是否有碰头的球(要避免重复比较)
for(int j=0;j<n;j++)
for(int k=j+1;k<n;k++)
if(pos[k]==pos[j])
step[k]=-step[k],step[j]=-step[j];
}
//输出结果
for(int i=0;i<n;i++)
cout<<pos[i]<<" ";
cout<<endl;
return 0;
}
结构体比较
//记录结构体
typedef struct
{
char date[100];
int grade;
}Record;
//自定义排序规则
bool cmp(Record x,Record y)
{
if(x.grade!=y.grade)
return x.grade>y.grade;//x.grade>y.grade 返回1,x在前面 -1:x.grade<y.grade,x在后面(总之从大到小排)
return x.date>y.date;//x.date<y.daye 返回1 x在前面
}
sort(res,res+n,cmp);
栈
二十四点
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
int n;
char str[10];
stack<int> num;
stack<char> sign;
int main(){
scanf("%d",&n);
getchar();
for(int i=0;i<n;i++){
gets(str);
while(!num.empty()) num.pop();
while(!sign.empty()) sign.pop();
int j=0;
while(j<strlen(str)){
if(str[j]>'0' && str[j]<='9'){
num.push(str[j]-'0');
}
else{
if(str[j]=='+'){
sign.push('+');
}
else if(str[j]=='-'){ //将减法转换成加法
num.push((str[j+1]-'0')*(-1));
sign.push('+');
j++;
}
else if(str[j]=='x'){ //直接计算乘法
int lhs=num.top();
num.pop();
num.push(lhs*(str[j+1]-'0'));
j++;
}
else if(str[j]=='/'){ //直接计算除法
int lhs=num.top();
num.pop();
num.push(lhs/(str[j+1]-'0'));
j++;
}
}
j++;
}
while(!sign.empty()){ //计算剩余的加法
int rhs=num.top();
num.pop();
int lhs=num.top();
num.pop();
sign.pop();
num.push(lhs+rhs);
}
int ans=num.top();
if(ans==24) printf("Yes\n");
else printf("No\n");
}
return 0;
}