夏季小学期第一周

欢迎讨论。只要方法正确,这些题十几行就能打完。要多多交流和思考。

我也得亏高人指点,不然太拉了,还以为自己写出来挺厉害。虽然现在改完感觉还是不够靓。

以前一些结构体排序应该可以改用map写,还不是很会,以后再说,反正题不会做得完。

emm,我这样直接上代码,可能几天后就忘了,不过我好懒的。

2. 错误的里程表

更新内容,删除冗余部分,一个循环搞定

#include <iostream>
using namespace std; 
int main(int argc, char** argv) 
{
	int T;
	cin>>T;
	for(int k=0;k<T;k++){
		long long int m=0;
		int sum=0,b=1,a=0;
		cin>>m;
	    while(m>0){
	    	a=m%10;
	    	if(a>=3&&a<8) a-=1;
	    	else if(a>=8) a-=2;
	    	sum+=a*b;
	    	b*=8;
	    	m=m/10;
		}    
		cout<<sum<<endl; 
	}
	return 0;
}

6. 世界杯来了

更新内容,找两只队伍的代码段优化。

#include<bits/stdc++.h> 
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct team{
	string name;
	int pure_score=0;
	int points=0;
	int score=0;//初始化很重要 
};
bool cmp1(const team& x,const team& y);
bool cmp2(const team& x,const team& y);
int main(int argc, char** argv) 
{
	int n;cin>>n;team X[n]; 
	for(int i=0;i<n;i++){
		cin>>X[i].name;
	} 
	for(int i=0;i<(n*(n-1))/2;i++){
		string x,y;int a=0,b=0,c=0,d=0;
		cin>>x>>y;
        int pos=x.find('-');
        string xx=x.substr(0,pos);
        string yy=x.substr(pos+1);
        for(int j=0;j<n;j++){
        	if(xx==X[j].name)
        	a=j;
        	if(yy==X[j].name)
        	b=j;
		}
		c=y[0]-'0';d=y[2]-'0';
		if(c>d){
			X[a].points+=3;
		}
		if(c==d){
			X[a].points+=1;
			X[b].points+=1;
		}
		if(c<d){
			X[b].points+=3;
		}
		    X[a].score+=c;
			X[b].score+=d;
			X[a].pure_score+=c-d;
			X[b].pure_score+=d-c; 
		}
	
	sort(X,X+n,cmp1);
	sort(X,X+(n/2),cmp2);
	for(int i=0;i<n/2;i++)
	cout<<X[i].name<<endl;
	return 0;
}
bool cmp1(const team& x,const team& y){
	if(x.points>y.points)
	return true;
	else if(x.points==y.points&&x.pure_score>y.pure_score)
	return true;
	else if(x.points==y.points&&x.pure_score==y.pure_score&&x.score>y.score)
	return true;
	else return false;
}
bool cmp2(const team& x,const team& y){
	if(x.name<y.name)
	return true;
	else
	return false;
}

9. 二叉树遍历,从前序、中序到后序

更新内容:截取字符串函数使用库函数,不自己写了

#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void find(string x,string y,string &xx);
int main(int argc, char** argv) 
{
	while(1){
		int n;
		cin>>n;
		if(n==0) break;
		else{
			string x,y,xx;//x是前序,y是中序遍历 
			cin>>x>>y;
			xx.clear();
			find(x,y,xx);
			for(int i=xx.size()-1;i>=0;i--)
			cout<<xx[i];
            cout<<endl;
		}
	}
	return 0;
}
void find(string x,string y,string& xx){
	//x是前序,y是中序 
	int a=y.find(x[0]) ;
	xx.push_back(y[a]);
	string n,m,c,d;
	n=y.substr(0,a);
	m=y.substr(a+1);
	d=x.substr(a+1);
	c=x.substr(1,a+1);
	if(!m.empty())find(d,m,xx);
	if(!n.empty())find(c,n,xx);
    return ;
}

19. 循环数

更新内容:选择直接把两串数字append,在于其中find有没有目标子串。

#include<bits/stdc++.h> 
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
string fight(string x,int y);
int main(int argc, char** argv) 
{
	string x,y;
	cin>>x;
	//cout<<x.size()<<endl;
    for(unsigned int j=1;j<=x.size();j++){
    	y=fight(x,j); 
    	string z;
    	y.append(y);
    	int a=y.find(x);
    if(a==-1){
    	cout<<"No"<<endl;
    	return 0;
	}
}cout<<"Yes"<<endl;
	return 0;
}
string fight(string x,int y){
	int c=0;string xx;
		for(int i=x.size()-1;i>=0;i--){
		int a=x[i]-'0';
	    int b=a*y+c; 
		char d=b%10+'0';
		xx.push_back(d);
		c=b/10;
	}
	reverse(xx.begin(),xx.end());
	return xx;
}

10. 内存管理

这题是舍友给提供的高级思路。然后我也没直接抄。自己打了一遍。结果找了一个钟的bug

下面说一下问题,收获还是挺多的

1.alloc的时候,输入的第二个数据是大小,而我find函数找到pos可能比这个大小还大。类似20题电源消耗那种坑。

2.defragment的时候,辅助字符串temp只可以这样 temp.push_back(x[j]);不可以temp【a】=x【j】

不知道为什么,可能是因为temp里面没东西吧。

3.erase的时候贪了一步,直接char a,cin>>a,然后直接x.find(a);这不是找死吗?首先输入的是两位数,不就越界裂开了嘛,然后就是负数的时候出来了奇奇怪怪的bug,循环自动跳了一步。我也是拿数据才发现这个bug。真是搞死我了。做人不能贪。

再次感谢舍友大大的思路,你才是真正的hero,我只是hero的搬运工,小弟。

#include <bits/stdc++.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(){
	int t,m;
	
	cin>>t>>m;
    string x;char count='0';
    for(int i=0;i<m;i++){
    	x.push_back('0');
	}
	for(int i=0;i<t;i++){
		string z,y;
		cin>>z;

		if(z=="alloc"){
			int a;cin>>a;
			for(int j=0;j<a;j++){
        	y.push_back('0');
		}
		int pos=x.find(y); 
		if(pos==-1)cout<<"NULL"<<endl;
		else {
			count++;
			cout<<count<<endl;
			for(int j=pos;j<pos+a;j++){
				x[j]=count;
			}
		}
		}
		else if(z=="erase"){
			int b;
			cin>>b; 
			if(b<=0){
				cout<<"ILLEGAL_ERASE_ARGUMENT"<<endl;
				continue;
			}
			char a=b+'0';
			int pos=x.find(a);
			if(pos==-1) 
			cout<<"ILLEGAL_ERASE_ARGUMENT"<<endl;
			else {
				while(x[pos]==a){
					x[pos]='0';pos++;
				}
			}
		}
		else if(z=="defragment"){
			string temp;
			int a=0,b=x.size();
			for(unsigned int j=0;j<x.size();j++){
				if(x[j]!='0')
				{
					temp.push_back(x[j]);a++;
				}
			}
			x=temp;
			for(int j=a;j<b;j++)
			x.push_back('0');//右补0 
		}
	}
}

ee

8. 买房与选房

处理同位次的人的思路还是太乐色了。失望。

#include<bits/stdc++.h>
using namespace std;
struct people2
{
    char id[19];                  /* 身份证号码 */
    int social;                     /* 社保缴纳月数 */
    int area;                       /* 现有住房面积 */
    char date[11];              /* 申报日期 */
};
struct people
{
    char id[19];                  /* 身份证号码 */
    int social;                     /* 社保缴纳月数 */
    int area;                       /* 现有住房面积 */
    char date[11];              /* 申报日期 */
    bool flag=0;
    int count=0;
};
people2* getMess(int &n);//yes
bool cmp(const people& x,const people& y);//
bool judge2(const people& x,const people& y);//yes?
void copyu(people *person,people2 *person2,int n);//yes
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    people2 *person2;          /* 指向所有报名人的基本资料首地址,通过调用函数getMess获取 */
    int n;                            /* n为报名人数,通过调用函数getMess获取 */
    person2=getMess(n);
    people *person=new people[n];

    copyu(person,person2,n);
    sort(person,person+n,cmp);
    for(int i=0; i<n-1; i++)
    {
        if(judge2(person[i],person[i+1]))
        {
            person[i].flag=1;
            person[i+1].flag=1;
        }
    }
    int count=1;

    for(int i=0; i<n-1; i++)
    {
        if(person[i].flag==1)
        {
            if(judge2(person[i],person[i+1]))
            {
                person[i].count=person[i+1].count=count;
            }
            else
            {
                count++;
            }
        }
    }
    int m,T;
    cin>>m>>T;

    for(int i=0; i<T; i++)
    {
        char ch[19];
        cin>>ch;
        int a=0;
        for(int j=0; j<n; j++)
        {
                if(strcmp(person[j].id,ch)==0)
            {
                a=j;//必找得到
                break;
            }
        }
        if(person[a].area==0&&person[a].social<=24)
        {
            cout<<"Sorry"<<endl;//没买房子资格
            continue;
        }
        int sum=0;
        int l=n;
        for(int j=0; j<n; j++)
        {
            if(person[j].count==person[a].count)
            {
                if(j<l) l=j;//也可能找到0的情况
                sum++;
            }
        }
        if(l+1>m)
        {
            cout<<"Sorry"<<endl;//最好情况也超出了房子套数
            continue;
        }
        else
        {
            if(person[a].flag==0&&a+1<=m)
            {
                cout<<a+1<<endl;
                continue;//没有同顺位。
            }
            if(person[a].flag==0&&a+1>m)
            {
                cout<<"Sorry"<<endl;//single并且超出顺位
                continue;
            }
            if(l+sum<=m)
            {
                cout<<l+1<<" "<<l+sum<<endl;
            }
            else
            {
                int C=l+sum-m;
                int A=sum-C;
                cout<<A<<"/"<<sum<<endl;
            }
        }
    }
    return 0;
}
bool judge2(const people& x,const people& y)
{
	if(x.area==0&&y.area==0) {
		if(strcmp(x.date,y.date)==0&&x.social==y.social)
	    return true;
	}
	
	else {
		if(strcmp(x.date,y.date)==0&&x.area==y.area)
		return true;
	}
    return false;
}
bool cmp(const people& x,const people& y)
{
    int flag1=0,flag2=0,xflagx=1,yflagx=1;
    if(x.area==0&&x.social<=24)xflagx=0;
    if(y.area==0&&y.social<=24)yflagx=0;
    if(xflagx>yflagx) return true;
    else if(xflagx<yflagx) return false;

    if(x.area==0&&x.social>24)
    {
        flag1=1;
    }
    if(y.area==0&&y.social>24)
    {
        flag2=1;
    }
    if(flag1>flag2)return true;
    else if(flag1<flag2)return false;
    else
    {
        if(flag1==1&&x.social>y.social)
            return true;
        else if(flag1==0&&x.area<y.area)
            return true;
        else if(flag1==1&&x.social<y.social)
            return false;
        else if(flag1==0&&x.area>y.area)
            return false;
    }
    string a[3],b[3];
    int c=0;
    for(int i=0; x.date[i]!='\0'; i++)
    {
        if(x.date[i]=='-')
        {
            c++;
        }
        else
        {
            a[c].push_back(x.date[i]);
            b[c].push_back(y.date[i]);
        }
    }
    return a[2]<b[2];
    return a[0]<b[0];
    return a[1]<b[1];
    return false;
}
people2* getMess(int &n)            /* 将文件数据读入内存 */
{
    FILE *fp;

    fp=fopen("house.bin","rb");

    fseek(fp,-1*(long)sizeof(int), 2);

    fread(&n, sizeof(int),1, fp);

    rewind(fp);

    people2 *tmp=new people2[n];

    fread(tmp, sizeof(people2), n, fp);

    fclose(fp);

    return tmp;
}
void copyu(people* person,people2* person2,int n)
{
    for(int i=0; i<n; i++)
    {
        person[i].area=person2[i].area;
        person[i].social=person2[i].social;
        strcpy(person[i].date,person2[i].date);
        strcpy(person[i].id,person2[i].id);
    }
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值