CCF 1-2题答题记录(2)

202006_1

//测试样例
/*
9 3
1 1 A
1 0 A
1 -1 A
2 2 B
2 3 B
0 1 A
3 1 B
1 3 B
2 0 A
0 2 -3
-3 0 2
-3 1 1
*/


//#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

//判断点(xi,yi)与直线Ax+By+C=0的几何位置关系
int Pos(int C,int A,int B,int xi,int yi){
    if(A==0){ //直线方程式:y=-C/B
        float y0=-C/B;
        if((yi-y0)>0){
            return 1; //点在线上方
        }
        else{
            return -1; //点在线下方(因为点线不会重合)
        }
    }
    else{ //线不垂直于坐标轴,Ax+By+C=0
        float x0=-(C+B*yi)/A; //过点的水平线与直线交点的横坐标
        if((xi-x0)>0){
            return 2; //点在线右边
        }
        else{
            return -2; //点在线左边
        }
    }
}

int main() {
    int n,m;
    scanf("%d %d",&n,&m);
    int xi,yi;char typei;
    vector<int> x;
    vector<int> y;
    vector<char> eleType;
    for(int i=0;i<n;++i){
        cin>>xi;x.push_back(xi);
        cin>>yi;y.push_back(yi);
        cin>>typei;eleType.push_back(typei);
    }
    vector<int> eleC,eleA,eleB;
    int C,A,B;
    for(int i=0;i<m;++i){
        scanf("%d %d %d",&C,&A,&B);
        eleC.push_back(C);
        eleA.push_back(A);
        eleB.push_back(B);
    }
    for(int i=0;i<m;++i){
        vector<int> value;
        for(int j=0;j<n;++j){
            int resj = Pos(eleC[i],eleA[i],eleB[i],x[j],y[j]);
            value.push_back(resj);
        }
        vector<int> A_value;
        vector<int> B_value;
        for(int i=0;i<n;++i){
            //cout<<"value[i]="<<value[i]<<" ";
            if(eleType[i]=='A'){
                A_value.push_back(value[i]);
            }
            else{
                B_value.push_back(value[i]);
            }
        }
        sort(A_value.begin(),A_value.end());
        sort(B_value.begin(),B_value.end());
        if( ((A_value.front()+A_value.back()) == 0) || ((B_value.front()+B_value.back()) == 0)){
            cout<<"No"<<endl;
        }
        else{
            cout<<"Yes"<<endl;
        }
    }
    return 0;
}

202006_2

//样例输入
/*
10 3 4
4 5
7 -3
10 1
1 10
4 20
5 30
7 40
*/

//#include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int a,b;
    long n;
    scanf("%d %d %d",&n,&a,&b);
    vector<int> u(n,0);
    vector<int> v(n,0);
    for(int i=0;i<a;++i){
        long indexi;
        int valuei;
        scanf("%d %d",&indexi,&valuei);
        u[indexi-1]=valuei;
    }
    for(long i=0;i<b;++i){
        long indexi;
        int valuei;
        scanf("%d %d",&indexi,&valuei);
        v[indexi-1]=valuei;
    }
    long mul=0;
    for(long i=0;i<n;++i){
        mul += u[i]*v[i];
    }
    cout<<mul;
    return 0;
}

201912_2

//测试样例2
/*
2
0 0
-100000 -10
*/
#include <iostream>
#include <map>
#include <utility> //pair
using namespace std;

int main() {
    int n;
    cin>>n;
    pair<int,int> point[n];
    map<pair<int,int>,int> hash;
    int count[5]={0};
    for(int i=0;i<n;++i){
        int xi,yi;
        cin>>xi;
        cin>>yi;
        point[i]=make_pair(xi,yi);
        hash.insert(pair<pair<int,int>,int>(point[i],1));
    }
    for(int i=0;i<n;++i){
        int x,y;
        int score=0;
        x=point[i].first;
        y=point[i].second;
        if(hash[pair<int,int>(x+1,y)]==1 && hash[pair<int,int>(x-1,y)]==1 && hash[pair<int,int>(x,y+1)]==1 && hash[pair<int,int>(x,y-1)]==1){
            if(hash[pair<int,int>(x+1,y+1)]==1){
                ++score;
            }
            if(hash[pair<int,int>(x-1,y+1)]==1){
                ++score;
            }
            if(hash[pair<int,int>(x-1,y-1)]==1){
                ++score;
            }
            if(hash[pair<int,int>(x+1,y-1)]==1){
                ++score;
            }
            ++count[score];
        }
    }
    for(int i=0;i<5;++i){
        cout<<count[i]<<endl;
    }
    return 0;
}

201909_1

//样例输入
/*
3 3
73 -8 -6 -4
76 -5 -10 -8
80 -6 -15 0
*/

#include <iostream>
#include <vector>
#include <map>
using namespace std;

int main() {
    int N,M;
    int sum=0;
    cin>>N;
    cin>>M;
    vector< vector<int> > list(N);
    map<int,int> hash;
    for(int i=0;i<N;++i){
        list[i].resize(M+1);
    }
    for(int i=0;i<N;++i){
        for(int j=0;j<M+1;++j){
            int aij;
            cin>>aij;
            list[i][j]=aij;
            sum+=aij;
        }
    }
    for(int i=0;i<N;++i){
        int value=0;
        for(int j=1;j<M+1;++j){
            value+=list[i][j];
        }
        hash[i+1]=value;
    }
    int min=1;
    for(int i=min;i<N+1;++i){
        if(hash[i]<hash[min]){
            min=i;
        }
    }
    cout<<sum<<" "<<min<<" "<<-hash[min];
    return 0;
}

201912_1

//#include <bits/stdc++.h>
#include <iostream>
using namespace std;

bool pass(int num){
    if(num%7==0){
        return 1;
    }
    while(num>0){
        if(num%10==7){
            return 1;
        }
        num/=10;
    }
    return 0;
}

int main() {
    int n;
    cin>>n;
    int passA,passB,passC,passD,sum;
    passA=passB=passC=passD=sum=0;
    int i=1;
    int distribute=0;
    while(sum<n){
        if(distribute%4==0){ // 甲
            if(pass(i)){
                ++passA;
                //cout<<"A: "<<i<<endl;
            }
            else{
                ++sum;
                //cout<<"A:"<<sum<<endl;
            }
        }
        else if(distribute%4==1){ // 乙
            if(pass(i)){
                ++passB;
                //cout<<"B: "<<i<<endl;
            }
            else{
                ++sum;
                //cout<<"B:"<<sum<<endl;
            }
        }
        else if(distribute%4==2){ // 丙
            if(pass(i)){
                ++passC;
                //cout<<"C: "<<i<<endl;
            }
            else{
                ++sum;
                //cout<<"C:"<<sum<<endl;
            }
        }
        else{ // 丁
            if(pass(i)){
                ++passD;
                //cout<<"D: "<<i<<endl;
            }
            else{
                ++sum;
                //cout<<"D:"<<sum<<endl;
            }
        }
        ++distribute;
        ++i;
    }
    cout<<passA<<endl;
    cout<<passB<<endl;
    cout<<passC<<endl;
    cout<<passD;
    return 0;
}

201912_2

#include <iostream>
#include <utility>
#include <map>
using namespace std;

const int N = 1000;
pair<int, int> place[N];


int main()
{
    int n;
    int cnt[5] = {0,0,0,0,0};
    map<pair<int, int>, int> ps;
    cin >> n;
    for(int i=0;i<n;i++){
        int a,b;
        cin >> a;
        cin >> b;
        place[i] = make_pair(a,b);
        ps[place[i]] = 1;
    }

    for(int i=0;i<n;i++){
        int sum=0;
        int a = place[i].first;
        int b = place[i].second;
        if(ps[make_pair(a-1,b)]==1&&ps[make_pair(a+1,b)]==1&&ps[make_pair(a,b-1)]==1
           &&ps[make_pair(a,b+1)]==1)  //寻找有资格进行评分的选址
        {
            if(ps[make_pair(a-1,b-1)]==1) sum++;
            if(ps[make_pair(a+1,b+1)]==1) sum++;
            if(ps[make_pair(a-1,b+1)]==1) sum++;
            if(ps[make_pair(a+1,b-1)]==1) sum++;
            cnt[sum]++;    //计算该选址的评分
        }
    }
    for(int i=0;i<5;i++){
        cout << cnt[i] << endl;
    }
    return 0;
}

201909_2

//样例输入1
/*
4
4 74 -7 -12 -5
5 73 -8 -6 59 -4
5 76 -5 -10 60 -2
5 80 -6 -15 59 0
*/
//样例输入2
/*
5
4 10 0 9 0
4 10 -2 7 0
2 10 0
4 10 -3 5 0
4 10 -1 8 0
*/

#include <iostream>
#include <queue>
#include <map>
using namespace std;

int main() {
    int N;
    cin>>N;
    queue<int> Q[N];
    for(int i=0;i<N;++i){
        int mi;
        cin>>mi;
        int ai;
        for(int j=0;j<mi;++j){
            cin>>ai;
            Q[i].push(ai);
        }
    }
    map<int,int> hash; // 记录苹果树i掉落苹果的个数
    int sumi;
    int sum=0;
    for(int i=0;i<N;++i){
        hash[i]=0;
        sumi=Q[i].front();
        Q[i].pop();
        while(!Q[i].empty()){
            if(Q[i].front()<0){
                sumi+=Q[i].front();
            }
            else if(Q[i].front()>0){
                if(Q[i].front()<sumi){
                    hash[i]=sumi-Q[i].front();
                    sumi=Q[i].front(); // 苹果个数刷新
                }
            }
            Q[i].pop();
        }
        sum+=sumi;
    }
    int drop=0;
    for(int i=0;i<N;++i){
        if(hash[i]>0){
            ++drop;
        }
    }
    int continuous=0;
    for(int i=0;i<N;++i){
        if(hash[i%hash.size()]>0 && hash[(i+1)%hash.size()]>0 && hash[(i+2)%hash.size()]>0){
            ++continuous;
        }
    }
    cout<<sum<<" "<<drop<<" "<<continuous;
    return 0;
}

201903_1

//sample
/*
4
-2 -1 2 4
*/
// cout << round(10/8.0*10)/10.0; // 四舍五入保留一位小数

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int main() {
    int n;
    cin>>n;
    vector<int> table;
    for(int i=0;i<n;++i){
        int ai;
        cin>>ai;
        table.push_back(ai);
    }
    float median; // int
    if(table.size()%2==1){
        median=table[table.size()/2];
    }
    else{
        median=round((table[table.size()/2-1]+table[table.size()/2])/2.0 * 10) /10.0;
    }
    cout<<max(table.front(),table.back())<<" "<<median<<" "<<min(table.front(),table.back());
    //cout<<endl;
    //cout<<round(10/8.0*10)/10.0;
    //cout<<endl;
    //cout<< round(10/8.0);
    return 0;
}

201903_2

//sample
/*
10
9+3+4x3
5+4x5x5
7-9-9+8
5x6/5x4
3+5+7+9
1x1+9-9
1x9-5/9
8/5+6x9
6x7-3x6
6x4+4/5
*/

//#include <bits/stdc++.h>
#include <iostream>
#include <stack>
using namespace std;

int a[100100];
stack<int> num;
int main()
{
	std::ios::sync_with_stdio(false);
	int n;
	char str[14];
	cin >> n;
	while (n--){
		cin >> str;
		while (!num.empty())num.pop();
		int i = 0;
		while (i < strlen(str)){
			if (isdigit(str[i])){
				num.push(str[i] - '0');
				i++;
			}
			else{
				if (str[i] == '+'){
				    i++;
				}
				else if (str[i] == '-'){
					i++;
					num.push((str[i] - '0') * -1);
					i++;
				}
				else if (str[i] == 'x'){
					int x = num.top();
					num.pop();
					i++;
					num.push((str[i] - '0') * x);
					i++;
				}
				else if (str[i] == '/'){
					int y = num.top();
					num.pop();
					i++;
					num.push(y / (str[i] - '0'));
					i++;
				}
			}
		}
		int sum = 0;
		while (!num.empty()) {
            sum += num.top();
            num.pop();
        }
		if (sum == 24){
			cout << "Yes" << endl;
		}
		else{
			cout << "No" << endl;
		}
	}
	return 0;
}

201803_1

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int num;
    cin>>num;
    int score=0;
    vector<int> jump;
    int continuous1=0; // 初始化为0
    int continuous2=0; // 初始化为0
    while(num!=0){
        jump.push_back(num);
        cin>>num;
    }
    /*for(int i=0;i<jump.size();++i){
        cout<<jump[i]<<" ";
    }*/
    for(vector<int>::iterator iter=jump.begin();iter!=jump.end();++iter){
        if(iter==jump.begin() && *iter==2){
            continuous2++;
            score+=2;
        }
        else if(iter==jump.begin() && *iter==1){
            continuous1=1;
            score+=1;
        }
        else if(*iter==1){
            continuous1=1;
            continuous2=0;
            score+=1;
        }
        else if(*iter==2){
            continuous2++;
            continuous1=0;
            if(continuous1){
                score+=2;
            }
            else{
                score+=continuous2*2;
            }
        }
    }
    cout<<score;
    return 0;
}

201809_1

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n;
    cin>>n;
    vector<int> first;
    for(int i=0;i<n;++i){
        int price;
        cin>>price;
        first.push_back(price);
    }
    vector<int> second;
    for(vector<int>::iterator iter=first.begin();(iter+1)!=first.end();++iter){
        if(iter==first.begin()){
            second.push_back((*iter + *(iter+1))/2);
        }
        else{
            second.push_back((*(iter-1) + *iter + *(iter+1))/3);
        }
    }
    second.push_back((first[n-1]+first[n-2])/2);
    for(int i=0;i<n;++i){
        cout<<second[i]<<" ";
    }
    return 0;
}

201809_2

#include <iostream>
using namespace std;
const int N = 1000000;
int t[N];//用来存储时间 

int main() {
    int n, a, b, c, d, max = 0, time = 0;
    cin >> n;//读入时间段数量
    for (int i = 0; i < N; i++) {
        t[i] = 0;
    }//把数组初始化为零,若使用memset函数更简洁

    for (int i = 0; i < n; i++) {
        cin >> a >> b;
        if (b > max)
            max = b;
        for (int j = a; j < b; j++)
            t[j]++;
    }//输入小H的时间段,并在对应时间段+1 

    for (int i = 0; i < n; i++) {
        cin >> c >> d;
        if (d > max)
            max = d;
        for (int j = c; j < d; j++)
            t[j]++;
    }//输入小W的时间段,并在对应时间段+1 

    for (int i = 0; i < max; i++) {
        if (t[i] == 2)
            time++;
    }//若时间为2,说明该时刻两次+1,即两人都在装车,可以聊天,time统计总时间 

    cout << time << endl;//输出所求时间
    return 0;
}

201812_1

#include <iostream>
using namespace std;

int main() {
    int r,y,g;
    scanf("%d %d %d",&r,&y,&g);
    int n;
    cin>>n;
    int result=0;
    for(int i=0;i<n;++i){
        int k,t;
        scanf("%d %d",&k,&t);
        if(k==0){
            result+=t;
        }
        else if(k==1){
            result+=t;
        }
        else if(k==2){
            result+=(t+r);
        }
    }
    cout<<result;
    return 0;
}

201812_2

/*
 * 这道题是一道模拟题,按照题目的描述进行模拟。需要注意的是数据的规模,要使用long long int类型的变量。另外,这道题需要使用模运算来判断信号灯的状态。
当经过道路时,直接加上通过时间;当遇到路灯时,都要首先判断所经过的时间内,路灯有没有发生变化。如果没有发生变化,那么就按照路灯的规则来计算时间即可;如果发生了变化,就先将经过的时间减去路灯剩余时间,然后再模红绿黄灯变化一周的时间,接着判断当前路灯的状态,最后按照路灯规则计算即可。
比如出发前是红灯,到路口后路灯发生变化,接着判断现在是否是绿灯。如果是绿灯,则直接通过;不是绿灯,则要计算到达时还需等待多长时间的红灯和黄灯。其他情况同理可得,需要仔细判断当前的状态把那个计算时间。
 */

#include <iostream>
using namespace std;

int main(){
	int r, y, g;
	cin >> r >> y >> g;
	int mode = r + y + g; //模数
	int n;
	cin >> n;
	int k, t;
	long long int result = 0; //注意结果需要是长整型
	int temp; //剩余时间
	for(int i = 0; i < n; ++i){
		cin >> k >> t;
		if(k == 0){ //路口
			result += t;
		}
		else if(k == 1){ //红灯
			if(t >= result){ //红灯时间很长的情况
				result = t;
			}
			else{ //红灯已经变化
				temp = result - t;
				temp %= mode; //现在是什么灯
				if(temp > g){ //不是绿灯
					result += mode - temp; //还需等待多久才是绿灯
				}
			}
		}
		else if(k == 2){ //黄灯
			if(t + r >= result){ //红灯时间很长的情况
				result = t + r;
			}
			else{
				temp = result - t - r;
				temp %= mode; //现在是什么灯
				if(temp > g){ //不是绿灯
					result += mode - temp; //还需等待多久才是绿灯
				}
			}
		}
		else{ //绿灯
			if(t < result){ //绿灯已经变化
				temp = result - t;
				temp %= mode; //现在是什么灯
				if(temp < y + r){ //不是绿灯
					result += y + r - temp; //还需等待多久才是绿灯
				}
			}
		}
	}
	cout << result;
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你这个代码我看不懂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值