2021-01-21

本文介绍了四个编程题目,涉及项链问题、交流电路分析、对称矩阵判断和同质方格判断。每个题目都通过数学公式推导和编程实现,展示了从理论到实践的过程。题目涵盖数值计算、电路理论、矩阵理论和图论概念,旨在锻炼读者的逻辑思维和编程能力。
摘要由CSDN通过智能技术生成

灵动ICPC冬令营基础-4

A - Necklace

#include<cstdio>
int main(){
	double Vt,V0;
	while(~scanf("%lf%lf",&Vt,&V0)&&Vt&&V0){
		if(V0>=Vt) printf("0\n");
		else if(Vt<=2*V0) printf("1\n");
		else {
			if(Vt/(2*V0)-(int)(Vt/(2*V0))==0.5) printf("0\n");
			else if(Vt/(2*V0)-(int)(Vt/(2*V0))<0.5) printf("%d\n",(int)(Vt/(2*V0)));
			else printf("%d\n",(int)(Vt/(2*V0)));
		}
	}
	return 0;
}

推公式代入,与整形数据相减求出小数与0.5比较

B - Bode Plot

Consider the AC circuit below. We will assume that the circuit is in steady-state. Thus, the voltage at nodes 1 and 2 are given by v1 = VS coswt and v2 = VRcos (wt + q ) where VS is the voltage of the source, w is the frequency (in radians per second), and t is time. VR is the magnitude of the voltage drop across the resistor, and q is its phase.

You are to write a program to determine VR for different values of w. You will need two laws of electricity to solve this problem. The first is Ohm’s Law, which states v2 = iR where i is the current in the circuit, oriented clockwise. The second is i = C d/dt (v1-v2) which relates the current to the voltage on either side of the capacitor. "d/dt"indicates the derivative with respect to t.
Input
The input will consist of one or more lines. The first line contains three real numbers and a non-negative integer. The real numbers are VS, R, and C, in that order. The integer, n, is the number of test cases. The following n lines of the input will have one real number per line. Each of these numbers is the angular frequency, w.
Output
For each angular frequency in the input you are to output its corresponding VR on a single line. Each VR value output should be rounded to three digits after the decimal point.
Sample Input
1.0 1.0 1.0 9
0.01
0.031623
0.1
0.31623
1.0
3.1623
10.0
31.623
100.0
Sample Output
0.010
0.032
0.100
0.302
0.707
0.953
0.995
1.000
1.000

#include<cstdio>
#include<cmath>
int main(){
	int i,n;
	double VR,VS,R,C,w;
	scanf("%lf%lf%lf%d",&VS,&R,&C,&n);
	for(i=0;i<n;i++){
		scanf("%lf",&w);
		VR=C*R*w*VS/sqrt(1+C*C*R*R*w *w );
		printf("%.3lf\n",VR);
	}
	return 0;
}

推推·推·公式

C - Symmetric Matrix

You‘re given a square matrix M. Elements of this matrix are Mij : {0 < i < n, 0 < j < n}. In this
problem you’ll have to find out whether the given matrix is symmetric or not.
Definition: Symmetric matrix is such a matrix that all elements of it are non-negative and symmetric
with relation to the center of this matrix. Any other matrix is considered to be non-symmetric. For
example:
M =


5 1 3
2 0 2
3 1 5

 is symmetric
M =


5 1 3
2 0 2
0 1 5

 is not symmetric, because 3 ̸= 0
All you have to do is to find whether the matrix is symmetric or not. Elements of a matrix given
in the input are −2
32 ≤ Mij ≤ 2
32 and 0 < n ≤ 100.
Input
First line of input contains number of test cases T ≤ 300. Then T test cases follow each described in
the following way. The first line of each test case contains n – the dimension of square matrix. Then
n lines follow each of then containing row i. Row contains exactly n elements separated by a space
character. j-th number in row i is the element Mij of matrix you have to process.
Output
For each test case output one line ‘Test #t: S’. Where t is the test number starting from 1. Line S
is equal to ‘Symmetric’ if matrix is symmetric and ‘Non-symmetric’ in any other case.
Sample Input
2
N = 3
5 1 3
2 0 2
3 1 5
N = 3
5 1 3
2 0 2
0 1 5
Sample Output
Test #1: Symmetric.
Test #2: Non-symmetric.

#include<cstdio>
long long M[1000][1000];
int main(){
	int T,t,i,j,N,flag;
	while(~scanf("%d",&T)){
		for(t=1;t<=T;t++){
			getchar();
			scanf("N = %d",&N);
			flag=1;
			for(i=0;i<N;i++){
				for(j=0;j<N;j++){
					scanf("%lld",&M[i][j]);
				}
			}
			for(i=0;i<N;i++){
				for(j=0;j<N;j++){
					if(M[i][j]<0||M[i][j] !=M[N-1-i][N- 1-j]) {
						flag=0;
						break;
					}
				}
				if(flag==0) break;
			}
			if(flag) printf("Test #%d: Symmetric.\n",t);
			else printf("Test #%d: Non-symmetric.\n",t);
		} 
    }
	return 0;
}

D - Homogeneous Squares

Assume you have a square of size n that is divided into n × n positions just as a checkerboard. Two positions (x1, y1) and (x2, y2), where 1 ≤ x1, y1, x2, y2 ≤ n, are called “independent” if they occupy different rows and different columns, that is, x1 ≠ x2 and y1 ≠ y2. More generally, n positions are called independent if they are pairwise independent. It follows that there are n! different ways to choose n independent positions.

Assume further that a number is written in each position of such an n × n square. This square is called “homogeneous” if the sum of the numbers written in n independent positions is the same, no matter how the positions are chosen. Write a program to determine if a given square is homogeneous!

Input
The input contains several test cases.

The first line of each test case contains an integer n (1 ≤ n ≤ 1000). Each of the next n lines contains n numbers, separated by exactly one space character. Each number is an integer from the interval [−1000000, 1000000].

The last test case is followed by a zero.

Output
For each test case output whether the specified square is homogeneous or not. Adhere to the format shown in the sample output.

Sample Input
2
1 2
3 4
3
1 3 4
8 6 -2
-3 4 0
0
Sample Output
homogeneous
not homogeneous

#include<cstdio>
int a[1008][1008];
int main(){
	int n;
	while (~scanf("%d",&n)&&n){
		int flag=1;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				scanf("%d",&a[i][j]);
			}
		}
		for(int i=1;i<n;i++){
			for(int j=1;j<n;j++){
				if(a[i][j]+a[i+1][j+1]!=a[i][j+1]+a[i+1][j]){
					flag=0;
					break;
				}
			}
			if(flag==0) break;
		}
		if(flag) printf("homogeneous\n");
		else printf("not homogeneous\n");
	}
	return 0;
}

每一个小矩形的对角和相等,就能代表整个大的

E - To the Max

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.
As an example, the maximal sub-rectangle of the array:

0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
is in the lower left corner:

9 2
-4 1
-1 8
and has a sum of 15.
Input
The input consists of an N * N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N^2 integers separated by whitespace (spaces and newlines). These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].
Output
Output the sum of the maximal sub-rectangle.
Sample Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1

8 0 -2
Sample Output
15

#include<cstdio>
const int s=108;
int a[s][s]={0};
int sum[s]={0},d[s]={0};
int main(){
	int N,max=0,ans=0,i,j,k;
	scanf("%d",&N);
	for( i=1;i<=N;i++){
		for( j=1;j<=N;j++){
			scanf("%d",&a[i][j]);
			a[i][j]=a[i-1][j]+a[i][j];
		}
	}
	for(i=1;i<=N;i++){
		for(j=i;j<=N;j++){
			for(k=1;k<=N;k++){
				sum[k]=a[j][k]-a[i-1][k];
				d[k]=d[k-1]>=0?d[k-1]+sum[k]:sum[k];
				if(d[k]>max) max=d[k];
			}
			if(max>ans) ans=max;
		}
    }
	printf("%d",ans);
	return 0;
}

读入的时候 就把每一行加上前一行。 然后算第i行到j行 压缩后的序列的最大子列和,求不同压缩的子列和,互相比较求最大子矩阵

F - Who’s in the Middle

FJ is surveying his herd to find the most average cow. He wants to know how much milk this ‘median’ cow gives: half of the cows give as much or more than the median; half give as much or less.

Given an odd number of cows N (1 <= N < 10,000) and their milk output (1…1,000,000), find the median amount of milk given such that at least half the cows give the same amount of milk or more and at least half give the same or less.
Input

  • Line 1: A single integer N

  • Lines 2…N+1: Each line contains a single integer that is the milk output of one cow.
    Output

  • Line 1: A single integer that is the median milk output.
    Sample Input
    5
    2
    4
    1
    3
    5
    Sample Output
    3
    Hint
    INPUT DETAILS:

Five cows with milk outputs of 1…5

OUTPUT DETAILS:

1 and 2 are below 3; 4 and 5 are above 3.

#include<cstdio>
int a[10003];
void swap(int *a, int *b){ 
int temp = *a;
*a = *b;
*b = temp;
}
int main(){
	int N;
	scanf("%d",&N);
	for(int i = 0 ;i < N ; i++) scanf("%d",&a[i]);
	for (int i = 0; i < N - 1; i++){
		for (int j = 0; j < N - 1 - i; j++){
			if (a[j] > a[j + 1]) swap(&a[j], &a[j+1]);
		}
	}
	printf("%d",a[N/2]);
	return 0;
}

排序

G - Train Swapping

At an old railway station, you may still encounter one of the last remaining “train swappers”. A train
swapper is an employee of the railroad, whose sole job it is to rearrange the carriages of trains.
Once the carriages are arranged in the optimal order, all the train driver has to do, is drop the
carriages off, one by one, at the stations for which the load is meant.
The title “train swapper” stems from the first person who performed this task, at a station close to
a railway bridge. Instead of opening up vertically, the bridge rotated around a pillar in the center of
the river. After rotating the bridge 90 degrees, boats could pass left or right.
The first train swapper had discovered that the bridge could be operated with at most two carriages
on it. By rotating the bridge 180 degrees, the carriages switched place, allowing him to rearrange the
carriages (as a side effect, the carriages then faced the opposite direction, but train carriages can move
either way, so who cares).
Now that almost all train swappers have died out, the railway company would like to automate
their operation. Part of the program to be developed, is a routine which decides for a given train the
least number of swaps of two adjacent carriages necessary to order the train. Your assignment is to
create that routine.
Input
The input contains on the first line the number of test cases (N). Each test case consists of two input
lines. The first line of a test case contains an integer L, determining the length of the train (0 ≤ L ≤ 50).
The second line of a test case contains a permutation of the numbers 1 through L, indicating the current
order of the carriages. The carriages should be ordered such that carriage 1 comes first, then 2, etc.
with carriage L coming last.
Output
For each test case output the sentence: ‘Optimal train swapping takes S swaps.’ where S is an
integer.
Sample Input
3
3
1 3 2
4
4 3 2 1
2
2 1
Sample Output
Optimal train swapping takes 1 swaps.
Optimal train swapping takes 6 swaps.
Optimal train swapping takes 1 swaps.

#include<cstdio>
void swap(int *a, int *b){
	int temp = *a;
    *a = *b;
    *b = temp;
}
int a[60];
int main(){
	int N;
	scanf("%d",&N);
	while(N--){
		int L,c=0;
		scanf("%d",&L);
		for(int i = 0 ;i < L ; i++) scanf("%d",&a[i]);
		for (int i = 0; i < L - 1; i++){
		    for (int j = 0; j < L - 1 - i; j++){
			if (a[j] > a[j + 1]) swap(&a[j], &a[j+1]),c++;
		    }
	    }
	    printf("Optimal train swapping takes %d swaps.\n",c);
	}
	return 0;
}

计排序次数

H - DNA Sorting

One measure of unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequenceDAABEC’’, this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence AACEDGG'' has only one inversion (E and D)---it is nearly sorted---while the sequenceZWQM’’ has 6 inversions (it is as unsorted as can be—exactly the reverse of sorted).

You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of sortedness'', frommost sorted’’ to least sorted''. All the strings are of the same length. Input The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (0 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n. Output Output the list of input strings, arranged frommost sorted’’ to ``least sorted’’. Since two strings can be equally sorted, then output them according to the orginal order.
Sample Input
10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT
Sample Output
CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA

#include<cstdio>
struct p{
	char a[101];
    int c;
}d[101];
void swap(struct p *a, struct p *b){
	struct p temp = *a;
    *a = *b;
    *b = temp;
}
int main(){
	int m,n,i,j,k;
	scanf("%d%d",&n,&m);
	for( i = 0 ; i < m ; i++){
		scanf("%s",d[i].a);
	}
	for( k=0;k<m;k++){
		for ( i = 0; i < n-1 ; i++){
		    for ( j = i+1; j < n ; j++){
			    if (d[k].a[i] > d[k].a[j]) d[k].c++;
		    }
	    }
	}
	for ( i = 0; i < m - 1; i++){
		    for ( j = 0; j < m - 1 - i; j++){
			if (d[j].c > d[j + 1].c) swap(&d[j], &d[j+1]);
		    }
	    }
	for(int i = 0 ; i < m ; i++){
		printf("%s\n",d[i].a);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值