Contest2813 - 2021个人训练赛第9场

(D请移步)https://blog.csdn.net/ctfxiaomengxin/article/details/116855378?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162117316316780261996702%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162117316316780261996702&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-3-116855378.pc_search_result_cache&utm_term=UPC+2021%E4%B8%AA%E4%BA%BA%E8%AE%AD%E7%BB%83%E8%B5%9B

(A,F,G请移步)https://blog.csdn.net/Luoxiaobaia/article/details/116809700?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162117316316780261996702%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162117316316780261996702&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_v2~rank_v29-1-116809700.pc_search_result_cache&utm_term=UPC+2021%E4%B8%AA%E4%BA%BA%E8%AE%AD%E7%BB%83%E8%B5%9B

问题 C: Error Correction

题目描述

A boolean matrix has the parity property when each row and each column has an even sum, i.e. contains an even number of bits which are set. Here’s a 4 × 4 matrix which has the parity property:
1 0 1 0
0 0 0 0
1 1 1 1
0 1 0 1
The sums of the rows are 2, 0, 4 and 2. The sums of the columns are 2, 2, 2 and 2.
Your job is to write a program that reads in a matrix and checks if it has the parity property. If not, your program should check if the parity property can be established by changing only one bit. If this is not possible either, the matrix should be classified as corrupt.

输入

The input file will contain one or more test cases. The first line of each test case contains one integer n (n < 100), representing the size of the matrix. On the next n lines, there will be n integers per line.
No other integers than ‘0’ and ‘1’ will occur in the matrix. Input will be terminated by a value of 0 for n.

输出

For each matrix in the input file, print one line. If the matrix already has the parity property, print ‘OK’. If the parity property can be established by changing one bit, print ‘Change bit (i,j)’ where i is the row and j the column of the bit to be changed. Otherwise, print ‘Corrupt’.

样例输入

4
1 0 1 0
0 0 0 0
1 1 1 1
0 1 0 1
4
1 0 1 0
0 0 1 0
1 1 1 1
0 1 0 1
4
1 0 1 0
0 1 1 0
1 1 1 1
0 1 0 1
0

样例输出

OK
Change bit (2,3)
Corrupt

 

思路

条件:只含0,1的矩阵,每行和每列之和都要为偶数。

若满足情况,输出‘OK’,否则改变一个位置上的值使得其满足情况,输出“Change bit (i,j),若无法做到改变一个数使得满足条件,输出”Corrupt“。

#include<iostream>
using namespace std;
int a[105][105];
int main()
{
    int n,sum,x,y;
    while(cin>>n)
    {
    	if(n==0)
    	break;
        x=0,y=0;
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        cin>>a[i][j];
        for(int i=0;i<n;i++)
        {
            sum=0;
            for(int j=0;j<n;j++)
            {
                sum+=a[i][j];
            }
            if(sum%2)
            {
                if(x==0)
                {
                x=i;
            }
                else
                {
                x=-1;
                break;
                }
            }
            if(x==-1)
            break;
        }
        if(x==-1)
        {
            cout<<"Corrupt"<<endl;
            continue;
        }
        for(int j=0;j<n;j++)
        {
            sum=0;
            for(int i=0;i<n;i++)
            {
                sum+=a[i][j];
            }
            if(sum%2)
            {
                if(y==0)
                {
                y=j;
                }
                else
                {
                y=-1;
                break;
                }
            }
        }
        if(y==-1)
        {
            cout<<"Corrupt"<<endl;
            continue;
        }

        if(x==0&&y==0)
        {
            cout<<"OK"<<endl;
        }
        else
        {
        	x++,y++;
            cout<<"Change bit ("<<x<<","<<y<<")"<<endl;
        }

    }
}

问题 D: Ecosystem

题目描述

There are various food chains in any ecosystem, hawk, mouse and corn to name one. Mice feed on corn,hawks eat mice and life goes on. Given the relations between the species one can easily create food chains.
One can also determine whether a chain is a cyclic one with caterpillars, plants, fungi and bacteria, as an example of cyclic chain. Caterpillars feed on plants, plants use organic substances produced by bacteria and fungi by decomposition of dead bodies of organisms such as caterpillars. These constitute a 3-member cyclic food chain.
Your task is to find 3-member cyclic food chains.

输入

Unlimited number of tests. Each test consists of: number of observer species (n), consecutive n lines containing coincidence matrix describing food relations between species. There is ‘0’ in the (i, k)-th element when i-th species does not eat k-th species and respectively ‘1’ when it does so.

输出

Outputted cyclic chains, followed by word ‘total:’ and then number of cyclic chains. The chain consists of three species separated by spaces, for example ‘1 2 4’ means that 1 eats 2, which eats 4,and 1 is a food for 4. With such a rule the chain ‘1 2 4’ is identical with ‘4 1 2’ and ‘2 4 1’ but not with ‘4 2 1’ (inverted sequence of prey and predator).
You should not print all identical chain (only that one with species sorted with ascend or descend order). Chains should be sorted. This means that the chains ‘1 2 4’, ‘1 2 3’ and ‘3 2 1’ should be
printed in the following order:
1 2 3
1 2 4
3 2 1
Output a blank line after each test case.
Assumptions: number of species in one test belongs to a range [3, 100].

样例输入 

3
0 1 0
0 0 1
1 0 0
3
0 0 1
1 0 0
0 1 0
3
0 1 1
1 0 1
1 1 0
4
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0

样例输出 

1 2 3
total:1

3 2 1
total:1

1 2 3
3 2 1
total:2

1 2 3
1 2 4
1 3 4
2 3 4
3 2 1
4 2 1
4 3 1
4 3 2
total:8

问题 E: Soundex

题目描述

Soundex coding groups together words that appear to sound alike based on their spelling. For example,“can” and “khawn”, “con” and “gone” would be equivalent under Soundex coding.
Soundex coding involves translating each word into a series of digits in which each digit represents a letter:
1 represents B, F, P, or V
2 represents C, G, J, K, Q, S, X, or Z
3 represents D or T
4 represents L
5 represents M or N
6 represents R
The letters A, E, I, O, U, H, W, and Y are not represented in Soundex coding, and repeated letters with the same code digit are represented by a single instance of that digit. Words with the same Soundex coding are considered equivalent.

输入

Each line of input contains a single word, all upper case, less than 20 letters long.

输出

For each line of input, produce a line of output giving the Soundex code.

样例输入 

KHAWN
PFISTER
BOBBY

样例输出 

25
1236
11

思路:

判断字母对应输出,注意字母赋相邻且赋值相同的时候只输出一次。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int s[30];
	s[0] = 0;  s[1] = 1; s[2] = 2;  s[3] = 3;  s[4] = 0;  s[5] = 1;  s[6] = 2;  s[7] = 0;  s[8] = 0;  s[9] = 2;  s[10] = 2; s[11] = 4; s[12] = 5; s[13] = 5; s[14] = 0; s[15] = 1; s[16] = 2; s[17] = 6; s[18] = 2; s[19] = 3; s[20] = 0; s[21] = 1;s[22] = 0; s[23] = 2; s[24] = 0;s[25] = 2; 
    string str;
    while(cin>>str)
    {
    for(int i=0;i<str.size(); i++)
    {
    if(str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i] == 'O'||str[i] == 'U'||str[i] == 'H'||str[i] == 'W'||str[i] == 'Y')
    continue;
    else
    {
    if(i==0)
    cout<<s[str[0]-'A'];
    else
    {
    if(s[str[i]-'A']== s[str[i-1]-'A'])
    continue;
    else
    cout<<s[str[i]-'A'];
    }
    }
    }
        cout<<endl;
}
    return 0;
}	

问题 F: Antiarithmetic

题目描述

A permutation of n is a bijective function of the initial n natural numbers: 0, 1, . . . , n − 1. A permutation p is called antiarithmetic if there is no subsequence of it forming an arithmetic progression of length bigger than 2, i.e. there are no three indices 0 ≤ i < j < k < n such that (pi, pj, pk) forms an arithmetic progression.
For example, the sequence (2, 0, 1, 4, 3) is an antiarithmetic permutation of 5. The sequence (0,5, 4, 3, 1, 2) is not an antiarithmetic permutation as its first, fifth and sixth term (0, 1, 2) forman arithmetic progression; and so do its second, forth and fifth term (5, 3, 1).
Your task is to check whether a given permutation of n is antiarithmetic.

输入

There are several test cases, followed by a line containing 0. Each test case is a line of the input file containing a natural number 3 ≤ n ≤ 10000 followed by a colon and then followed by n distinct numbers separated by whitespace. All n numbers are natural numbers smaller than n.

输出

For each test case output one line with ‘yes’ or ‘no’ stating whether the permutation is  antiarithmetic or not.

样例输入 

3: 0 2 1
5: 2 0 1 3 4
6: 2 4 3 5 0 1
0

样例输出 

yes
no
yes

问题 G: Only I did it!

题目描述

Once upon a time, in the land of Ceeplenty, lived 3 friends that liked to solve problems. They used internet engines to look for problems and they often tried to solve the problems that none of the other
2 had solved. They once met you and you managed to convince them that you were better at problem solving. So they asked you to write a program that tells which of the 3 friends solved more problems
that none of the other 2 have solved.

输入

The first line of input gives the number of cases, T (1 ≤ T ≤ 20). T test cases follow. Each test case is composed of three lines corresponding to the problems solved by the first, second and third friend,
respectively. Each of these lines has an integer S (0 ≤ S ≤ 1000) followed by the list of S solved problems. A solved problem is identified uniquely by a positive integer smaller or equal than 10000.

输出

The output is comprised a line identifying the test case with the string ‘Case #C:’ (where C is the number of the current test case). Then print another line with the number of the friend (1, 2 or 3) asked in the description followed by the number of problems that he solved but none of the other 2 did,followed by the sorted list of these problems in one line. When there is a tie, print one such line for each tied friend, sorted by the number of the friend.

样例输入 

4
3 1 2 3
4 4 5 6 7
5 8 9 10 11 12
2 1 5
2 2 3
3 2 3 1
6 400 401 402 403 404 405
2 101 100
7 400 401 402 403 404 405 406
1 1
1 2
1 3

样例输出 

Case #1:
3 5 8 9 10 11 12
Case #2:
1 1 5
Case #3:
2 2 100 101
Case #4:
1 1 1
2 1 2
3 1 3

问题 H: Square Numbers

题目描述

A square number is an integer number whose square root is also an integer. For example 1, 4, 81 are some square numbers. Given two numbers a and b you will have to find out how many square numbers are there between a and b (inclusive).

输入

The input file contains at most 201 lines of inputs. Each line contains two integers a and b (0 < a ≤ b ≤ 100000). Input is terminated by a line containing two zeroes. This line should not be processed.

输出

For each line of input produce one line of output. This line contains an integer which denotes how many square numbers are there between a and b (inclusive).

样例输入 

1 4
1 10
0 0

样例输出

2
3

思路

a,b之间(包含a,b)的完全平方数有多少个。注意开longlong以及判断完全平方数

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	ll a,b,k;
	for(int i=0;;i++)
	{
		cin>>a>>b;
		if(a==0&&b==0)
		break;
		k=0;
		for(int j=a;j<=b;j++)
		{
		ll t= (ll)(sqrt(j*1.0)+1e-5);
		if(t*t==j)
		k++;
		}
		cout<<k<<endl;
	}
	return 0;
}

问题 I: 面积

题目描述

乐乐的家中共有 n 个长方形,请你帮助乐乐计算一下这些长方形的总面积。

输入

第一行只有一个正整数 n 
接下来的 n 行,每行两个正整数:x  y,分别表示长方形的长和宽
 

输出

只有一行且只有一个整数:这些长方形的总面积。

样例输入

3 
3 2 
5 4 
7 3

样例输出 

47

提示

3*2 + 5*4 + 7*3 = 47。 
【数据范围】 
对于 70%的数据,  1 < n <= 1 000 ,  1 <= y <= x <= 1 000 
对于 100%的数据, 1 < n <= 10 000 , 1 <= y <= x <= 100 000 

思路

开longlong,长乘宽再相加即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	ll n,a,b,s=0;
	cin>>n;
	while(n--)
	{
		cin>>a>>b;
		s+=a*b;
	}
	cout<<s<<endl;
	return 0;
 } 

问题 J: 分数

题目描述

每位同学都有自己的一个幸运数,乐乐所在班级共有 n 位同学,因此有编号为 1 至 n的 n 块标牌;标牌的编号与自己的幸运数相乘,就是这位同学的分数。你的工作就是帮乐乐寻找一种方案,使得班级的总分数最大。 

输入

第一行只有一个正整数 n 
第二行共有 n 个不超过 10000 的正整数,中间用 1 个空格隔开。 
 

输出

只有一行且只有一个整数:乐乐班级的总分数。

样例输入 

3 
2 4 3

样例输出 

20

提示

2*1 + 3*2 + 4*3 = 20 

对于 70%的数据,  1 < n <= 1 000 
对于 100%的数据, 1 < n <= 10 000 

思路:开longlong,数组排序并与其所在位置相乘

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	ll n,a[10005],s=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++)
	{
		s+=a[i]*i;
	}
	cout<<s<<endl;
	return 0;
}

问题 K: 立方

题目描述

今年乐乐开始学编程了,上几天刚刚解决了一个平方数的问题,问题是这样的:随便告诉你一个不超过 1 个亿的正整数,请你算出不超过该数的所有立方数的个数。 
但是,如果这个超过 1 亿,是一个 18 位正整数,或 28 位的正整数呢? 
乐乐不会算,你会吗? 

输入

只有一行且只有一个正整数:n

输出

只有一行且只有一个正整数:不超过 n 的平方数个数

样例输入

100

样例输出 

4

提示

1*1*1 = 1        2*2*2 = 8 
3*3*3 = 27       4*4*4 = 64 
5*5*5 = 125   但是  125  >  100 

对于 30%的数据,  1 <= n <= 10^8 
对于 70%的数据,  1 <= n <= 10^18  
对于 100%的数据, 1 <= n <= 10^28  

思路:由于10^28很大,所以正常循环会超时,所以将n用double 定义开三次根

#include<bits/stdc++.h>
using namespace std;
int main()
{
	double n;
	cin>>n;
	printf("%.0lf",pow(n,1/3.0)-0.5);
	return 0;
}

问题 L: 排队

题目描述

乐乐的 n 位朋友都拥有唯一的一个编号,编号分别为 1 至 n。某天按到达的时间顺序又给了一个顺序号,此时发现顺序号与多数的朋友编号不一致。乐乐想:如果俩俩交换顺序号,使得每位朋友的编号与顺序号相同,则最少需要交换几次? 

输入

包含二行: 

第一行只有一个正整数:n,表示乐乐朋友的人数 

第二行共有 n 个正整数,分别表示按顺序到达的朋友编号 

输出

只有一行且只有一个正整数:最少的交换次数 

样例输入 

5  
4 2 1 5 3

样例输出 

3

提示

对于 30%的数据,  1 <= n <= 100 

对于 80%的数据,  1 <= n <= 10 000 

对于 100%的数据, 1 <= n <= 100 000 

思路:对数组排序,求最少交换次数。正常排序会超时,同时发现每个集合都需要交换 (集合的长度-1) ,答案就是 n-集合的个数。

#include<bits/stdc++.h>
#include<vector>
using namespace std;
int n,x;
vector<int> v;
int get(vector<int> v)
{
    map<int, int>a;    
    vector<int>s(v);
    sort(s.begin(),s.end());
    for(int i=0;i<s.size();i++)
	a[s[i]]=i;
    int cnt=0;
    vector<bool> flag(s.size(),false);
    for(int i=0;i<s.size();i++)
    {
    	if(flag[i]==0)
    	{
    		int p=i;
    		while(flag[p]==0)
    		{
    			flag[p]=true;
    			p=a[v[p]];
			}
			cnt++;
		}
	}
    return s.size()-cnt;
}
int main()
{
    cin>>n;
 	for(int i=0;i<n;i++)
	 {
 		cin>>x;
 		v.push_back(x);
	 }
	cout<<get(v)<<endl;
 	return 0;
}

(只做出来这些,我是fw)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值