ACM题目及答案
- 格式:pdf
- 大小:284.58 KB
- 文档页数:12
ACM作业与答案整理1、平面分割方法:设有n条封闭曲线画在平面上,而任何两条封闭曲线恰好相交于两点,且任何三条封闭曲线不相交于同一点,问这些封闭曲线把平面分割成的区域个数。
#include <iostream.h>int f(int n){if(n==1) return 2;else return f(n-1)+2*(n-1);}void main(){int n;while(1){cin>>n;cout<<f(n)<<endl;}}2、LELE的RPG难题:有排成一行的n个方格,用红(Red)、粉(Pink)、绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色.编程全部的满足要求的涂法.#include<iostream.h>int f(int n){if(n==1) return 3;else if(n==2) return 6;else return f(n-1)+f(n-2)*2;}void main(){int n;while(1){cin>>n;cout<<f(n)<<endl;}}3、北大ACM(1942)Paths on a GridTime Limit: 1000MS Memory Limit: 30000K DescriptionImagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastered years ago (this time he's explaining that (a+b)2=a2+2ab+b2). So you decide to waste your time with drawing modern art instead.Fortunately you have a piece of squared paper and you choose a rectangle of size n*m on the paper. Let's call this rectangle together with the lines it contains a grid. Starting at the lower left corner of the grid, you move your pencil to the upper right corner, taking care that it stays on the lines and moves only to the right or up. The result is shown on the left:Really a masterpiece, isn't it? Repeating the procedure one more time, you arrive with the picture shown on the right. Now you wonder: how many different works of art can you produce?InputThe input contains several testcases. Each is specified by two unsigned 32-bit integers n and m, denoting the size of the rectangle. As you can observe, the number of lines of the corresponding grid is one more in each dimension. Input is terminated by n=m=0.OutputFor each test case output on a line the number of different art works that can be generated using the procedure described above. That is, how many paths are there on a grid where each step of the path consists of moving one unit to the right orone unit up? You may safely assume that this number fits into a 32-bit unsigned integer.Sample Input5 41 10 0Sample Output1262#include<iostream>using namespace std;longlong f(long long m, long long n){if(n==0) return 1;else return f(m-1,n-1)*m/n;}int main(){longlongm,n;while(scanf("%I64d %I64d",&n,&m) &&n+m){printf("%I64d\n",f(m+n,min(m,n)));}return 0;}1、(并查集)若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系。
ACM程序设计东北林业大学陈宇Lg_chenyu@第一讲算法原理和ACM入门(Introduction to ACM)我校的ACM在线评测系统**课件下载地址:*/kj/suanfa01.ppt预期赛事(今后每年)*3~4月,举行校内大赛(暨选拔赛)*4月,ACM全国邀请赛*5月,参加黑龙江省大学生程序设计大赛*6月,参加东北4省大学生程序设计大赛*10~11月,参加ACM/ICPC亚洲区比赛(至少参加4~5个赛区的比赛)*另外,每学期至少有三次月赛以及适当的练习赛第一部分算法概述*算法分析(Algorithm Analysis):对算法所需要的两种计算机资源——时间和空间进行估算;时间复杂性(Time Complexity)空间复杂性(Space Complexity)算法分析的目的:设计算法——设计出复杂性尽可能低的算法选择算法——在多种算法中选择其中复杂性最低者算法的描述语言:⑴自然语言优点:容易理解缺点:冗长、二义性使用方法:粗线条描述算法思想注意事项:避免写成自然段(2)流程图优点:流程直观缺点:缺少严密性、灵活性使用方法:描述简单算法注意事项:注意抽象层次⑶程序设计语言优点:能由计算机执行缺点:抽象性差,对语言要求高使用方法:算法需要验证注意事项:将算法写成子函数(4)伪代码——算法语言伪代码(Pseudocode):介于自然语言和程序设计语言之间的方法,它采用某一程序设计语言的基本语法,操作指令可以结合自然语言来设计。
优点:表达能力强,抽象性强,容易理解评价算法*评价算法的三条主要标准是:*(1) 算法实现所耗费的时间;*(2) 算法实现所所耗费的存储空间,其中*主要考虑辅助存储空间;*(3) 算法应易于理解,易于编码,易于调*试等等。
和算法执行时间相关的因素:1)问题中数据存储的数据结构2)算法采用的数学模型3)算法设计的策略4)问题的规模5)实现算法的程序设计语言6)编译算法产生的机器代码的质量7)计算机执行指令的速度算法效率的衡量方法*通常有两种衡量算法效率的方法:*1)事后统计法(有缺点,较少使用)*2)事前分析估算法*算法的时间效率是问题规模的函数。
ACM题目、测试用例及参考答案汇编——一次ACM协会内部测试第一题:梦境是虚幻吗?时间限制:3000ms 内存限制:65535KB 难度:★★描述《盗梦空间》是一部精彩的影片,在这部电影里,Cobb等人可以进入梦境之中,梦境里的时间会比现实中的时间过得快得多,这里假设现实中的3分钟,在梦里就是1小时。
然而,Cobb他们利用强效镇静剂,可以从第一层梦境进入第二层梦境,甚至进入三层,四层梦境,每层梦境都会产生同样的时间加速效果。
那么现在给你Cobb在各层梦境中经历的时间,你能算出现实世界过了多长时间吗?比如,Cobb先在第一层梦境待了1个小时,又在第二层梦境里待了1天,之后,返回第一层梦境之后立刻返回了现实。
那么在现实世界里,其实过了396秒(6.6分钟)输入第一行输入一个整数T(0<=T<=100),表示测试数据的组数。
每组测试数据的第一行是一个数字M(3<=M<=100)随后的M行每行的开头是一个字符串,该字符串如果是"IN" 则Cobb向更深层的梦境出发了,如果是字符串"OUT"则表示Cobb从深层的梦回到了上一层。
如果是首字符串是"STAY"则表示Cobb在该层梦境中停留了一段时间,本行随后将是一个整数S表示在该层停留了S分钟(1<=S<=10000000)。
数据保证在现实世界中,时间过了整数秒。
输出对于每组测试数据,输出现实世界过的时间(以秒为单位)。
样例输入16INSTAY 60INSTAY 1440OUTOUT样例输出396测试输入106INSTAY 60INSTAY 1440OUTOUT6INININOUTOUTOUT7INININSTAY 0 OUTOUTOUT2INSTAY 203INSTAY 0 OUT3INSTAY 10 OUT4INSTAY 10 STAY 10 OUT5INSTAY 20 STAY 20 OUT STAY 120 10INSTAY 20 STAY 20 INSTAY 1440STAY 1440OUTSTAY 120OUTSTAY 11STAY 50测试输出39660306073209723000参考代码:#include<stdio.h>int main(){int n;char a[5];scanf("%d",&n);while(n--){int m,i,b=1,c,time=0;scanf("%d",&m);for(i=0;i<m;i++){scanf("%s",&a);if(a[0]=='I') b*=20;else if(a[0]=='S') {scanf("%d",&c);time+=c*60/b;} else if(a[0]=='O') b/=20;}printf("%d\n",time);}return 0;}第二题:独木舟过河时间限制:3000ms 内存限制:65535KB 难度:★★描述进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别。
acm竞赛试题及答案ACM(Association for Computing Machinery)竞赛是一项全球性计算机科学竞赛,旨在锻炼参赛者的问题解决能力和编程技巧。
每年都有数千名来自不同学校的学生参加这一挑战。
本文将提供一些最近的ACM竞赛试题以及相应的答案,帮助读者了解和学习竞赛题目的类型和解题思路。
1. 问题描述给定一个由N个整数组成的数组A,请编写一个程序,找出数组中两个不同元素的差的最小值。
2. 输入格式- 第一行包含一个整数N,表示数组A的长度。
- 第二行包含N个以空格分隔的整数,表示数组A中的元素。
3. 输出格式输出一个整数,表示数组中两个不同元素的差的最小值。
4. 示例输入:51 52 9 12输出:15. 解题思路该问题可以通过对数组进行排序,并比较相邻两个数的差值来求解。
首先,将数组A进行升序排序。
然后,遍历排序后的数组,依次计算相邻两个数的差值,并记录其中的最小值。
最后,返回这个最小差值即可。
6. 代码实现```pythondef min_difference(nums):nums.sort() # 对数组进行升序排序min_diff = float('inf') # 初始化最小差值为正无穷大for i in range(len(nums)-1):diff = abs(nums[i] - nums[i+1]) # 计算相邻两个数的差值min_diff = min(min_diff, diff) # 更新最小差值return min_diff# 输入处理N = int(input())A = list(map(int, input().split()))# 调用函数并输出结果result = min_difference(A)print(result)```7. 答案解析对给定的数组进行排序后,遍历数组计算相邻两个数的差值,并记录其中的最小值。
上述代码中,首先将数组A进行升序排序,然后使用一个变量`min_diff`来记录最小差值。
1000 A + B ProblemProblem DescriptionCalculate A + B .InputEach line will contain two integers A and B. Process to end of file.OutputFor each case, output A + B in one line.Sample Input1 1Sample Output2AuthorHDOJ代码:#include<stdio.h>int main(){int a , b;while( scanf ( "%d %d" ,& a,& b)!= EOF)printf( "%d\n" , a+b);}1001 Sum ProblemProblem DescriptionHey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.InputThe input will consist of a series of integers n, one integer per line.OutputFor each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.Sample Input1100Sample Output15050AuthorDOOM III解答:#include<stdio.h>main (){int n , i , sum;sum =0;while (( scanf ( "%d" ,& n)!=- 1)) {sum for =0;( i =0; i <= n; i ++)sum +=i ;printf( "%d\n\n" , sum);}}1002 A + B Problem IIProblem DescriptionI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. InputThe first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.OutputFor each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.Sample Input21 2Sample OutputCase 1:1+2=3Case 2:Author代码:#include <stdio.h>#include <string.h>int main (){char str1 [ 1001 ], str2 [ 1001 ];int t , i , len_str1 , len_str2 , len_max , num = 1 , k ; scanf ( "%d" , & t );getchar ();while ( t --){int a [ 1001 ] = { 0}, b [1001]={ 0}, c [1001]={ 0};scanf ( "%s" , str1 );len_str1 = strlen ( str1 );for ( i = 0 ; i <= len_str1 - 1;++ i )a [ i ] = str1 [ len_str1 - 1 - i ] - '0' ;scanf ( "%s" , str2 );len_str2 = strlen ( str2 );for ( i = 0 ; i <= len_str2 - 1;++ i )b [ i ] = str2 [ len_str2 - 1 - i ] - '0' ;if ( len_str1 > len_str2 )len_max = len_str1 ;elselen_max = len_str2 ;k = 0 ;for ( i = 0 ; i <= len_max - 1 ;++ i ){c [ i ] = ( a[ i ] + b [ i ] + k ) % 10 ;k = ( a[ i ] + b [ i ] + k ) / 10 ;}if ( k != 0 )c [ len_max ] = 1 ;printf ( "Case %d:\n" , num );num ++;printf ( "%s + %s = " , str1 , str2 );if ( c[ len_max ] == 1 )printf ( "1" );for ( i = len_max - 1 ; i >= 0 ;-- i ){printf ( "%d" , c [ i ]);}printf ( "\n" );if ( t >= 1 )printf ( "\n" );}return 0 ;}1005 Number Sequence Problem DescriptionA number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.Given A, B, and n, you are to calculate the value of f(n).InputThe input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.OutputFor each test case, print the value of f(n) on a single line.Sample Input1 1 312100 0 0Sample Output25AuthorCHEN, ShunbaoSourceRecommendJGShining代码:#include<stdio.h>int f [ 200 ];int main (){int a , b, n, i ;while ( scanf ( "%d%d%d" ,& a,& b,& n)&& a&&b&&n){if ( n>= 3){f [ 1]= 1; f [ 2]= 1;for ( i =3; i <= 200 ; i ++){f [ i ]=( a* f [ i - 1]+ b* f [ i - 2])% 7;if ( f [ i - 1]== 1&&f [ i ]== 1)break ;}i -= 2;n =n%i ;if ( n== 0)printf ( "%d\n" , f [ i ]);elseprintf ( "%d\n" , f [ n]);}elseprintf ( "1\n" );}return 0 ;}1008 ElevatorProblem DescriptionThe highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevatorup one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.InputThere are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.OutputPrint the total time on a single line for each test case.Sample Input1 23231Sample Output1741AuthorZHENG, JianqiangSourceRecommendJGShining代码:#include<stdio.h>int a [ 110 ];int main(){int while { sum , i , n;( scanf ( "%d" ,& n)&& n!= 0)forscanf ( i =1; i <= n; i ++)( "%d" ,& a[ i ]);sum a for=0;[ 0]= 0;( i =1; i <= n; i ++){ifsum ( a[ i ]> a[ i - 1])+=6*( a[ i ]- a[ i - 1]);elsesum +=4*( a[ i - 1]- a[ i ]);sum +=5;printf ( "%d\n" , sum);}return 0 ;}1009 FatMouse' TradeProblem DescriptionFatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.InputThe input consists of multiple test cases. Each test case begins with a line containing two non-negative integersM and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test caseis followed by two -1's. All integers are not greater than 1000.OutputFor each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.Sample Input53724352203251824151510-1-1Sample OutputAuthorCHEN, YueSourceRecommendJGShining代码:#include<stdio.h>#include<string.h>#define MAX 1000int main(){int i,j,m,n,temp;int J[MAX],F[MAX];double P[MAX];double sum,temp1;scanf("%d%d",&m,&n);while(m!=-1&&n!=-1){sum=0;memset(J,0,MAX*sizeof(int));memset(F,0,MAX*sizeof(int));memset(P,0,MAX*sizeof(double));for(i=0;i<n;i++){ scanf("%d%d",&J[i],&F[i]); P[i]=J[i]*1.0/((double)F[i]); }for(i=0;i<n;i++){for(j=i+1;j<n;j++){if(P[i]<P[j]){temp1=P[i];P[i]=P[j];P[j]=temp1;temp=J[i]; J[i]=J[j]; J[j]=temp; temp=F[i];F[i]=F[j]; F[j]=temp;} }}for(i=0;i<n;i++) { if(m<F[i]){ else{sum+=m/((double)F[i])*J[i];sum+=J[i];break;m-=F[i];} }}printf("%.3lf\n",sum); scanf("%d%d",&m,&n); }return 0; }1021 Fibonacci AgainProblem DescriptionThere are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).InputInput consists of a sequence of lines, each containing an integer n. (n < 1,000,000).OutputPrint the word "yes" if 3 divide evenly into F(n). Print the word "no" if not.Sample Input0 1 2 3 4 5Sample Outputno no yes no no noAuthorLeojayRecommendJGShining#include<stdio.h> int main () { long while if printfn ;( scanf ( "%ld" ,& n) !=( n%8==2 || n %8==6) ( "yes\n" ); EOF ) elseprintf ( "no\n");return0 ;}1089 A+B for Input-Output Practice (I)Problem DescriptionYour task is to Calculate a + b.Too easy?! Of course! I specially designed the problem for acm beginners.You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.InputThe input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.Sample Input151020Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>main (){int a , b;while( scanf ( "%d%d" ,& a,& b)!=EOF)printf( "%d\n" , a+b);}1090 A+B for Input-Output Practice (II)Problem DescriptionYour task is to Calculate a + b.InputInput contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.Sample Input2151020Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>#define M 1000void main (){int a , b, n, j [ M], i ;//printf("please input n:\n");scanf( "%d" ,& n);for( i =0; i <n; i ++){scanf( "%d%d" ,& a,& b);//printf("%d %d",a,b);j[ i ]= a+b;}i=0;while( i <n){printf( "%d" , j [ i ]);i++;printf( "\n" );}}1091 A+B for Input-Output Practice(III) Problem DescriptionYour task is to Calculate a + b.InputInput contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line.A test case containing 0 0 terminates the input and this test case is not to be processed.OutputFor each pair of input integers a and b you should output the sum of a and b in one line, and with one line ofoutput for each line in input.Sample Input15102000Sample Output630AuthorlcyRecommendJGShining解答:#include<stdio.h>main (){int a , b;scanf( "%d %d" ,& a,& b);while(!( a== 0&&b==0)){printf( "%d\n" , a+b);scanf( "%d %d" ,& a,& b);}}1092 A+B for Input-Output Practice(IV) Problem DescriptionYour task is to Calculate the sum of some integers.InputInput contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.OutputFor each group of input integers you should output their sum in one line, and with one line of output for each line in input.。
acm大学生程序设计试题题目一:最大公约数(GCD)题目描述:给定两个正整数,求它们的最大公约数(GCD)。
输入两个正整数a和b(1 <= a, b <= 10^9),求它们的最大公约数。
输入格式:两个正整数,以空格分隔。
输出格式:输出一个整数,表示输入两个正整数的最大公约数。
示例:输入:14 21输出:7思路和分析:最大公约数(GCD)可以使用欧几里得算法来求解,即辗转相除法。
具体的步骤如下:1. 用较大的数除以较小的数,将得到的余数作为新的较大数。
2. 再用新的较大数除以较小数,将得到的余数作为新的较大数。
3. 如此重复,直到两个数可以整除,此时较小的数就是最大公约数。
代码实现:```cpp#include <iostream>using namespace std;int gcd(int a, int b) {if (b == 0)return a;return gcd(b, a % b);}int main() {int a, b;cin >> a >> b;int result = gcd(a, b);cout << result << endl;return 0;}```题目二:字符串反转题目描述:给定一个字符串,要求将其反转并输出。
输入一个字符串s(1 <= |s| <= 1000),输出该字符串的反转结果。
输入格式:一个字符串s,只包含大小写字母和数字。
输出格式:一个字符串,表示输入字符串的反转结果。
示例:输入:HelloWorld123输出:321dlroWolleH思路和分析:字符串反转可以使用双指针的方法来实现。
初始时,左指针指向字符串的开头,右指针指向字符串的末尾,然后交换左右指针所指向的字符,并向中间移动,直到左指针不小于右指针。
代码实现:```cpp#include <iostream>using namespace std;string reverseString(string s) {int left = 0, right = s.length() - 1; while (left < right) {swap(s[left], s[right]);left++;right--;}return s;}int main() {string s;cin >> s;string result = reverseString(s); cout << result << endl;return 0;}```题目三:字符串匹配题目描述:给定一个字符串s和一个模式串p,判断s中是否存在与p相匹配的子串。
2019年南华大学ACM校赛题解题解:A题题目意思很明确,求一个三项式展开式中的系数是多少。
首先需要了解最基本的公式没学过三项式展开怎么办因为我们学过二项式展开公式,三项式自然就是多展开一次可以了,接下来就是求的问题了由于此题数据量比较小,初略一看,直接暴力就可以写出,怎么暴力?求n的阶乘,放心循环就可以了(才1000的数据量),再做除法,取个模。
这样写有个致命的错误,就是除法的取余的问题,除法不满足拆开分别取余,再相除后取余的,如果是对质数取余,可以考虑下费小马+快速幂。
高级解法:杨辉三角,找找杨辉三角的层数和对应位置就知道,正好与之对应,中间过程取余,因为递推是加法,所以不影响最终的结果。
此题只要注意对中间数不断取余,就不会有太大问题。
B题暴力呗,签到题啊。
会写判质数就可以了。
C题并查集,也没有什么别的东西。
不过数据不是很强大。
不用压缩路径好像也能过。
统计一下一个集合有多少个元素减一就好了。
时间复杂度:O(m)+O(n)?D题DP吧。
先可以把删除长度所的到的价值用O(n^2)预处理一下。
得到删除长度所得到的最大价值。
设x[i]为删除长度i所得到的最大价值,等于max(x[j]+x[i-j],x[i])(0<=j<=i)(虽然不这么做也可以得到答案)。
考虑题目所给的操作,我们从中删除一段,再把前后拼接起来,如何设置状态?先看一个例子:11111011110000然后我们删除0后变为1111111110000答案等于单独的0加上{11111}11110000这样我们可以设置一个前缀,这样我们就可以表示任何地方删除的状态了,答案不会变。
我们可以压缩一下相同的元素。
(因为我们提前预处理过最大价值)设dp[l][r][v]表示l+1到r加上前缀的v的最大答案。
压缩后得到数组z。
我们来考虑一下状态的转移:1、我们直接删除前缀v,dp[l][r][v] = x[v] + dp[l+1][r][z[l+1]],2、然后我们删中间任意一段,dp[l][r][v]= max ( dp[l][r][v] ,dp[l+1][i][z[l+1]] + dp[i][r][v+z[i] ) (必须满足条件(l%2)==(i%2)因为只有这样删除才能把前缀合在一起。
【试题一】青蛙相遇问题【题目描述】两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。
它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。
可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。
不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。
但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。
为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。
我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。
设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。
青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。
纬度线总长L米。
现在要你求出它们跳了几次以后才会碰面。
Input 输入只包括一行5个整数x,y,m,n,L,其中x≠y < 2000000000,0 < m、n < 2000000000,0 < L < 2100000000。
Output 输出碰面所需要的跳跃次数,如果永远不可能碰面则输出一行"Impossible"示例Sample Input1 2 3 4 5Sample Output4题目要求;1.程序应该可以运行:通过控制台输入数据,输出数据通过printf给出;2.程序结构明了逻辑缜密、清晰,符合编码规范;3.程序功能正确;程序要注意效率;并且要求根据输入及时确认是否能够相遇,如果不能相遇请及早退出程序;程序思路:说明:可以将青蛙的路线看坐是一个圆判断其坐标如果下一次跳动后将饶过原点则把坐标转换这样无论青蛙在哪个位置上都用坐标来表示它如果两只青蛙坐标相等则相遇如果两只青蛙开始的坐标不一样但是速度一样那么将永远不能相遇【试题二】田忌赛马此题就是经典的田忌赛马,田忌和他的对手分别有n匹马,分别给出田忌的马的速度和对手的马的速度。
社会工作基础知识试题(含答案)一、单项选择题1.医务社会工作是在健康照顾体系内实施的社会工作。
属于对狭义的医务社会工作内容的是( )。
354A.在社区推动医疗保健与社会福利的整合B.在医疗保健机构开展心理—社会服务C.为促进公众健康开发社会资源D.为预防疾病开展全民健康教育【答案】C解析:狭义的医务社会工作是指在医疗保键机构中围绕疾病的诊断、治疗与康复过程所展开的社会工作专业服务,其内容主要包括协助病人及其家属解决与疾病相关的情绪问题、获取更多的资源以及对医疗过程的适应等。
C项符合狭义的医务社会工作的内容。
2.某社区服务机构为该社区残障人士及其特困家庭提供服务。
下列工作中属于时间进度管理方法的是( )。
A.计算该活动中政府拨款和社会捐赠资金总额B.3个月内教会30名智障人士手工制作肥皂技能C.提供联系方式以便服务对象能随时找到社会工作者D.通过张贴光荣榜等形式表彰优秀志愿者【答案】B解析:时间进度管理,首先是整个服务(活动)安排的期限管理,如在一周完成或在一个月内完成,其次是服务(活动)各个阶段的进展时间管理,再次是服务(活动)进行环节的时间管理,如志愿者精神培训要求45分钟完成,其中热身游戏要求3分钟完成等。
B项符合时间进度管理方法。
3.我国残疾预防分为一级、二级和三级。
下列预防措施中属于第三级预防的是( )。
168 A.开展新生儿计划免疫工作B.宣讲安全规划,推动安全教育C.及早发现伤病,及时治疗D.使用运动治疗方法减轻残疾【答案】D解析:三级预防是采取相应措施,预防残疾后产生各种障碍,通过运动治疗、作业治疗、语言治疗、心理治疗等康复功能训练方法改善功能,预防或减轻残疾。
4.为了加快培养现代建设人才,国家财政部等五部门自2011年开始实施自主就业退役士兵教育赞助政策。
这项政策满足的是退役士兵( )的需要。
216A.适应角色转变B.获取社会承认C.赢得社会尊重D.提高就业能力【答案】D解析:复员退伍军人安置社会工作服务对象的就业权益的保障需要是指当前复员退伍军人的需要主要集中在就业方面。
acm数学竞赛试题及答案# 题目一:数列问题问题描述:给定一个数列 \( a_1, a_2, a_3, \ldots, a_n \),数列中每个元素都是正整数,且满足 \( a_i = a_{i-1} + a_{i-2} \) 对于所有\( i \geq 3 \)。
如果 \( a_1 = 1 \) 且 \( a_2 = 1 \),请找出数列的第 \( n \) 项。
解答:根据题意,这是一个斐波那契数列。
第 \( n \) 项的值可以通过递归关系计算得出。
对于 \( n \) 的值,可以使用以下递归公式:\[ a_n = a_{n-1} + a_{n-2} \]其中,\( a_1 = 1 \) 和 \( a_2 = 1 \)。
因此,数列的前几项为 1, 1, 2, 3, 5, 8, 13, 21, ...。
对于任意的 \( n \),可以通过递归或动态规划方法计算出 \( a_n \)。
# 题目二:组合问题问题描述:从 \( n \) 个不同的元素中选择 \( k \) 个元素的所有可能组合的个数是多少?解答:这个问题可以通过组合数学中的二项式系数来解决。
从 \( n \) 个不同元素中选择 \( k \) 个元素的组合数 \( C(n, k) \) 可以用以下公式计算:\[ C(n, k) = \frac{n!}{k!(n-k)!} \]其中,\( n! \) 表示 \( n \) 的阶乘。
# 题目三:几何问题问题描述:在一个直角坐标系中,给定三个点 \( A(x_1, y_1) \),\( B(x_2, y_2) \) 和 \( C(x_3, y_3) \)。
如果 \( \overrightarrow{AB} \) 和 \( \overrightarrow{AC} \) 是垂直的,求证 \( A \) 是直角三角形 \( ABC \) 的直角顶点。
解答:如果 \( \overrightarrow{AB} \) 和 \( \overrightarrow{AC} \) 垂直,那么它们的数量积(点积)应该为零。
acm基础试题及答案1. 题目:给定一个整数数组,请找出数组中第二大的数。
答案:我们可以使用排序的方法,将数组从小到大排序,然后数组中的倒数第二个数就是第二大的数。
或者使用一次遍历的方法,首先初始化两个变量,一个用来存储最大值,一个用来存储第二大的值。
遍历数组,每次比较当前元素与最大值,如果当前元素大于最大值,则更新第二大的值为最大值,并将当前元素赋给最大值;如果当前元素小于最大值但大于第二大的值,则更新第二大的值。
2. 题目:实现一个函数,计算一个字符串中字符出现的次数。
答案:可以使用哈希表来实现,遍历字符串中的每个字符,将其作为键值对存储在哈希表中,键是字符,值是该字符出现的次数。
遍历结束后,哈希表中存储的就是每个字符出现的次数。
3. 题目:给定一个链表,删除链表的倒数第n个节点,并且返回新的链表头节点。
答案:可以使用双指针的方法,首先初始化两个指针,都指向链表的头节点。
然后移动第一个指针,移动n步,此时第一个指针指向倒数第n个节点的前一个节点。
接着同时移动两个指针,直到第一个指针到达链表的末尾,此时第二个指针指向的节点就是需要删除的节点的前一个节点。
然后更新第二个指针的next指针,使其指向第二个指针的next节点的next节点,最后返回链表的头节点。
4. 题目:编写一个函数,判断一个整数是否是回文数。
回文数是指正序和倒序读都一样的数。
答案:首先将整数转换为字符串,然后使用双指针的方法,一个指针从字符串的开始位置,一个指针从字符串的结束位置,向中间移动。
如果两个指针指向的字符不相等,则该整数不是回文数。
如果遍历结束后没有发现不相等的字符,则该整数是回文数。
5. 题目:给定一个字符串,找出其中不含有重复字符的最长子串的长度。
答案:可以使用滑动窗口的方法,维护一个哈希表记录窗口内字符的出现情况,以及一个变量记录不含有重复字符的最长子串的长度。
遍历字符串,每次移动窗口的右端点,如果当前字符不在窗口内,则更新最长子串的长度,并将字符添加到哈希表中。
2022南开大学acm竞赛题目及答案第一部分理论题(总分100分,考试时间:60分钟)一、选择题(单选,共30题,每个2分)1、下面关于变量及其作用范围的陈述哪个是不对的?()A.实例变量是类的成员变量。
B.实例变量用关键字static声明。
//Static 声明的是类变量C.在方法中定义的局部变量在该方法被执行时创建。
D.局部变量在使用前必须被初始化。
2、下面哪条语句把方法声明为抽象的公共方法?()A.public abstract method();B.public abstract void method();C.public abstract void method(){}D.public void method() extends abstract;3、哪个是将一个十六进制值赋值给一个long型变量?()A.long number = 345L;B.long number = 0345;C.long number = 0345L;D.long number = 0x345L;4、下面的哪个赋值语句是不对的?()A.float f = 11.1;B.double d = 5.3E12;C.double d = 3.14159;D.double d = 3.14D;5、main方法是Java Application程序执行的入口点,关于main 方法的方法头以下哪项是合法的()。
A、public static void main()B、public static void main(String[ ] args)C、public static int main(String[ ] args)D、public void main(String arg[ ])6、在Java中,一个类可同时定义许多同名的方法,这些方法的形式参数个数、类型或顺序各不相同,传回的值也可以不相同。
这种面向对象程序的特性称为()。
acm大学生程序试题及答案1. 题目:字符串反转描述:给定一个字符串,编写一个函数来将字符串中的字符按相反的顺序重新排列。
输入:一个字符串输出:反转后的字符串答案:```pythondef reverse_string(s):return s[::-1]```2. 题目:寻找最大数描述:给定一个整数数组,找出数组中的最大数。
输入:一个整数数组输出:数组中的最大数答案:```pythondef find_max(nums):return max(nums)```3. 题目:两数之和描述:给定一个整数数组和一个目标值,找出数组中和为目标值的两个数的索引(从1开始计数)。
输入:一个整数数组和一个目标值输出:两个数的索引,如果没有则返回空数组答案:```pythondef two_sum(nums, target):num_to_index = {}for i, num in enumerate(nums):complement = target - numif complement in num_to_index:return [num_to_index[complement] + 1, i + 1] num_to_index[num] = ireturn []```4. 题目:无重复字符的最长子串描述:给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。
输入:一个字符串输出:最长子串的长度答案:```pythondef length_of_longest_substring(s):char_map = {}start = max_length = 0for end in range(len(s)):if s[end] in char_map:start = max(start, char_map[s[end]] + 1)char_map[s[end]] = endmax_length = max(max_length, end - start + 1)return max_length```5. 题目:整数转罗马数字描述:将一个整数转换为罗马数字。
ACM软件大赛之编程大赛比赛注意事项:●比赛时间为3小时(180分钟);比赛分两个阶段:第一阶段限时30分钟,完成公示的3题,第二阶段限时150分钟(事先完成第一阶段题目的小组可提前进入第二阶段);●比赛第一阶段的3道题目将在前期宣传中告知参赛选手,比赛第二阶段的题目将由赛事主席当场公布竞赛题目;●前两阶段题目分为三个分值(5分、10分、15分),第一阶段3道公示题都为5分;第二阶段总共15道题,根据不同的难度分值不同,分别为5道5分题,5道10分题,5道15分题;第一阶段参赛队员不可参考任何相关资料;第二阶段参赛队员可以携带诸如书,手册,程序清单等参考资料。
比赛过程中队员不得携带任何电子媒质的资料;参赛者可以选择自己擅长的语言(C,C++,JAVA等等)进行编写●考虑到大一和大二学生的知识掌握程度,大一参加选手一开始就会有10分的分数,最后总分是由所做题目及初始的10分相加得到。
●每组队员根据安排使用电脑,小组人数为两人的使用一台电脑,超过两人的使用两台电脑,每台的电脑配置完全相同;●各小组每做完一题或几题,必须交予评委老师运行,评委老师当场给分;●如在比赛中发现作弊等行为,将取消比赛资格。
第一阶段公示题目:题目一:(5分)打印以下图形,纵遵从字母顺序,行字符数遵从斐波那契数列ABCCDDDEEEEEFFFFFFFFGGGGGGGGGGGGG#include<iostream>int f(int x){int a = 1 , b = 0;int max_ = x;int sum = 0;for(int i = 0; i < max_ ; i++){sum = a + b;a = b;b = sum;}return sum;}void loop_print(int num,char chr){for(int i = 0; i < num ;i++)std::cout<<chr;std::cout<<"\n";}int main(){int line_max = 7;char chr = 'A';for(int line = 0; line < line_max; line++){loop_print(f(line+1),chr);chr++;}return 0;}题目二:(5分)有个电子钟,12点显示为12:00(即12小时制),那么请问一天24时间,出现连续3个相同数字的钟点有几个?#include<iostream>using namespace std;bool check(int time){int h=time/100;int m=time-100*h;return h<=12&&m<=59&&h>0?true:false;//12小时制}int main(){int time=0;int j(0);//总计数器while(time<1270){//max 12:59int t=time;int n[4];for(int i=0;i<4;i++){n[i]=t%10;t /= 10;}if(n[1]==n[2]&&(n[0]==n[1]||n[3]==n[1])&&check(time)){//cout<<n[3]<<n[2]<<":"<<n[1]<<n[0]<<"\n";//testj++;}time++;}cout<<"total: "<<j*2<<endl;}题目三:(5分)10进制的四位数中有几个符合如下特征:将其分别表示为16进制、10进制、12进制,在每种状态下,分别将各个位上的数相加,能得到3个相等10进制数。
Qwertyuiopasdfghjklzxcvbnmqwertyui opasdfghjklzxcvbnmqwertyuiopasdfgh jklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzxc vbnmqwertyuiopasdfghjklzxcvbnmqw ertyuiopasdfghjklzxcvbnmqwertyuiopa sdfghjklzxcvbnmqwertyuiopasdfghjklz xcvbnmqwertyuiopasdfghjklzxcvbnmq wertyuiopasdfghjklzxcvbnmqwertyuio zxcvbnmqwertyuiopasdfghjklzxcvbnm qwertyuiopasdfghjklzxcvbnmqwertyui opasdfghjklzxcvbnmrtyuiopasdfghjklz xcvbnmqwertyuiopasdfghjklzxcvbnmq wertyuiopasdfghjklzxcvbnmqwertyuio pasdfghjklzxcvbnmqwertyuiopasdfghj Sincisco ACM 答案 2010/11/10 Sincisco 小组成员排球队员站位问题i【题目】排球队员站位问题┏━━━━━━━━┓图为排球场的平面图,其中一、二、三、四、五、六为位置编号,┃┃二、三、四号位置为前排,一、六、五号位为后排。
某队比赛时,┃┃一、四号位放主攻手,二、五号位放二传手,三、六号位放副攻┠──┬──┬──┨手。
队员所穿球衣分别为1,2,3,4,5,6号,但每个队┃ 四│ 三│ 二┃员的球衣都与他们的站位号不同。
已知1号、6号队员不在后排,┠──┼──┼──┨2号、3号队员不是二传手,3号、4号队员不在同一排,5号、┃ 五│ 六│ 一┃6号队员不是副攻手。
┗━━┷━━┷━━┛编程求每个队员的站位情况。
【算法分析】本题可用一般的穷举法得出答案。
也可用回溯法。
#include <cstdlib>#include <iostream>using namespace std;int counter=0;void display(int place[]){for(int i=1;i<=6;i++){cout<<place[i]<<"\t";if(i%3==0)cout<<"\n";}cout<<endl;}bool judge(int mark,int place[]){switch(mark){case 1:return true;case 2:return place[2]!=5&&place[2]!=6;case 3:return place[3]!=2&&place[3]!=3;case 4:return place[4]!=1&&place[4]!=2&&place[4]!=3&&place[4]!=6; case 5:return place[5]!=1&&place[5]!=5&&place[5]!=6;case 6:return place[6]!=1&&place[6]!=6; }}bool others(int place[]){int m=1,n=1;for(int i=0;i<=6;i++){if(place[i]==3)m=i-1;if(place[i]==4)n=i-1;}return m/3!=n/3;}void backtrace(int mark,int place[],int people[]){for(int i=1;i<=6;i++){if(people[i]!=0){place[mark]=people[i];if(judge(mark,place)){if(mark==6&&others(place)){counter++;display(place);}else{people[i]=0;backtrace(mark+1,place,people);people[i]=i;}}}}}int main(int argc, char *argv[]){int *place=new int[7];int *people=new int[7];for(int i=1;i<=6;i++)people[i]=i;backtrace(1,place,people);cout<<"The total methods is "<<counter<<endl;system("PAUSE");return EXIT_SUCCESS;}数字布置问题【题目】把1-8这8个数放入下图8个格中,要求相邻的格(横,竖,对角线)上填的数不连续.┌─┐│①│┌─┼─┼─┐│②│③│④│├─┼─┼─┤│⑤│⑥│⑦│└─┼─┼─┘│⑧│└─┘#include <cstdlib>#include <iostream>using namespace std;void display(int field[],int mark){for(int i=1;i<=mark;i++)cout<<field[i]<<"\t";cout<<endl;}bool continues(int m,int n){if((m-n==-1)||(m-n==1)){return true;}else{return false;}}bool judge(int mark,int field[]){switch(mark){case 1:return true;case 2:return true;case 3:if(continues(field[3],field[1])||continues(field[3],field[2]))return false;elsereturn true;case 4:if(continues(field[3],field[4]))return false;elsereturn true;case 5:if(continues(field[5],field[2]))return false;elsereturn true;case 6:if(continues(field[3],field[6])||continues(field[5],field[6]))return false;elsereturn true;case 7:if(continues(field[7],field[4])||continues(field[7],field[6]))return false;elsereturn true;case 8:if(continues(field[8],field[6]))return false;elsereturn true;}}void backtrace(int num[],int field[],int mark){for(int i=1;i<=8;i++){if(num[i]!=0){field[mark]=num[i];if(judge(mark,field)){if(mark==8)display(field,mark);else{num[i]=0;backtrace(num,field,mark+1);num[i]=i;}}}}}int main(int argc, char *argv[]){int *num;int *field;num=new int[9];field=new int[9];for(int i=0;i<9;i++){num[i]=i;}backtrace(num,field,1);system("PAUSE");return EXIT_SUCCESS;}改错题下列程序的功能是:一只甲虫在一个迷宫(一个10×10的矩阵表示)中移动,迷宫中有若干个柱子(在矩阵中表示为值为1的单元)。
甲虫从迷宫中的某个位置出发,按照预先设置的指令在迷宫中行进:指令是一串由1~4组成的数字,分别代表行进的4个方向:1代表向上,2代表向右,3代表向下,4代表向左。
当按照移动指令移动后的位置是一个柱子或超出了迷宫的范围则忽略该步指令。
最终程序将显示指令序列结束后的甲虫所处的位置。
修改后的结果:#include <iostream.h>int m[5][5]={1,0,0,1,0,0,1,0,1,1,0,0,0,0,0,1,0,1,0,0,0,1,1,0,0}; //迷宫,1-柱子int isPole(int x,int y){if(m[x][y]==1)return 1;elsereturn 0;}void move(int d[],int x[2],int n){int nx, ny;for(int i=0; i<n ; i++){switch(d[i]){case 1:ny = x[1]-1;break;case 2:nx = x[0]+1;break;case 3:ny = x[1]+1;break;case 4:nx = x[0]-1;break;default:break;}if(isPole(nx,ny)||nx<0||nx>4||ny<0||ny>4)continue;x[0]=nx; x[1]=ny;}}void main(){int x[2];int d[]={2, 2, 3, 3, 1, 3, 3, 4, 4, 1, 2, 3, 2, 2, 4}; //指令集cout<<"输入初始位置: ";cin>>x[0]>>x[1];move(d,x,sizeof(d[0])/sizeof(d));cout<<"The final position is: "<<x[0]<<", "<<x[1]<<endl;}2、给定一个整数n,求出所有连续的且和为n正整数。