ACM程序设计竞赛例题
ACM程序设计-赛题样例
ACM程序设计-赛题样例座位调整题目描述:百度办公区里到处摆放着各种各样的零食。
百度人力资源部的调研发现,员工如果可以在自己喜欢的美食旁边工作,工作效率会大大提高。
因此,百度决定进行一次员工座位的大调整。
调整的方法如下:1 .首先将办公区按照各种零食的摆放分成 N 个不同的区域。
(例如:可乐区,饼干区,牛奶区等等)。
2 .每个员工对不同的零食区域有不同的喜好程度(喜好程度度的范围为 1 — 100 的整数,喜好程度越大表示该员工越希望被调整到相应的零食区域)。
3 .由于每个零食区域可以容纳的员工数量有限,人力资源部希望找到一个最优的调整方案令到总的喜好程度最大。
数据输入:第一行包含两个整数 N , M ,( 1<=N ,M<=300 )。
分别表示 N 个区域和 M 个员工。
第二行是 N 个整数构成的数列 a ,其中 a[i] 表示第 i 个区域可以容纳的员工数,(1<=a[i]<=M , a[1]+a[2]+..+a[N]=M) 。
紧接着是一个 M*N 的矩阵 P , P ( i , j )表示第 i 个员工对第 j 个区域的喜好度。
答案输出:对于每个测试数据,输出可以达到的最大的喜好程度。
输入样例3 31 1 1100 50 25100 50 25100 50 25输出样例175#include<stdio.h>void main(){int n,m,l=0,i,j,s,t,sum=0;int a[100],p[100][100],b[100],c[100];for(i=0;i<100;i++){a[i]=0,c[i]=0,b[i]=0;for(j=0;j<100;j++)p[i][j]=0;}scanf("%d %d",&n,&m);for(i=0;i<n;i++){scanf("%d ",&a[i]);}for(i=0;i<n;i++){for(j=0;j<m;j++){scanf("%d",&p[i][j]);}}i=0;frist:if(i>=m)goto finish;if(l>0){for(s=0;s<l;s++){t=c[s];for(j=0;j<m;j++){p[t][j]=0;}}}for(s=0;s<m;s++){b[s]=0;}for(s=0;s<n;s++){b[s]=p[s][i];}for(s=0;s<n-1;s++){for(j=0;j<n-1-s;j++){if(b[j]<b[j+1]){t=b[j];b[j]=b[j+1]; b[j+1]=t;}}}// printf("%d\t",b[0]); // test++;for(s=0;s<a[i];s++){sum=sum+b[s];}s=0;sec:if(s>=a[i]){i++;goto frist;}for(t=0;t<n;t++){if(b[s]==p[t][i]){c[l]=t;l++;break;else continue;}s++;goto sec;finish:// printf("\n");printf("最大的喜好程度为:%d\n",sum);// printf("%d",test);}#include<stdio.h>#include<stdlib.h>void main(){int s,t,u,v,w,x,y,z,sum=0,a[20],b[20][20],c[20],d[20],sumx[80],*p=sumx,max; scanf("%d%d",&x,&y);for(z=0;z<x;z++){scanf("%d",&a[z]);}for(z=0;z<y;z++){c[z]=z;d[z]=z;}for(w=0;w<y;w++){for(z=0;z<x;z++){scanf("%d",&b[w][z]);}}sum=0;for(z=0;z<y;z++){sum+=b[0][z];for(w=z;w<y-1;w++)d[w]=d[w+1];}for(w=0;w<y-1;w++){s=d[w];sum+=b[1][s];for(v=w;v<y-2;v++){d[v]=d[v+1];}for(u=0;u<y-2;u++){s=d[u];sum+=b[1][s];for(t=u;t<y-3;t++){d[u]=d[u+1];}for(t=0;t<y-3;t++){s=d[t];sum+=b[2][s];for(z=0;z<y;z++){d[20]=c[20];}}*p=sum;sum=0;p++;}}}t=1;max=sumx[0];while(sumx[t]){if(sumx[t]>max)max=sumx[t];t++;}printf("%d\n",max);}#include<stdio.h>#include<malloc.h>int main(int argc,char** argv){int N,M,*a,**p,*temp,i,j,k,max=0;;printf("Please input area and staff num:");scanf("%d %d",&N,&M);a=(int*)malloc(sizeof(int)*N);printf("\nPlease input staff num of each area:");for(i=0;i<N;i++)scanf("%d",&a[i]);p=(int**)malloc(sizeof(int)*N);printf("\nPlease input like:");for(i=0;i<M;i++)p[i]=(int*)malloc(sizeof(int)*M);for(i=0;i<M;i++)for(j=0;j<N;j++)scanf("%d",&p[i][j]);temp=(int*)malloc(sizeof(int)*M);for(i=0;i<N;i++){if(i<N-1){for(j=0;j<M;j++){k=i+1;temp[j]=0;while(k<N){temp[j]+=p[j][i]-p[j][k];k++;}}int temp1,temp2;while(a[i]){temp1=-101;temp2=0;for(j=0;j<M;j++){if(temp[j]>temp1){temp1=temp[j];temp2=j;}}max+=p[temp2][i];printf("%d %d %d\n",p[temp2][i],temp2,i);for(int m=0;m<N;m++)p[temp2][m]=-101;printf("\n");for(int q=0;q<M;q++){for(int w=0;w<N;w++){printf(" %d ",p[q][w]);}printf("\n");}temp[temp2]=-101;a[i]--;}}else{int temp3,temp4;while(a[i]){temp3=-101;temp4=0;for(j=0;j<M;j++){if(p[j][i]>temp3){temp3=p[j][i];temp4=j;}}max+=p[temp4][i];printf("%d %d %d\n",p[temp4][i],temp4,i);for(int m=0;m<N;m++)p[temp4][m]=-101;printf("\n");for(int q=0;q<M;q++){for(int w=0;w<N;w++){printf(" %d ",p[q][w]);}printf("\n");}temp[temp4]=-101;a[i]--;}}}printf("Max=%d\n",max);return0;//Your program should return 0 on normal termination.}蝈蝈式的记分内容描述:蝈蝈小朋友刚刚学会了 0-9 这十个数字 , 也跟爸爸妈妈来参加百度每周进行的羽毛球活动。
山东科技大学第二届ACM程序设计大赛试题
山东科技大学第二届ACM程序设计大赛试题册试题共14页,题目共计12道山东科技大学第二届ACM 程序设计大赛试题册Problem A 简单计算Description给出n 个十进制的数,找出这n 个数的二进制表示中1的个数最少的数。
Input输入的第一行为一个正整数T (1≤T ≤20),代表测试数据组数。
对于每组测试数据,输入的第一行为一个正整数n (1≤n ≤10000),第二行为n个正整数A 1、A 2、…、A n (1≤A i ≤109),每个数之间以空格分隔。
Output每组数据输出一行,先输出数据组数,再输出二进制中含1最少的数,如果存在多个数符合条件,输出最小的那个。
具体输出格式见样例输出。
Sample Input Sample Output山东科技大学第二届ACM 程序设计大赛试题册Problem B 关键字搜索Description我们的新网站具有了全新的搜索功能,使用了2个通配符“*”和“?”,其中“*”表示0或者多个小写字母,“?”代表1个字母。
当我们输入一个关键字的时候,我们在不确定的地方就使用通配符。
我们在数据库里面有多条记录,每条记录都是由小写字母组成,现在给出一个关键字,你能告诉我数据库里面有多少条与关键字相匹配的记录吗?例如: 如果关键字是j*y*m*y?,那么jiyanmoyu ,jyanmoyu ,jymyu 都是相匹配的记录。
Input第一行输入一个T (T ≤20),表示有T 组测试数据。
对于每组测试数据,第一行是输入的关键字,接下是数据库里面的所有记录的条数n ,1≤n ≤10000,每条记录的长度不超过50个小写字母。
Output对于每组测试数据,输出与关键字相匹配的总记录条数,占一行。
Sample Input Sample Output山东科技大学第二届ACM 程序设计大赛试题册Problem C 正方形Description在二维坐标轴内给出四个点,这四个点能否构成一个正方形。
ACM软件大赛之编程大赛题目(附部分答案)
ACM 软件大赛之编程大赛比赛注意事项:l 比赛时间为3小时(小时(180180分钟);比赛分两个阶段:第一阶段限时30分钟,完成公示的3题,第二阶段限时150分钟(事先完成第一阶段题目的小组可提前进入第二阶段); l 比赛第一阶段的3道题目将在前期宣传中告知参赛选手,比赛第二阶段的题目将由赛事主席当场公布竞赛题目;主席当场公布竞赛题目;l 前两阶段题目分为三个分值(前两阶段题目分为三个分值(55分、分、1010分、分、1515分),第一阶段3道公示题都为5分;第二阶段总共15道题,根据不同的难度分值不同,分别为5道5分题,分题,55道10分题,分题,55道15分题;第一阶段参赛队员不可参考任何相关资料;第二阶段参赛队员可以携带诸如书,如书,手册,程序清单等参考资料。
手册,程序清单等参考资料。
手册,程序清单等参考资料。
比赛过程中队员不得携带任何电子媒质的资料;参比赛过程中队员不得携带任何电子媒质的资料;参赛者可以选择自己擅长的语言(赛者可以选择自己擅长的语言(C,C++,JAVA C,C++,JAVA 等等)进行编写等等)进行编写l 考虑到大一和大二学生的知识掌握程度,大一参加选手一开始就会有10分的分数,最后总分是由所做题目及初始的10分相加得到。
分相加得到。
l 每组队员根据安排使用电脑,小组人数为两人的使用一台电脑,超过两人的使用两台电脑,每台的电脑配置完全相同;脑,每台的电脑配置完全相同;l 各小组每做完一题或几题,必须交予评委老师运行,评委老师当场给分;各小组每做完一题或几题,必须交予评委老师运行,评委老师当场给分; l 如在比赛中发现作弊等行为,将取消比赛资格。
如在比赛中发现作弊等行为,将取消比赛资格。
第一阶段公示题目:题目一:(5分) 打印以下图形,纵遵从字母顺序,行字符数遵从斐波那契数列ABCCDDD EEEEEFFFFFFFFGGGGGGGGGGGGG#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 me){int h= me/100;int m= me-100*h;return h<=12&&m<=59&&h>0?true:false;//12小时制小时制}int main(){int me=0;int j(0);//总计数器总计数器while( me<1270){//max 12:59int t= me;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( me)){//cout<<n[3]<<n[2]<<":"<<n[1]<<n[0]<<"\n";//testj++;me++;}cout<<"total: "<<j*2<<endl;}题目三:(5分)10进制的四位数中有几个符合如下特征:将其分别表示为16进制、10进制、12进制,在每种状态下,分别将各个位上的数相加,能得到3个相等10进制数。
CDOJ第七届ACM趣味程序设计竞赛第三场(正式赛)题解
CDOJ第七届ACM趣味程序设计竞赛第三场(正式赛)题解宝贵资源题⽬连接:题意平⾯上给n个点(n<=1000),要求找⼀个⾯积最⼩的正⽅形,将所有的点都囊括进去。
要求正⽅形的边必须平⾏于坐标轴。
题解:对于这道题,我们可以⾸先找⼀个满⾜题意的,并且⾯积是最⼩的矩形。
假设矩形的长为L,宽为W,那么很显然:L = (MaxX - MinX)W = (MaxY - MinY)MaxX,MaxY 指题⽬中输⼊的最⼤横、纵坐标的值,MinX,MinY 指题⽬中输⼊的最⼩横,纵坐标的值。
因为我们需要得到正⽅形,那么正⽅形的边长L 取 max(L,W)即可。
这道题就结束了~代码如下:#include<iostream>#include<stdio.h>using namespace std;unsigned long long x1=99999999999LL,x2=0,y1=99999999999LL,y2=0;int main(){int n;scanf("%d",&n);for(int i=1;i<=n;i++){unsigned long long X ,Y;cin>>X>>Y;x1 = min(x1,X);x2 = max(x2,X);y1 = min(y1,Y);y2 = max(y2,Y);}unsigned long long l = max(x2-x1,y2-y1);cout<<l*l<<endl;}The Desire of Asuna题⽬连接:题意给你n条链,每条链的长度为a[i]每次操作你可以选择⼀条链,使得这条链的长度减1,然后使得任意两条链连接在⼀起,连接之后的长度要加1。
询问最少操作次数。
题解:贪⼼。
我们选择长度减⼩的链,⼀定是可选的,并且长度最⼩的链。
我们选择合并的链,⼀定是当前长度最⼤的两条链。
ACM程序大赛选拔初赛试题 - 参考答案
《解方程》参考答案:
#include <stdio.h> #include <math.h> void main() {
int a,b,c,d; float x1,x2; scanf("%d %d %d %d",&a,&b,&c,&d); if(a==0) printf("x=%.2f",(float)(d-c)/b); else {
float dr=b*b-4*a*c; c-=d; if(dr>0) {
x1=(-b-sqrt(dr))/(2*a); x2=(-b+sqrt(dr))/(2*a); printf("x1=%.2f x2=%.2f\n",x1,x2); } if(dr==0) { x1=(-b+sqrt(dr))/(2*a); printf("x=%.2f\n",x1); } if(dr<0) printf("no answer\n"); } }
3、计算器 (难度系数:★★★☆☆)
题意:输入一个没有答案的算术式子,用计算机求出输出它的结果。 输入:一个字符串形如“a#b=”(#代表运算符号)。注意:输入的字符串中 a,b 代表的数 都为自然数。而且 a,b 的范围都是从 0 到 999。 输出:字符串对应的算式的答案。答案需保留两位有效数字。若无答案则输出“error” 示例:
输出:第1 行是选出顺序,第2 行是两名幸运儿的开始位置(按升序排列),位置编号之 间用一个空格分开。
示例:
输入(只选一组) 3 5 12
输出(幸运儿的位置)
2 13 241 53 2 4 6 8 10 12 3 7 11 5 19
acm竞赛试题及答案
acm竞赛试题及答案ACM竞赛试题及答案1. 问题描述:给定一个整数数组,找出数组中没有出现的最小的正整数。
2. 输入格式:第一行包含一个整数n,表示数组的长度。
第二行包含n个整数,表示数组的元素。
3. 输出格式:输出一个整数,表示数组中没有出现的最小的正整数。
4. 样例输入:53 4 1 2 55. 样例输出:66. 问题分析:首先,我们需要理解题目要求我们找出数组中缺失的最小正整数。
这意味着我们需要检查数组中的每个元素,并确定最小的正整数是否在数组中。
7. 算法描述:- 遍历数组,使用一个哈希集合记录出现的数字。
- 从1开始,检查每个正整数是否在哈希集合中,直到找到不在集合中的最小正整数。
8. 代码实现:```pythondef find_missing_positive(nums):seen = set()for num in nums:if num <= 0:continuewhile num in seen or num > len(nums):num += 1seen.add(num)return min(set(range(1, len(nums) + 1)) - seen)```9. 测试用例:- 输入:[3, 4, -1, 1]- 输出:210. 答案解析:在给定的测试用例中,数组[3, 4, -1, 1]中没有出现的最小正整数是2。
这是因为-1不是正整数,所以可以忽略。
数组中已经出现了1和3,所以下一个最小的正整数就是2。
11. 注意事项:- 确保数组中的元素是整数。
- 考虑数组中可能包含0或负数的情况。
- 算法的时间复杂度应尽可能低。
12. 扩展思考:- 如果数组非常大,如何优化算法?- 如果数组中的元素可以是浮点数,算法应该如何修改?13. 参考答案:- 针对大数组,可以考虑使用更高效的数据结构,如平衡二叉搜索树。
- 如果元素是浮点数,需要先将其转换为整数,然后再进行处理。
acm大赛试题及答案
acm大赛试题及答案ACM大赛试题及答案1. 题目一:字符串反转问题描述:编写一个程序,输入一个字符串,输出其反转后的字符串。
输入格式:输入包含一个字符串,字符串长度不超过100。
输出格式:输出反转后的字符串。
示例:输入:hello输出:olleh答案:```pythondef reverse_string(s):return s[::-1]input_string = input().strip()print(reverse_string(input_string))```2. 题目二:计算阶乘问题描述:编写一个程序,输入一个非负整数n,输出n的阶乘。
输入格式:输入包含一个非负整数n,n不超过20。
输出格式:输出n的阶乘。
示例:输入:5输出:120答案:```pythondef factorial(n):if n == 0:return 1else:return n * factorial(n - 1)n = int(input())print(factorial(n))```3. 题目三:寻找最大数问题描述:给定一个包含n个整数的数组,找出数组中的最大数。
输入格式:输入包含一个整数n,表示数组的大小,随后是n个整数。
输出格式:输出数组中的最大数。
示例:输入:5 1 2 3 4 5输出:5答案:```pythonn = int(input())numbers = list(map(int, input().split()))max_number = max(numbers)print(max_number)```4. 题目四:判断闰年问题描述:编写一个程序,输入一个年份,判断该年份是否为闰年。
输入格式:输入包含一个整数,表示年份。
输出格式:如果输入的年份是闰年,则输出"Yes",否则输出"No"。
示例:输入:2000输出:Yes答案:```pythonyear = int(input())if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):print("Yes")else:print("No")```5. 题目五:斐波那契数列问题描述:编写一个程序,输入一个非负整数n,输出斐波那契数列的第n项。
acm试题及答案
acm试题及答案1. 问题描述:给定一个整数数组,找出数组中最长的连续子数组,使得子数组中所有元素的和等于给定的数值。
2. 输入格式:第一行包含两个整数 n 和 k,分别表示数组的长度和给定的数值。
第二行包含 n 个整数,表示整数数组。
3. 输出格式:输出最长的连续子数组的长度。
如果不存在这样的子数组,则输出 0。
4. 样例输入:```5 31 2 3 1 2```5. 样例输出:```3```6. 分析:要解决这个问题,我们可以遍历数组,使用一个哈希表来存储前缀和及其对应的索引。
当我们遇到相同的前缀和时,就可以计算出子数组的长度。
7. 算法:1. 初始化一个哈希表,用于存储前缀和及其索引。
2. 初始化一个变量 maxLen 来存储最长子数组的长度,初始值为 0。
3. 遍历数组,计算当前的前缀和。
4. 检查当前前缀和是否已经在哈希表中,如果在,则更新 maxLen。
5. 如果当前前缀和不在哈希表中,将其添加到哈希表中。
6. 最后输出 maxLen。
8. 代码实现:```pythondef find_max_length(nums, k):prefix_sum = {0: -1}max_len = 0current_sum = 0for i in range(len(nums)):current_sum += nums[i]if (current_sum - k) in prefix_sum:max_len = max(max_len, i - prefix_sum[current_sum - k])if current_sum not in prefix_sum:prefix_sum[current_sum] = ireturn max_lenn, k = map(int, input().split())nums = list(map(int, input().split()))print(find_max_length(nums, k))```9. 注意事项:- 确保输入的数组长度和给定数值是有效的。
acm编程比赛题
比赛试题主办方:迅翔计算机协会【问题描述】这是一个古老而又经典的问题。
用给定的几种钱币凑成某个钱数,一般而言有多种方式。
例如:给定了6种钱币面值为2、5、10、20、50、100,用来凑15元,可以用5个2元、1个5元,或者3个5元,或者1个5元、1个10元,等等。
显然,最少需要2个钱币才能凑成15元。
你的任务就是,给定若干个互不相同的钱币面值,编程计算,最少需要多少个钱币才能凑成某个给出的钱数。
【要求】【数据输入】输入可以有多个测试用例。
每个测试用例的第一行是待凑的钱数值M(1 <= M <= 2000,整数),接着的一行中,第一个整数K(1 <= K <= 10)表示币种个数,随后是K 个互不相同的钱币面值Ki(1 <= Ki <= 1000)。
输入M=0时结束。
【数据输出】每个测试用例输出一行,即凑成钱数值M最少需要的钱币个数。
如果凑钱失败,输出“Impossible”。
你可以假设,每种待凑钱币的数量是无限多的。
【样例输入】156 2 5 10 20 50 10011 2【样例输出】2Impossible【问题描述】Felicia 的生日是11月1日(和Kitty是同一天生的哦)。
于是Feli请来Kitty一起过生日。
Kitty带来了最新款的“Kitty猫”玩具准备送给Feli,不过她说,这份礼物可不是白送的。
Feli要帮她一个忙,才能够得到心仪已久的玩具。
Kitty说,“Kitty猫”玩具已经卖出了n!个,n<=10^100 *_*,Kitty想知道确切的数字,而不是无聊的“一个数加个感叹号”。
Feli 听了大吃一惊。
要知道,算出n!是一个无比艰巨的任务。
Feli告诉Kitty,就算Feli算出n!,Kitty也看不下去,因为当n=20 时,计算机的长整型已经存不下了(Kitty只能接受1-9之间的数字)。
于是Kitty说,你只要告诉我n!最后一位非0的数就可以了。
ACM比赛试题
The 35th ACM-ICPC Asia Regional Contest (Hangzhou)Contest SectionOctober 24, 2010Sponsored by IBM & AlibabaZhejiang Sci-Tech UniversityThis problem set should contain 10 problems on numbered 24 pages. Please inform a runner immediately if something is missing from your problem set.Problem A. Naughty fairiesDescriptionOnce upon a time, there lived a kind of fairy in the world. Those fairies could hear the voice of fruit trees, and helped people with a harvest. But people did n’t know that fruits are also those fairies’ favorite food. After the fairies ate people’s fruits, they always did something to cover it up.One day a little fairy named Lily flew into an orchard and found a large peach tree. Hungry as Lily was, she started eating without thinking until her stomach was full. In the fairy world, when a fairy ate the fruits in a fruit tree, sometimes the fruit tree would feel honored and bore more fruits immediately. That’s why sometimes the number of fruits in a tree got increased after a fairy ate fruits of that tree.But the fairies didn’t want people to find out weird things such as fruits become more or less suddenly. Lily decided to use a magic spell so that the orchard owner couldn’t find the change of the number of p eaches.Suppose there were N peaches on a tree originally and there were M peaches left after Lily was full. M may be greater than, less than or equal to N. All M peaches were visible at first, and Lily wanted to make an illusion so that exactly N peaches are visible.Lily can do 3 kinds of spell to change the total visible number of peaches:1) “PAPADOLA”:This spell would increase the number of visible peaches by one.2) “EXPETO POTRONUM”:This spell would double the number of visible peaches.3) “SAVIDA LOHA”:This spell would decrease the number of visible peaches by one. Each spell would take one minute and Lily wanted to finish as fast as possible. Now please tell Lily the least time she needs to change the number of visible peaches to N.InputThere are several test cases, ended by “0 0”.For each test case, there are only one line containing two numbers separated by a blank, N and M, the original numbers of peaches and the numbers of peaches left(0<N,M<10500).There is no leading zero.OutputFor each test case, you should output just a number K indicating the minimum time (in minutes) Lily needed to finish her illusion magic.Sample Input5 21 9986 320 0Sample Output29812Problem B. Prison BreakDescriptionRompire is a robot kingdom and a lot of robots live there peacefully. But one day, the king of Rompire was captured by human beings. His thinking circuit was changed by human and thus became a tyrant. All those who are against him were put into jail, including our clever Micheal#1. Now it’s time to escape, but Micheal#1 needs an optimal plan and he contacts you, one of his human friends, for help.The jail area is a rectangle contains n×m little grids, each grid might be one of the following:1) Empty area, represented by a capital letter ‘S’.2) The starting position of Micheal#1, represented by a capital letter ‘F’.3) Energy pool, represented by a capital letter ‘G’. When entering a n energy pool, Micheal#1 can use it to charge his battery ONLY ONCE. After the charging, Micheal#1’s batt ery will become FULL and the energy pool will become an empty area. Of course, passing an energy pool without using it is allowed.4) Laser sensor, represented by a capital letter ‘D’. Since it is extremely sensitive, Micheal#1 cannot step into a grid with a laser sensor.5) Power switch, represented by a capital letter ‘Y’. Once Micheal#1 steps into a grid with a Power switch, he will certainly turn it off.In order to escape from the jail, Micheal#1 need to turn off all the power switches to stop the electric web on the roof—then he can just fly away. Moving to an adjacent grid (directly up, down, left or right) will cost 1 unit of energy and only moving operation costs energy. Of course, Micheal#1 cannot move when his battery contains no energy.The larger the battery is, the more energy it can save. But larger battery means more weight and higher probability of being found by the weight sensor. So Micheal#1 needs to make his battery as small as possible, and still large enough to hold all energy he need. Assuming that the size of the battery equals to maximum units of energy that can be saved in the battery, and Micheal#1 is fully charged at the beginning, Please tell him the minimum size of the battery needed for his Prison break.InputInput contains multiple test cases, ended by 0 0. For each test case, the first line contains two integer numbers n and m showing the size of the jail. Next n lines consist of m capital letters each, which stands for the description of the jail.You can assume that 1<=n,m<=15, and the sum of energy pools and power switches is less than 15.OutputFor each test case, output one integer in a line, representing the minimum size of the battery Micheal#1 needs. If Micheal#1 can’t escape, output -1.Sample Input5 5GDDSSSSSFSSYGYSSGSYSSSYSS0 0Sample Output4Problem C. To Be an Dream Architect DescriptionThe “dream architect” is the key role in a team of “dream extractors” who enter other’s dreams to steal secrets. A dream architect is responsible for crafting the virtual world that the team and the target will dream into. To avoid the target noticing the world is artificial, a dream architect must have powerful 3D imagination.Cobb uses a simple 3D imagination game to test whether a candidate has the potential to be an dream architect. He lets the candidate imagine a cube consisting of n×n×n blocks in a 3D coordinate system as Figure 1. The block at bottom left front corner is marked (1, 1, 1) and the diagonally opposite block is marked (n, n, n). Then he tells the candidate that the blocks on a certain line are eliminated. The line is always parallel to an axis. After m such block eliminations, the candidate is asked to tell how many blocks are eliminated. Note that one block can only be eliminated once even if it is on multiple lines.Here is a sample graph according to the first test case in the sample input:InputThe first line is the number of test cases.In each test case, the first line contains two integers n and m( 1 <= n <= 1000, 0 <= m <= 1000).,meaning that the cube is n x n x n and there are m eliminations.Each of the following m lines represents an elimination in the following format:axis_1=a, axis_2=bwhere axis_i (i=1, 2) is ‘X’ or ‘Y’, or ‘Z’ and axis_1 is not equal to axis_2. a and b ar e 32-bit signed integers.OutputFor each test case output the number of eliminated blocks.Sample Input23 2Y=1,Z=3X=3,Y=110 2X=3,Y=3Y=3,Z=3Sample Output519Problem D. GomokuDescriptionYou are probably not familiar with the title, “Gomoku”, but you must have played it a lot. Gomoku is an abstract strategy board game and is also called Five in a Row, or GoBang. It is traditionally played with go pieces (black and white stones) on a go board (19x19 intersections). Nowadays, standard chessboard of Gomoku has 15x15 intersections. Black plays first, and players alternate in placing a stone of their color on an empty intersection. The winner is the first player to get an unbroken row of five or more stones horizontally, vertically, or diagonally.For convenience, we coordinate the chessboard as illustrated above. The left-bottom intersection is (0,0). And the bottom horizontal edge is x-axis, while the left vertical line is y-axis.I am a fan of this game, actually. However, I have to admit t hat I don’t have a sharp mind. So I need a computer program to help me. What I want is quite simple. Given a chess layout, I want to know whether someone can win within 3 moves, assuming both players are clever enough. Take the picture above for example. There are 31 stones on it already, 16 black ones and 15 white ones. Then we know it is white turn. The white player must place a white stone at (5,8). Otherwise, the black player will win next turn. After that, however, the white player also gets a perfect situation that no matter how his opponent moves, he will win at the 3rd move.So I want a program to do similar things for me. Given the number of stones and positions of them, the program should tell me whose turn it is, and what will happen within 3 moves.InputThe input contains no more than 20 cases.Each case contains n+1 lines which are formatted as follows.nx1 y1 c1x2 y2 c2......x n y n c nThe first integer n indicates the number of all stones. n<=222 which means players have enough space to place stones. Then n lines follow. Each line contains three integers: x i and y i and c i. x i and y i are coordinates of the stone, and ci means the color of the stone. If c i=0 the stone is white. If c i=1 the stone is black. It is guaranteed that 0<=x i,y i<=14, and c i=0 or 1. No two stones are placed at the same position. It is also guaranteed that there is no five in a row already, in the given cases.The input is ended by n=0.OutputFor each test case:First of all, the program should check whose turn next. Le t’s call the player who will move next “Mr. Lucky”. Obviously, if the number of the black stone equals to the number of white, Mr. Lucky is the black player. If the number of the black stone equals to one plus the numbers of white, Mr. Lucky is the white player. If it is not the first situation or the second, print “Invalid.”A valid chess layout leads to four situations below:1)Mr. Lucky wins at the 1st move. In this situation, print :Place TURN at (x,y) to win in 1 move.“TURN” must be replaced by “black” or “white” according to the situation and (x,y) is the position of the move. If there are different moves to win, choose theone where x is the smallest. If there are still different moves, choose the one where y is the smallest.2)Mr. Lucky’s opp onent wins at the 2nd move. In this situation, print:Lose in 2 moves.3)Mr. Lucky wins at the 3rd move. If so, print:Place TURN at (x,y) to win in 3 moves.“TURN” should replaced by “black” or “white”, (x,y) is the position where the Mr.Lucky should place a stone at the 1st move. After he place a stone at (x,y), no matter what his opponent does, Mr. Lucky will win at the 3rd step. If there are multiple choices, do the same thing as described in situation 1.4)Nobody wins within 3 moves. If so, print:Cannot win in 3 moves.Sample Input313 3 13 4 03 5 03 6 04 4 14 5 14 7 05 3 05 4 05 5 15 6 15 7 15 9 16 4 16 5 16 6 06 7 16 8 06 9 07 5 17 6 07 7 17 8 17 9 08 5 08 6 18 7 08 8 18 9 09 7 110 8 017 7 117 7 0Sample OutputPlace white at (5,8) to win in 3 moves. Cannot win in 3 moves.Invalid.Problem E. GunshotsDescriptionPresident Bartlet was shot! A group of terrorists shot to the crowd when President Bartlet waved to cheering people after his address. Many people were shot by the irrational bullets. Senior FBI agent Don Epps takes responsibility for this case. According to a series of crime scene investigation, including analyzing shot shells, replaying video from closed-circle television and collecting testimony by witnesses, Don keeps all the information about where and how the terrorists shot to crowd, as well as the location of every single person when the gun shoot happened. Now he wants to know how many gunshot victims are there in this case.Imagine that each target person can be regarded as a polygon (can be concave or self-intersecting) and each gunshot can be regarded as a half-line. The bullet will be stopped by the first person it shoots. A person can be shot in three ways:To simplify the problem, we assume that any two polygons can be completely separated by a line. Also each start point of the gunshot can be separated from each polygon by a line. Now given M people and N gunshots, please work out which person has been shot by each bullet.InputThere are multiple test cases in the input. The first line of the input file is an integer T demonstrating the number of test cases. (T<=10).For each test case, the first line is an integer N, representing the number of people (polygons). Following lines demonstrates the polygons. For the i th polygon (0<=i<N), the first line is an integer Q i , representing the number of edges of this polygon. In each of the following Q i lines, there are two real numbers x i and y i representing a point. Every pair of adjacent points demonstrate an edge of this polygon (i.e. (x i , y i ) to (x i+1, y i+1) is an edge, in which 0<=i<Q i -1), and (x Qi-1, y Qi-1) to (x 0, y 0) also demonstrates an edge of this polygon.PersonPerson Person 1. Normal shot 2. The bullet’s path isparallel to an edge 3. The bullet’s path is tangent to an vertexThen there is a line contains an integer M representing the number of gunshots. In the following M lines, each line contains four real numbers x, y, dx and dy, representing the start point (x, y) and direction vector (dx, dy) of that gunshot.In all test cases, we assume that 0< N<=100, 0<Q i<=1000, 0<M<=10000.OutputFor each test case, output contains M lines and the i th line demonstrates the result of the i th gunshot.If the i th gunshot shoots the j th polygon, the i th line contains “HIT j”, otherwise it contains a word “MISS” (means that it does not shoot any target). The polygons are numbered in the order of their appearance in the input file, and the numbers start from 0.At the end of each test case, please output a single line with “*****”.Sample Input1140 01 10 11 02-1 0 1 0-2 0 -1 0Sample OutputHIT 0MISS*****HintThe figure of the first case in the samples is as follows:Problem F. Rotational PaintingDescriptionJosh Lyman is a gifted painter. One of his great works is a glass painting. He creates some well-designed lines on one side of a thick and polygonal glass, and renders it by some special dyes. The most fantastic thing is that it can generate different meaningful paintings by rotating the glass. This method of design is called “Rotatio nal Painting (RP)” which is created by Josh himself.You are a fan of Josh and you bought this glass at the astronomical sum of money. Since the glass is thick enough to put erectly on the table, you want to know in total how many ways you can put it so that you can enjoy as many as possible different paintings hiding on the glass. We assume that material of the glass is uniformly distributed. If you can put it erectly and stably in any ways on the table, you can enjoy it.More specifically, if the polygonal glass is like the polygon in Figure 1, you have just two ways to put it on the table, since all the other ways are not stable. However, the glass like the polygon in Figure 2 has three ways to be appreciated.Pay attention to the cases in Figure 3. We consider that those glasses are not stable.InputThe input file contains several test cases. The first line of the file contains an integer T representing the number of test cases.For each test case, the first line is an integer n representing the number of lines of the polygon. (3<=n<=50000). Then n lines follow. The i th line contains two real number x i and y i representing a point of the polygon. (x i, y i) to (x i+1, y i+1) represents a edge of the polygon (1<=i<n), and (x n,y n) to (x1, y1) also represents a edge of the polygon. The input data insures that the polygon is not self-crossed.OutputFor each test case, output a single integer number in a line representing the number of ways to put the polygonal glass stably on the table.Sample Input240 0100 099 11 160 00 101 101 110 110 0Sample Output23HintThe sample test cases can be demonstrated by Figure 1 and Figure 2 in Description part.Problem G. Traffic Real Time Query System DescriptionCity C is really a nightmare of all drivers for its traffic jams. To solve the traffic problem, the mayor plans to build a RTQS (Real Time Query System) to monitor all traffic situations. City C is made up of N crossings and M roads, and each road connects two crossings. All roads are bidirectional. One of the important tasks of RTQS is to answer some queries about route-choice problem. Specifically, the task is to find the crossings which a driver MUST pass when he is driving from one given road to another given road.InputThere are multiple test cases.For each test case:The first line contains two integers N and M, representing the number of the crossings and roads.The next M lines describe the roads. In those M lines, the i th line (i starts from 1)contains two integers X i and Y i, representing that road i connects crossing X i and Y i (X i≠Y i).The following line contains a single integer Q, representing the number of RTQs. Then Q lines follows, each describing a RTQ by two integers S and T(S≠T) meaning that a driver is now driving on the road s and he wants to reach road t . It will be always at least one way from road s to road t.The input ends with a line of “0 0”.Please note that: 0<N<=10000, 0<M<=100000, 0<Q<=10000, 0<X i,Y i<=N, 0<S,T<=M OutputFor each RTQ prints a line containing a single integer representing the number of crossings which the driver MUST pass.Sample Input5 61 22 33 44 53 522 32 40 0Sample Output 01Problem H. National Day ParadeDescriptionThere are n×n students preparing for the National Day parade on the playground. The playground can be considered as a n×m grid. The coordinate of the west north corner is (1,1) , and the coordinate of the east south corner is (n,m).When training, every students must stand on a line intersection and all students must form a n×n square. The figure above shows a 3×8 playground with 9 students training on it. The thick black dots stand for the students. You can see that 9 students form a 3×3 square.After training, the students will get a time to relax and move away as they like. To make it easy for their masters to control the training, the students are only allowed to move in the east-west direction. When the next training begins, the master would gather them to form a n×n square again, and the position of the square doesn’t matter. Of course, no student is allowed to stand outside the playground.You are given the coordinates of each student when they are having a rest. Your task is to figure out the minimum sum of distance that all students should move to form a n×n square.InputThere are at most 100 test cases.For each test case:The first line of one test case contain two integers n,m. (n<=56,m<=200)Then there are n×n lines. Each line contains two integers, 1<=X i<=n,1<= Y i<=m indicating that the coordinate of the i th student is (X i , Y i ). It is possible for more than one student to stand at the same grid point.The input is ended with 0 0.OutputYou should output one line for each test case. The line contains one integer indicating the minimum sum of distance that all students should move to form a n×n square. Sample Input2 1682 1011 1271 1052 900 0Sample Output41Problem I. SearchlightsDescriptionThere is a piece of grids land of size n×m. Chandler and his team take responsibility to guard it. There are some searchlights on some pieces and each of them has a capability to lighten a distance towards four directions: north, south, east and west. Different searchlight has different lightening capability shown in levels. Searchlight with level k means that it can lighten k grids (including the gird that the searchlight stands in) along any of the four directions. Shown in following figure, there is a searchlight of level 3 and the shadow grids are ones that can be lightened by it. Particularly, searchlight of level 1 means that it can only lighten the grid in which the searchlight stands.Figure: A searchlight of Level 3Each searchlight has a maximum level. You can decrease a searchlight’s level to save the energy. A searchlight whose maximum level is k can be turned to level k, k-1, k-2, …, 1 and 0. Level 0 means turning off the searchlight.A grid is well-guarded if and only if at least one of the following two conditions is satisfied:1.There is a searchlight in this grid, and it is not switched to level 0 (the light is on).2.The grid is lightened by at least two searchlights. One lightens it in horizontaldirection (east or west), and another lightens it in vertical direction (north or south).Chandler asks you to help finding a solution that he can turn on some of the searchlights so that:1.All the grids are well-guarded.2.All the searchlights turned on are in a same level.3.That same level mentioned above is as small as possible.More specifically, if you choose a same level Q, then all the searchlights whose maximum level are less than Q have to be turned off. Please help him to find a solution with the minimum same level.InputThe input file contains several test cases.For each test case, the first line is two integers n and m, representing a grids land of size n×m. (0<n<=100, 0<m<=10000). Following n lines describe an n×m matrix in which a i,j means the maximum level of the searchlight in grid (i, j). a i,j can be zero, which means there is no searchlight on that grid. For all the cases, a i, j<=10000.The input file ends with a line containing two zeros.OutputFor each test case, output a single line with an integer, representing the minimum level you have found. If there is no such a solution, output “NO ANSWER!”Sampl e Input2 20 23 02 20 21 00 0Sampl e Output2NO ANSWER!Problem J. Infinite monkey theorem DescriptionCould you imaging a monkey writing computer programs? Surely monkeys are smart among animals. But their limited intelligence is no match for our human beings. However, there is a theorem about monkeys, and it states that monkeys can write everything if given enough time.The theorem is called “Infinite monkey theorem”. It states that a monkey hitting keys at random on a typewriter keyboard for an infinite amount of time will almost surely type any given text, which of course includes the programs you are about to write (All computer programs can be represented as text, right?).It’s very easy to prove this theorem. A little calculation will show you that if the monkey types for an infinite length of time the probability that the output contains a given text will approach 100%.However, the time used is too long to be physically reasonable. The monkey will not be able to produce any useful programs even if it types until the death of the universe. To verify this and ensure that our human beings are not replaceable by monkeys, you are to calculate the probability that a monkey will get things right.InputThere will be several test cases.Each test case begins with a line containing two integers n and m separated by a whitespace (2<=n<=26, 1<=m<=1000). n is the number of keys on the typewriter and the monkey will hit these keys m times. Thus the typewriter will finally produce an output of m characters.The following n lines describe keys on the typewriter. Each line has a lower case letter and a real number separated by a whitespace. The letter indicates what the typewriter will produce if the monkey hits that key and the real number indicates the probability that the monkey will hit this key. Two hits of the monkey are independent of each other (Two different hits have the same probability for a same key), and sum of all the probabilities for each key is ensured to be 1.The last line of the test case contains a word composed of lower case letters. The length of the word will be less than or equal to 10.The input will end with a line of two zeros separated by a whitespace. This line should not be processed.OutputFor each test case, output one line containing the probability that the given word will appear in the typewriter’s output. The output should be in percentage format and numbers should be rounded to two digits after the decimal point.Sampl e Input4 10w 0.25o 0.25r 0.25d 0.25word2 10a 1.0b 0.0abc2 100a 0.312345b 0.687655abab0 0Sampl e Output2.73%0.00%98.54%。
acm大学生程序设计试题
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相匹配的子串。
大一acm竞赛试题及答案
大一acm竞赛试题及答案一、选择题(每题5分,共20分)1. 下列哪个算法的时间复杂度为O(n^2)?A. 快速排序B. 归并排序C. 插入排序D. 冒泡排序答案:C2. 在C++中,下列哪个关键字用于定义类?A. structB. classC. unionD. enum答案:B3. 下列哪个数据结构适合用于实现稀疏矩阵?A. 顺序存储B. 链式存储C. 压缩存储D. 散列存储答案:C4. 在图论中,下列哪个算法用于寻找最短路径?A. 深度优先搜索B. 广度优先搜索C. 迪杰斯特拉算法D. 弗洛伊德算法二、填空题(每题5分,共20分)1. 在二叉树的遍历算法中,______遍历会先访问根节点。
答案:前序2. 哈希表的冲突解决方法之一是______。
答案:链地址法3. 在数据库中,用于实现一对多关系的表结构是______。
答案:外键4. 动态规划算法的核心是______。
答案:状态转移方程三、编程题(每题30分,共60分)1. 编写一个函数,实现对一个整数数组进行排序,并返回排序后的数组。
答案:```pythondef sort_array(arr):arr.sort()return arr```2. 编写一个函数,实现计算给定整数n的阶乘。
答案:```pythondef factorial(n):if n == 0:return 1return n * factorial(n - 1)```四、算法题(每题30分,共30分)1. 给定一个整数数组,请设计一个算法找出数组中第二大的数。
答案:```pythondef find_second_max(nums):first_max = second_max = float('-inf')for num in nums:if num > first_max:second_max = first_maxfirst_max = numelif num > second_max and num != first_max:second_max = numreturn second_max```。
ACM程序设计竞赛例题
ACM程序设计竞赛例题备战ACM资料习题1. 0-1背包问题在0 / 1背包问题中,需对容量为c 的背包进行装载。
从n 个物品中选取装入背包的物品,每件物品i 的重量为wi ,价值为pi 。
对于可行的背包装载,背包中物品的总重量不能超过背包的容量,最佳装载是指所装入的物品价值最高。
程序如下:#includevoid readdata();void search(int);void checkmax();void printresult();int c=35, n=10; //c:背包容量;n:物品数int w[10], v[10]; //w[i]、v[i]:第i件物品的重量和价值int a[10], max; //a数组存放当前解各物品选取情况;max:记录最大价值//a[i]=0表示不选第i件物品,a[i]=1表示选第i件物品int main() {readdata(); //读入数据search(0); //递归搜索printresult();}void search(int m){if(m>=n)checkmax(); //检查当前解是否是可行解,若是则把它的价值与max 比较else{a[m]=0; //不选第m件物品search(m+1); //递归搜索下一件物品a[m]=1; //不选第m件物品search(m+1); //递归搜索下一件物品}}void checkmax(){int i, weight=0, value=0;for(i=0;i<n;i++)< p="">{if(a[i]==1) //如果选取了该物品{weight = weight + w[i]; //累加重量value = value + v[i]; //累加价值}}if(weight<=c) //若为可行解if(value>max) //且价值大于maxmax=value; //替换max}void readdata(){int i;for(i=0;i<n;i++)< p="">scanf("%d%d",&w[i],&v[i]); //读入第i件物品重量和价值}void printresult(){printf("%d",max);}2.装载问题有两艘船,载重量分别是c1、c2,n个集装箱,重量是wi (i=1…n),且所有集装箱的总重量不超过c1+c2。
ACM程序设计竞赛II第一章
•
if(q >= y || (p < m && A[p] <= A[q]))//右边空,或者两边都不空且右边大
•
T[i++] = A[p++];//复制左边的
•
else
•3;] = A[q++];//复制右边的
•
*cnt += m-p;//是逆序数的数目
•
}
•}
• for(i = x; i < y; i++) A[i] = T[i];
count++;
printf("%d\n",count);
}
2021/8r/e2turn 0;
11
}
首先抽象数学模型
• 数据如何存储
– 问题一:顺序表、链表? – 问题三:二维数组?
• 对模型选择适当算法
– 问题one by one
• 求解
– 此处省略1万字
2021/8/2
12
关于字符串
• 基本:长度、拷贝、连接、比较、反串、判断回文 • 进阶:子串(模式匹配)
照号码的字典升序输出。如果输入数据中没有重复的号码,输出一行:
No duplicates
(注2意021N/8大/2 写)
19
Sample Input
问题六—(HDOJ-1800)
Flying to the Mars
2021/8/2
20
字符串哈希 HDOJ-1800题
• 除去马甲,本题的本质是——求相同级别(level)的人 最多是几个。
• 输出:每一行一个整数x,对应一组case,表示该组case的 字符串中所包含的最长回文长度.
福建农林大学思博杯第一届ACM程序设计大赛题目
福建农林大学思博杯第一届ACM程序设计大赛题目福建农林大学思博杯第一届ACM程序设计大赛题目1.多项式相乘(本题30分)(conv.cpp/c)【题目描述】编程实现若干个多项式相乘。
多项式的输入输出格式为:系数在前,指数在后,各项按指数递增排列,每个多项式输入时以两个0结束。
系数为0的项不输出。
例如:1+4X3-9X5输入格式为:1 0 0 1 0 2 4 3 0 4 -9 5 0 0或者 1 0 4 3 -9 5 0 0,其输出只能是:1 0 4 3 -9 5【输入】输入文件conv.in每行为一个多项式,多项式输入时以两个0结束。
数据为若干行的多项式,例如:1 0 1 1 0 01 0 -1 1 0 01 0 12 0 0表示(1+x)(1-x)(1+x2)【输出】输出文件conv.out包含1行,为上述多项式相乘结果。
上例的输出为:1 0 -1 4表示1-x4所有系数、指数均为整数(int类型)2.Total求和(本题60分)(total.cpp/c)【问题描述】编写程序实现从一字符串str中取出连续的数字作为一个正整数,计算所有这些正整数之和。
例如字符串str="abc123 x456,xy16639ghks-7890# zxy",按题目要求可以取出4个正整数:123,456,16639,7890。
这4个正整数之和为:25108【输入】输入文件total.in只有一行,是一个字符串(1≤字串长度≤500)。
【输出】输出文件total.out也只有一行,从输入一字符串中取出连续的数字作为一个正整数,输出所有这些正整数之和(1位整数≤和≤100位整数)。
3. FBI树(本题60分)(fbi.cpp/c)【问题描述】我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串。
FBI树是一种二叉树,它的结点类型也包括F结点,B结点和I结点三种。
ACM程序设计竞赛例题[1]
A C M程序设计竞赛例题[1]-CAL-FENGHAI.-(YICAI)-Company One1备战ACM资料习题1.0-1背包问题在0 / 1背包问题中,需对容量为c 的背包进行装载。
从n 个物品中选取装入背包的物品,每件物品i 的重量为wi ,价值为pi 。
对于可行的背包装载,背包中物品的总重量不能超过背包的容量,最佳装载是指所装入的物品价值最高。
程序如下:#include <>void readdata();void search(int);void checkmax();void printresult();int c=35, n=10; ");printf("\n");}printf("\n");}6.素数环问题把从1到20这20个数摆成一个环,要求相邻的两个数的和是一个素数。
分析:用回溯算法,考察所有可能的排列。
程序如下:#include <>#include <>void search(int);void init(); 表示空格;’X’表示墙。
程序如下:#include <>#include <>void search(int,int);int canplace(int,int);void readdata(); Floodfill给一个20×20的迷宫和一个起点坐标,用广度优先搜索填充所有的可到达的格子。
提示:参考第2题。
2. 电子老鼠闯迷宫如下图12×12方格图,找出一条自入口(2,9)到出口(11,8)的最短路本题给出完整的程序和一组测试数据。
状态:老鼠所在的行、列。
程序如下:#include<>void readdata();a[i][j]=0; ....注:测试数据可在运行时粘贴上去(点击窗口最左上角按钮,在菜单中选则“编辑”/“粘贴”即可)。
acm考试题目及答案
acm考试题目及答案1. 题目:给定一个整数数组,找出数组中没有出现的最小的正整数。
答案:首先,我们可以遍历数组,将每个元素与它的索引对应起来,即如果数组中存在数字`i`,则将其与索引`i-1`对应。
然后,我们可以遍历数组,检查索引`i`是否与数组中第`i`个元素相等。
如果不相等,则索引`i`对应的值就是没有出现的最小正整数。
如果所有元素都与其索引对应,则没有出现的最小正整数为数组长度加1。
2. 题目:实现一个函数,检查一个链表是否为回文结构。
答案:我们可以将链表的前半部分反转,然后比较反转后的前半部分与后半部分是否相同。
如果相同,则链表是回文的;如果不相同,则不是。
具体步骤如下:首先找到链表的中点,然后反转前半部分链表,接着比较反转后的前半部分与后半部分是否相同,最后将前半部分链表再次反转回来。
3. 题目:给定一个只包含 '(' 和 ')' 的字符串,判断字符串是否有效。
答案:我们可以使用一个栈来解决这个问题。
遍历字符串中的每个字符,如果遇到'(',则将其压入栈中;如果遇到')',则检查栈是否为空,如果为空,则字符串无效;如果不为空,则弹出栈顶元素。
遍历结束后,如果栈为空,则字符串有效;如果栈不为空,则字符串无效。
4. 题目:找出一个无序数组中第k大的元素。
答案:我们可以使用快速选择算法来解决这个问题。
首先,选择一个元素作为基准,然后将数组分为两部分:一部分是大于基准的元素,另一部分是小于基准的元素。
根据基准的位置,我们可以确定第k大的元素是在基准的左边还是右边,然后递归地在相应的部分中寻找第k大的元素。
重复这个过程,直到找到第k大的元素。
5. 题目:给定一个字符串,找出其中不含有重复字符的最长子串的长度。
答案:我们可以使用滑动窗口的方法来解决这个问题。
维护一个窗口,记录窗口内字符的出现情况。
遍历字符串,如果遇到重复的字符,则移动窗口的左边界,直到窗口内没有重复的字符。
acm竞赛试题及答案
acm竞赛试题及答案ACM竞赛试题及答案1. 问题描述给定一个整数数组,找出数组中最长的连续子数组,使得该子数组的和至少为给定的整数S。
如果不存在这样的子数组,则返回-1。
2. 输入格式输入包含两行,第一行包含两个整数n和S(1≤n≤10^5,1≤S≤10^9),分别表示数组的长度和子数组的最小和。
第二行包含n 个整数,表示数组的元素。
3. 输出格式输出一个整数,表示最长的连续子数组的长度。
如果不存在满足条件的子数组,则输出-1。
4. 样例输入```5 151 2 3 7 5```5. 样例输出```3```6. 试题分析本题要求找出数组中和至少为S的最长连续子数组。
可以通过滑动窗口的方法来解决。
首先,初始化两个指针left和right,分别表示子数组的左右边界。
同时,维护一个变量sum来记录当前子数组的和。
遍历数组,当sum小于S时,将right指针向右移动,并将对应的元素值加到sum上;当sum大于等于S时,尝试将left指针向右移动,减小sum的值,同时更新最长子数组的长度。
最后,如果未找到满足条件的子数组,则返回-1。
7. 答案```pythondef longest_subarray(n, S, arr):left = 0sum = 0max_len = 0for right in range(n):sum += arr[right]while sum >= S:max_len = max(max_len, right - left + 1)sum -= arr[left]left += 1return max_len if max_len > 0 else -1# 样例输入n, S = map(int, input().split())arr = list(map(int, input().split()))# 样例输出print(longest_subarray(n, S, arr))```8. 注意事项在实现过程中,需要注意边界条件的处理,例如当整个数组的和都小于S时,应该返回-1。
ACM程序设计试题及参考答案
ACM程序设计试题及参考答案猪的安家Andy和Mary养了很多猪。
他们想要给猪安家。
但是Andy没有足够的猪圈,很多猪只能够在一个猪圈安家。
举个例子,假如有16头猪,Andy建了3个猪圈,为了保证公平,剩下1头猪就没有地方安家了。
Mary生气了,骂Andy没有脑子,并让他重新建立猪圈。
这回Andy建造了5个猪圈,但是仍然有1头猪没有地方去,然后Andy又建造了7个猪圈,但是还有2头没有地方去。
Andy都快疯了。
你对这个事情感兴趣起来,你想通过Andy建造猪圈的过程,知道Andy家至少养了多少头猪。
输入输入包含多组测试数据。
每组数据第一行包含一个整数n (n <= 10) – Andy 建立猪圈的次数,解下来n行,每行两个整数ai, bi( bi <= ai <= 1000), 表示Andy建立了ai个猪圈,有bi头猪没有去处。
你可以假定(ai, aj) = 1.输出输出包含一个正整数,即为Andy家至少养猪的数目。
样例输入33 15 17 2样例输出16答案:// 猪的安家.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "iostream.h"void main(){int n;int s[10][2];bool r[10];char ch;cout<<"请输入次数:"<<endl;cin>>n;for (int i=0;i<n;i++){cout<<"请输入第"<<i+1<<"次的猪圈个数和剩下的猪:,用--分开,"<<endl;cin>>s[i][0]>>ch>>ch>>s[i][1];}for (i=0;i<10;i++)r[i]=true;for (int sum=1;;sum++){for (i=0;i<n;i++)r[i]=(sum%s[i][0]==s[i][1]);for (i=0;i<n;i++){if (r[i]==0)break;}if (i==n)break;}cout<<"猪至少有"<<sum<<"只。
ACM程序大赛选拔初赛试题 - 参考答案
a[n]=n+1; int x=0; while(n>=1) {
if(a[i]!=0) {
if(x%2==0) { if(n==2) printf("\n"); printf("%d ",a[i]); n--; a[i]=0; } x++; } if(i+1>=size) i=0; else i++; } }
4、解方程 难度系数:★★☆☆☆
(注意此题只供 10 级的同学,08,09 级的同学请做第三题) 题意:形如 ax2+bx+c=d 的方程。其中 a,c,d 均为整数,b 为正整数。请你用计算机求出对 应的 x 的值(只考虑实数根)。
输入:依次输入 a,b,c,d 的值。注意:只要输入一组数据。用空格隔开。
char temp,str[10]; scanf("%s",str);
for(int i=0;i<9;i++) for(int j=i+1;j<10;j++) if(str[i]>str[j]) { temp=str[i]; str[i]=str[j]; str[j]=temp; }
cout<<str<<endl; }
输入:依次输入函数的四个系数中的 a,b,c,d 的值。用空格隔开。
输出:如果有极值点则全部输出,结果保留两位小数,格式为“(x1=#,y1=#) (x2=#,y2=#)”,如果没有则输入“no answer。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
备战ACM资料一:知识点数据结构:1,单,双链表及循环链表2,树的表示与存储,二叉树(概念,遍历)二叉树的应用(二叉排序树,判定树,博弈树,解答树等)3,文件操作(从文本文件中读入数据并输出到文本文件中)4,图(基本概念,存储结构,图的运算)数学知识1,离散数学知识的应用(如排列组合、简单的图论,数理逻辑)2,数论知识3,线性代数4,组合代数5,计算几何二算法1,排序算法(冒抛法,插入排序,合并排序,快速排序,堆排序)2,查找(顺序查找,二分发)3,回溯算法4,递归算法5,分治算法6,模拟法7,贪心法8,简单搜索算法(深度优先,广度优先),搜索中的剪枝,A*算法9,动态规划的思想及基本算法10,高精度运算三、ACM竞赛的题型分析竞赛的程序设计一般只有16种类型,它们分别是:Dynamic Programming (动态规划)Greedy (贪心算法)Complete Search (穷举搜索)Flood Fill (不知该如何翻译)Shortest Path (最短路径)Recursive Search Techniques (回溯搜索技术)Minimum Spanning Tree (最小生成树)Knapsack (背包问题)Computational Geometry (计算几何学)Network Flow (网络流)Eulerian Path (欧拉回路)Two-Dimensional Convex Hull (不知如何翻译)BigNums (大数问题)Heuristic Search (启发式搜索)Approximate Search (近似搜索)Ad Hoc Problems (杂题)四ACM竞赛参考书《实用算法的分析与程序设计》(吴文虎,王建德著,电子工业出版社,竞赛类的黑宝书)《青少年国际和全国信息学(计算机)奥林匹克竞赛指导)――组合数学的算法和程序设计》(吴文虎,王建德著,清华大学出版社,参加竞赛组合数学必学)《计算机算法设计与分析》(王晓东编著,最好的数据结构教材)《数据结构与算法》(傅清祥,王晓东编著,我所见过的最好的算法教材)《信息学奥林匹克竞赛指导――1997-1998竞赛试题解析》(吴文虎,王建德著,清华大学出版社)《计算机程序设计技巧》 D.E.Kruth著,算法书中最著名的《葵花宝典》,大师的作品,难度大)《计算几何》周陪德著《ACM国际大学生程序设计竞赛试题与解析(一)》(吴文虎著,清华大学出版社)《数学建模竞赛培训教材》共三本叶其孝主编《数学模型》第二版姜启源《随机规划》《模糊数学》《数学建模入门》徐全智《计算机算法设计与分析》国防科大五常见的几个网上题库常用网站:1)信息学初学者之家:/(2)大榕树编程世界:/~drs/program/default.asp(3)中国教育曙光网:/aosai/(4)福建信息学奥林匹克:/fjas/index.htm(5)第20届全国青少年信息学奥林匹克竞赛:/(6)第15届国际青少年信息学奥林匹克竞赛:/(7)全美计算机奥林匹克竞赛:/usacogate(8)美国信息学奥林匹克竞赛官方网站:/(9)俄罗斯Ural州立大学:http://acm.timus.ru/(10)西班牙Valladolid大学:http://acm.uva.es/problemset(11)ACM-ICPC:/icpc/(12)北京大学:/JudgeOnline/index.acm(13)浙江大学:/(14)IOI:http://olympiads.win.tue.nl/ioi/(15)2003年江苏省信息学奥林匹克竞赛夏令营:(16)(17)(18)(19)/downldmanag/index.asp(20) colin_fox/colin_fox五如何备战ACM/ICPC1,个人准备(算法书,习题集,网上做题和讨论)2,1000题=亚洲冠军=世界决赛3,做好资料收集和整理工作实验一:递归与分治1.二分查找2.合并排序3.快速排序实验二:回溯1.0-1背包问题2.装载问题3.堡垒问题(ZOJ1002)4.*翻硬币问题5.8皇后问题6.素数环问题7.迷宫问题8.*农场灌溉问题(ZOJ2412)9.*求图像的周长(ZOJ1047)10.*骨牌矩阵11.*字母转换(ZOJ1003)12.*踩气球(ZOJ1004)实验三:搜索1.Floodfill2.电子老鼠闯迷宫3.跳马4.独轮车5.皇宫小偷6.分酒问题7.*找倍数8.*8数码难题实验四:动态规划1.最长公共子序列2.计算矩阵连乘积3.凸多边形的最优三角剖分4.防卫导弹5.*石子合并6.*最小代价子母树7.*旅游预算8.*皇宫看守9.*游戏室问题10.*基因问题11.*田忌赛马实验五:贪心与随机算法1.背包问题2.搬桌子问题3.*照亮的山景4.*用随即算法求解8皇后问题5.素数测试实验一:递归与分治实验目的理解递归算法的思想和递归程序的执行过程,并能熟练编写递归程序。
掌握分治算法的思想,对给定的问题能设计出分治算法予以解决。
实验预习内容编程实现讲过的例题:二分搜索、合并排序、快速排序。
对本实验中的问题,设计出算法并编程实现。
试验内容和步骤1.二分查找在对线性表的操作中,经常需要查找某一个元素在线性表中的位置。
此问题的输入是待查元素x和线性表L,输出为x在L中的位置或者x不在L中的信息。
程序略2.合并排序程序略3.快速排序程序略实验总结及思考合并排序的递归程序执行的过程实验二:回溯算法实验目的:熟练掌握回溯算法实验内容:回溯算法的几种形式a)用回溯算法搜索子集树的一般模式void search(int m){if(m>n) //递归结束条件output(); //相应的处理(输出结果)else{a[m]=0; //设置状态:0表示不要该物品search(m+1); //递归搜索:继续确定下一个物品a[m]=1; //设置状态:1表示要该物品search(m+1); //递归搜索:继续确定下一个物品}}b)用回溯算法搜索子集树的一般模式void search(int m){if(m>n) //递归结束条件output(); //相应的处理(输出结果)elsefor(i=m;i<=n;i++){swap(m,i); //交换a[m]和a[i]if()if(canplace(m)) //如果m处可放置search(m+1); //搜索下一层swpa(m,i); //交换a[m]和a[i](换回来)}}习题1.0-1背包问题在0 / 1背包问题中,需对容量为c 的背包进行装载。
从n 个物品中选取装入背包的物品,每件物品i 的重量为wi ,价值为pi 。
对于可行的背包装载,背包中物品的总重量不能超过背包的容量,最佳装载是指所装入的物品价值最高。
程序如下:#include <stdio.h>void readdata();void search(int);void checkmax();void printresult();int c=35, n=10; //c:背包容量;n:物品数int w[10], v[10]; //w[i]、v[i]:第i件物品的重量和价值int a[10], max; //a数组存放当前解各物品选取情况;max:记录最大价值//a[i]=0表示不选第i件物品,a[i]=1表示选第i件物品int main(){readdata(); //读入数据search(0); //递归搜索printresult();}void search(int m){if(m>=n)checkmax(); //检查当前解是否是可行解,若是则把它的价值与max比较else{a[m]=0; //不选第m件物品search(m+1); //递归搜索下一件物品a[m]=1; //不选第m件物品search(m+1); //递归搜索下一件物品}}void checkmax(){int i, weight=0, value=0;for(i=0;i<n;i++){if(a[i]==1) //如果选取了该物品{weight = weight + w[i]; //累加重量value = value + v[i]; //累加价值}}if(weight<=c) //若为可行解if(value>max) //且价值大于maxmax=value; //替换max}void readdata(){int i;for(i=0;i<n;i++)scanf("%d%d",&w[i],&v[i]); //读入第i件物品重量和价值}void printresult(){printf("%d",max);}2.装载问题有两艘船,载重量分别是c1、c2,n个集装箱,重量是wi (i=1…n),且所有集装箱的总重量不超过c1+c2。
确定是否有可能将所有集装箱全部装入两艘船。
提示:求出不超过c1的最大值max,若总重量-max < c2则能装入到两艘船。
3.堡垒问题(ZOJ1002)如图城堡是一个4×4的方格,为了保卫城堡,现需要在某些格子里修建一些堡垒。
城堡中的某些格子是墙,其余格子都是空格,堡垒只能建在空格里,每个堡垒都可以向上下左右四个方向射击,如果两个堡垒在同一行或同一列,且中间没有墙相隔,则两个堡垒都会把对方打掉。
问对于给定的一种状态,最多能够修建几个堡垒。
程序主要部分如下:int main(){readdata(); //读入数据search(0); //递归搜索printresult();}void search(int m){int row, col;row=m/n; //求第m个格子的行号col=m%n; //求第m个格子的列号if(m>=n*n)checkmax(); //检查当前解是否是可行解,若是则把它的价值与max比较else{search(m+1); //该位置不放堡垒递归搜索下一个位置if(canplace(m)) //判断第m个格子是否能放堡垒{place(m); //在第m个格子上放置一个堡垒search(m+1); //递归搜索下一个位置takeout(m); //去掉第m个格子上放置的堡垒}}}4.翻硬币问题把硬币摆放成32×9的矩阵,你可以随意翻转矩阵中的某些行和某些列,问正面朝上的硬币最多有多少枚?提示:(1)任意一行或一列,翻两次等于没有翻;(2)对于9列的任何一种翻转的情况,每一行翻与不翻相互独立。
5.8皇后问题在一个8×8的棋盘里放置8个皇后,要求这8个皇后两两之间互相都不“冲突”。