2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛

A

Fruit Ni

链接:https://www.nowcoder.com/acm/contest/163/A
来源:牛客网

题目描述

Fruit Ninja is a juicy action game enjoyed by millions of players around the world, with squishy,
splat and satisfying fruit carnage! Become the ultimate bringer of sweet, tasty destruction with every slash.
Fruit Ninja is a very popular game on cell phones where people can enjoy cutting the fruit by touching the screen.
In this problem, the screen is rectangular, and all the fruits can be considered as a point. A touch is a straight line cutting
thought the whole screen, all the fruits in the line will be cut.
A touch is EXCELLENT if ≥ x, (N is total number of fruits in the screen, M is the number of fruits that cut by the touch, x is a real number.)
Now you are given N fruits position in the screen, you want to know if exist a EXCELLENT touch.

输入描述:

The first line of the input is T(1≤ T ≤ 100), which stands for the number of test cases you need to solve.
The first line of each case contains an integer N (1 ≤ N ≤ 104) and a real number x (0 < x < 1), as mentioned above.
The real number will have only 1 digit after the decimal point.
The next N lines, each lines contains two integers xi and yi (-109 ≤ xi,yi ≤ 109), denotes the coordinates of a fruit.

输出描述:

For each test case, output "Yes" if there are at least one EXCELLENT touch. Otherwise, output "No".

示例1

输入

2
5 0.6
-1 -1
20 1
1 20
5 5
9 9
5 0.5
-1 -1
20 1
1 20
2 5
9 9

输出

Yes
No

解题:利用随机数rand

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+5;
struct Node{
    int x,y;
}f[maxn];
int n; 
double m;
bool paral(Node a, Node b ,Node c){
    if((c.y-b.y)*(c.x-a.x)==(c.y-a.y)*(c.x-b.x)) 
        return true;
    return false;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%lf",&n,&m);
        for(int i=0;i<n;i++) {
            scanf("%d%d",&f[i].x,&f[i].y);
        }
        int cnt=1000;
        int flag=0;
        int cc=(int)ceil(m*n);
        while(cnt--) {
            int a=rand()%n;
            int b=rand()%n;
            if(a==b) {
                continue;
            }
            int sum=2;
            for(int i=0;i<n;i++) {
                if(i==a||i==b) {
                    continue;
                }
                if(paral(f[i],f[a],f[b])) {
                    sum++;
                }
            }
            if(sum>=cc) {
                flag=1; break;
            }
        }
        if(flag) {
            printf("Yes\n");
        }
        else{
            printf("No\n");
        }
    }
   return 0;
}

B.Perfect Numbers

链接:https://www.nowcoder.com/acm/contest/163/B
来源:牛客网

题目描述

We consider a positive integer perfect, if and only if it is equal to the sum of its positive divisors less than itself.
For example, 6 is perfect because 6 = 1 + 2 + 3.
Could you write a program to determine if a given number is perfect or not?

输入描述:

The first line of the input is T(1≤ T ≤ 100), which stands for the number of test cases you need to solve.
Each test case contains a line with a positive integer N (2 ≤ N ≤ 105).

输出描述:

For each test case, print the case number and determine whether or not the number is perfect.
If the number is perfect, display the sum of its positive divisors less than itself. The ordering of the
terms of the sum must be in ascending order. If a number is not perfect, print "Not perfect.".

示例1

输入

3
6
8
28

输出

Case 1: 6 = 1 + 2 + 3
Case 2: Not perfect.
Case 3: 28 = 1 + 2 + 4 + 7 + 14
#include <bits/stdc++.h>
 
#define MAXN 40000000
using namespace std;
int main()
{
    int n, flag, i,cot=1,t;
    scanf("%d",&t);
    while (t--){
    	scanf("%d", &n);
    	printf("Case %d: ",cot++);
        if (n==6)
        printf("6 = 1 + 2 + 3\n");
        else if (n==28)
        printf("28 = 1 + 2 + 4 + 7 + 14\n");
        else if (n==496)
        printf("496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248\n");
        else if (n==8128)
        printf("8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064\n");
        else
        printf("Not perfect.\n");
	}
    return 0;
}

D.Thinking-Bear magic

链接:https://www.nowcoder.com/acm/contest/163/D
来源:牛客网

题目描述

In order to become a magical girl, Thinking-Bear are learning magic circle.
He first drew a regular polygon of N sides, and the length of each side is a.
He want to get a regular polygon of N sides, and the polygon area is no more than L.
He doesn't want to draw a new regular polygon as it takes too much effort.
So he think a good idea, connect the midpoint of each edge and get a new regular polygon of N sides.
How many operations does it need to get the polygon he want?

输入描述:

The first line of the input is T(1≤ T ≤ 100), which stands for the number of test cases you need to solve.
The first line of each case contains three space-separated integers N, a and L (3 ≤ N ≤ 10, 1 ≤ a ≤ 100, 1 ≤ L ≤ 1000).

输出描述:

For each test case, output a single integer.

示例1

输入

1
4 2 3

输出

1

解题:正四边形和它的中点四边形的比例

#include<bits/stdc++.h>
using namespace std;
#define PI acos(-1)
int t,cnt,n,side,l;
int main()
{
	
	scanf("%d",&t);
	while(t--){
		cnt=0;
		scanf("%d%d%d",&n,&side,&l);
		double rea=(n*side*side)/(4*tan(PI/n));
		double bi=(sin(PI*(n-2)/(2*n)))*(sin(PI*(n-2)/(2*n)));
		while (rea>l){
			rea*=bi;
			cnt++;
		}
		printf("%d\n",cnt);
    }
}

J.Beautiful Numbers

链接:https://www.nowcoder.com/acm/contest/163/J
来源:牛客网

题目描述

NIBGNAUK is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by the sum of its digits.

We will not argue with this and just count the quantity of beautiful numbers from 1 to N.

输入描述:

The first line of the input is T(1≤ T ≤ 100), which stands for the number of test cases you need to solve.
Each test case contains a line with a positive integer N (1 ≤ N ≤ 1012).

输出描述:

For each test case, print the case number and the quantity of beautiful numbers in [1, N].

示例1

输入

2
10
18

输出

Case 1: 10
Case 2: 12

解题:数位dp

#include<bits/stdc++.h>
using namespace std;
int mod;
long long n;
int a[22];
long long dp[22][109][150];
long long dfs(int pos, int sum, int remind, int limit)
{
    if(pos==-1)
    return sum==0&&remind==0;
    if(!limit&&dp[pos][sum][remind]!=-1) return dp[pos][sum][remind];
    int to;
    if(limit)
    to=a[pos];
    else to=9;
    long long ans=0;
    for(int i=0;i<=to;i++)
    if(i>sum)break;
    else
    ans+=dfs(pos-1,sum-i,(remind*10+i)%mod,limit&&i==a[pos]);
    if(!limit) dp[pos][sum][remind]=ans;
    return ans;
}
  
long long  solve(long long x)
{
    int cnt=0;
    while(x)
    {
        a[cnt++]=x%10;
        x/=10;
    }
    long long  ans=0;
    for(int i=1;i<=9*cnt;i++)
    {
        mod=i;
        memset(dp,-1,sizeof(dp));
        ans+=dfs(cnt-1,i,0,1);
    }
    return ans;
}
  
int main()
{
    int t,cnt=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        printf("Case %d: ",cnt++);
        printf("%lld\n",solve(n));
    }
}

K.Matrix Multiplication

链接:https://www.nowcoder.com/acm/contest/163/K
来源:牛客网

题目描述

In mathematics, matrix multiplication or matrix product is a binary operation that produces a matrix from two matrices with entries in a field, or, more generally, in a ring or even a semiring. The matrix product is designed for representing the composition of linear maps that are represented by matrices. Matrix multiplication is thus a basic tool of linear algebra, and as such has numerous applications in many areas of mathematics, as well as in applied mathematics, physics, and engineering. In more detail, if A is an n x m matrix and B is an m x p matrix, their product AB is an n x p matrix, in which the m entries across a row of are multiplied with the m emtries down a column of B and summed to produce an entry of AB. When two linear maps are represented by matrices, then the matrix product represents the composition of the two maps.

We can only multiply two matrices if their dimensions are compatible, which means the number of columns in the first matrix is the same as the number of rows in the second matrix. 

If A is an n x m matrix and B is an m x p matrix,

the matrix product C = AB is defined to be the n x p matrix

such that
,
for i = 1,2, ..., n and j = 1,2, ..., p.
Your task is to design a matrix multiplication calculator to multiply two matrices and
display the output. If the matrices cannot be multiplied, display "ERROR".

输入描述:

The first line of the input is T(1≤ T ≤ 100), which stands for the number of test cases you need to solve.
For each test case, the first line contains four integers m, n, p and q (1 ≤ m,n,p,q ≤ 20). m and n represent the dimension of matrix A, while p and q represent the dimension of matrix B.
The following m lines consist of the data for matrix A followed by p lines that contains the data for matrix B. (-100 ≤ aij ≤ 100, -100 ≤ bij ≤ 100).

输出描述:

For each test case, print the case number and the output of the matrix multiplication.

示例1

输入

2
2 3 3 2
1 1 1
1 2 3
2 3
4 5
6 7
2 3 2 3
1 2 3
1 2 3
2 3 4
2 3 4

输出

Case 1:
12 15
28 34
Case 2:
ERROR

解题:简单的矩阵相乘

#include <bits/stdc++.h>
 
#define maxn 25
using namespace std;
long long a[maxn][maxn],b[maxn][maxn],c[maxn][maxn]; 
long long t,n,m,p,q,cnt=1;
int main()
{
   
    scanf("%lld",&t);
    while (t--){
    	scanf("%lld%lld%lld%lld", &m,&n,&p,&q);
    	for (int i=0;i<m;i++)
    	    for (int j=0;j<n;j++)
    	        scanf("%lld",&a[i][j]);
    	for (int i=0;i<p;i++)
    	    for (int j=0;j<q;j++)
    	        scanf("%d",&b[i][j]);
    	printf("Case %lld:\n",cnt++);
    	if (m==q){
            for(int i=0;i<m;++i){ 
		        for(int j=0;j<q;++j){
			        c[i][j]=0;
			        for(int k=0;k<n;++k)
				    c[i][j]+=a[i][k]*b[k][j];
			    }
    	     }
	        for(int i=0;i<m;++i){
		        for(int j=0;j<q;++j){
				    printf("%lld ",c[i][j]);
		        }
		        printf("\n");
	         }
		}
        else
		printf("ERROR\n");
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40911499/article/details/81479805