2015程序设计实习之第二次上机周六

A:放苹果(POJ1664


总时间限制: 
1000ms 
内存限制: 
65536kB
描述
把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。
输入
第一行是测试数据的数目t(0 <= t <= 20)。以下每行均包含二个整数M和N,以空格分开。1<=M,N<=10。
输出
对输入的每组数据M和N,用一行输出相应的K。
样例输入
1
7 3
样例输出
8
来源
lwx@POJ


#include<iostream>
using namespace std;
int  f(int m,int n)//m个苹果,n个盘子
{
	if(n==1||m==0) return 1; 
	if(n>m)return f(m,m);
	return f(m,n-1)+f(m-n,n);
}

int main()
{
	int num;
	cin>>num;
	while(num--)
	{
		int m,n;
		cin>>m>>n;
		cout<<f(m,n)<<endl;
	}

	return 0;
}


B:古代密码(POJ2820)

总时间限制: 
1000ms 
内存限制: 
65536kB
描述
古罗马帝王有一个包括各种部门的强大政府组织。其中有一个部门就是保密服务部门。为了保险起见,在省与省之间传递的重要文件中的大写字母是加密的。当时最流行的加密方法是替换和重新排列。 
替换方法是将所有出现的字符替换成其它的字符。有些字符会碰巧替换成它自己。例如:替换规则可以是将'A' 到 'Y'替换成它的下一个字符,将'Z'替换成 'A',如果原词是 "VICTORIOUS" 则它变成 "WJDUPSJPVT"。
排列方法改变原来单词中字母的顺序。例如:将顺序 <2, 1, 5, 4, 3, 7, 6, 10, 9, 8> 应用到 "VICTORIOUS" 上,则得到"IVOTCIRSUO"。 
人们很快意识到单独应用替换方法或排列方法,加密是很不保险的。但是如果结合这两种方法,在当时就可以得到非常可靠的加密方法。所以,很多重要信息先使用替换方法加密,再将加密的结果用排列的方法加密。用两中方法结合就可以将"VICTORIOUS" 加密成"JWPUDJSTVP"。 
考古学家最近在一个石台上发现了一些信息。初看起来它们毫无意义,所以有人设想它们可能是用替换和排列的方法被加密了。人们试着解读了石台上的密码,现在他们想检查解读的是否正确。他们需要一个计算机程序来验证她,你的任务就是写这个验证程序。




输入
输入有两行。第一行是石台上的文字。文字中没有空格,并且只有大写英文字母。第二行是被解读出来的加密前的文字。第二行也是由大写英文字母构成的。
两行字符数目的长度都不超过计划100。
输出
如果第二行经过某种加密方法后可以产生第一行的信息,输出 "YES",否则输出"NO"。
样例输入
JWPUDJSTVP
VICTORIOUS
样例输出
YES
来源
2159

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int be[26]={0};
int af[26]={0};
int main()
{
	string origin;
	string  after;
	cin>>after>>origin;
	if(after.length()!=origin.length())
		{cout<<"NO"<<endl;
	return 0;
	}
	int len=after.length();
	for(int i=0;i<len;++i)
	{
		af[after[i]-'A']++;
		be[origin[i]-'A']++;
	}
	sort(af,af+26);
		sort(be,be+26);
		for(int i=0;i<26;++i)
		{
			if(af[i]!=be[i])
				{cout<<"NO"<<endl;
			return 0;
			}
		}

	  cout<<"YES"<<endl;
	   return 0;
}

C:棋盘问题(POJ1321)

总时间限制: 
1000ms 
内存限制: 
65536kB
描述
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。
输入
输入含有多组测试数据。
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。
输出
对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。
样例输入
2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1
样例输出
2
1
来源
蔡错@pku
#include<iostream>
#include<memory.h>
using namespace std;
int map[8][8],b[8];
int sol=0;
int side,num;
void dfs(int row,int n)
{
    if(n==0)
    {
        sol++;
        return;
    }
    if(row<0)return ;
    if(n>row+1)
        return;
    for(int i=0;i<side;++i)
        if(map[row][i]==1&&b[i]==0)
        {
            b[i]=1;
            dfs(row-1,n-1);
            b[i]=0;
        }
    dfs(row-1,n);
    return ;
}
int main()
{
    char ch;
    
    while(cin>>side>>num)
    {
        sol=0;
        if(side==-1&&num==-1)
            break;
        memset(map,0,sizeof(map));
        memset(b,0,sizeof(b));
        for(int i=0;i<side;++i)
            for(int j=0;j<side;++j)
            {
                cin>>ch;
                if(ch=='#')
          
                    map[i][j]=1;
            }
        dfs(side-1,num);
        cout<<sol<<endl;
    }
    return 0;
}

D:求平均年龄(POJ2714)

总时间限制: 
1000ms 
内存限制: 
65536kB
描述

班上有学生若干名,给出每名学生的年龄(整数),求班上所有学生的平均年龄,保留到小数点后两位。

输入
第一行有一个整数n(1<= n <= 100),表示学生的人数。其后n行每行有1个整数,表示每个学生的年龄,取值为15到25。
输出
输出一行,该行包含一个浮点数,为要求的平均年龄,保留到小数点后两位。
样例输入
2
18
17
样例输出
17.50
提示
要输出浮点数、双精度数小数点后2位数字,可以用下面这种形式: 

printf("%.2f", num);
来源
2005~2006医学部计算概论期末考试

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	int sum=0,count;
	int year;
	cin>>count;
	int cnt=count;
	while(count--)
	{
		cin>>year;
		sum+=year;
	}
	double average=(double)sum/(double)cnt;
	printf("%.2f", average);
	return 0;
}

E:拦截导弹(POJ2945)

总时间限制: 
1000ms 
内存限制: 
65536kB
描述
某国为了防御敌国的导弹袭击,开发出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭,并观测到导弹依次飞来的高度,请计算这套系统最多能拦截多少导弹。拦截来袭导弹时,必须按来袭导弹袭击的时间顺序,不允许先拦截后面的导弹,再拦截前面的导弹。
输入
输入有两行,
第一行,输入雷达捕捉到的敌国导弹的数量k(k<=25),
第二行,输入k个正整数,表示k枚导弹的高度,按来袭导弹的袭击时间顺序给出,以空格分隔。
输出
输出只有一行,包含一个整数,表示最多能拦截多少枚导弹。
样例输入
8
300 207 155 300 299 170 158 65
样例输出
6
来源
医学部计算概论2006期末考试题
#include<iostream>
using namespace std;
int num;	int height[25];
int table[25]={0};
int f(int n,int h)
{    
	int count1=0,count2=0; 
      if(n==num-1)
		  if(height[n]<=h){		   
			  return table[n];
		  }
		  else return 0;
		  bool small=0;
	  if(height[n]<=h)
		 {   
		  if(table[n]!=0)
		    count1=table[n];
		  else
		  {  
			  count1++;
			  count1+=f(n+1,height[n]);
		  table[n]=count1;
	         }
		  count2+=f(n+1,h);
		  small=1;
	  }
	  if(small==0)
		  count2=f(n+1,h);
	  int count ;
	  if(count1<count2)count=count2;
	  else count=count1;
	  return count;
}
int main()
{
	cin>>num;
	table[num-1]=1;
	for(int i=0;i<num;++i)
		cin>>height[i];
	int  count=f(0,10000);
	cout<<count<<endl;
	return 0;
}


F:Flip Game(POJ1753)

总时间限制: 
1000ms 
内存限制: 
65536kB
描述
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:
  1. Choose any one of the 16 pieces. 
  2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).

Consider the following position as an example:

bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:

bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
输入
The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.
输出
Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).
样例输入
bwwb
bbwb
bwwb
bwww
样例输出
4
来源
Northeastern Europe 2000
#include<iostream>
#include<memory.h>
using namespace std;
int table[6][6];
int press[6][6];
int sol[2]={1000000,100000};
int tempsol=0;
bool is[2]={0};
bool guess(int  i)
{
    for(int k=1;k<=4;++k)
        if(press[1][k]==1)
            tempsol++;
    int r,c;
    for(r=1;r<=3;++r)
        for(c=1;c<=4;++c)
           if((press[r][c]+press[r-1][c]+press[r][c+1]+press[r][c-1]+table[r][c])%2!=i)
           {
               press[r+1][c]=1;
               tempsol++;
           }else
               press[r+1][c]=0;
    for(int k=1;k<=4;++k)
        if((press[4][k]+press[3][k]+press[4][k-1]+press[4][k+1]+table[4][k])%2!=i)
            return false;
    return true;
}

G:最大子矩阵(POJ2766)

总时间限制: 
1000ms 
内存限制: 
65536kB
描述
已知矩阵的大小定义为矩阵中所有元素的和。给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵。

比如,如下4 * 4的矩阵

0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2

的最大子矩阵是

9 2
-4 1
-1 8

这个子矩阵的大小是15。
输入
输入是一个N * N的矩阵。输入的第一行给出N (0 < N <= 100)。再后面的若干行中,依次(首先从左到右给出第一行的N个整数,再从左到右给出第二行的N个整数……)给出矩阵中的N 2个整数,整数之间由空白字符分隔(空格或者空行)。已知矩阵中整数的范围都在[-127, 127]。
输出
输出最大子矩阵的大小。
样例输入
4
0 -2 -7 0 9 2 -6 2
-4 1 -4  1 -1

8  0 -2
样例输出
15
来源
翻译自 Greater New York 2001 的试题

#include<iostream>
#include<stdio.h>
#include <memory.h>
using namespace std;
int table[101][101],b[101],maxnum;
int n;
void getmax(int a[])
{
    int sum=0;
    for(int i=0;i<n;++i)
    {
        sum+=a[i];
        if(sum<=0)sum=0;
        if(sum>maxnum)  maxnum=sum;
    }
    return ;
}
int main()
{
      scanf("%d",&n);
    for(int i=0;i<n;++i)
        for(int j=0;j<n;++j)
            scanf("%d",&table[i][j]);
    for(int i=0;i<n;++i)
        for(int j=i;j<n;++j)
        {
            memset(b,0,sizeof(b));
            for(int k=0;k<n;++k)
                for(int m=i;m<=j;++m)
                    b[k]+=table[k][m];
            getmax(b);
        }
    cout<<maxnum<<endl;
    return 0;
}

H:Tour(暂时还不会做,代码也没看懂,先贴上代码。》---《)

总时间限制: 
1000ms 
内存限制: 
65536kB
描述

John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must determine the shortest closed tour that connects his destinations. Each destination is represented by a point in the plane pi = < xi,yi >. John uses the following strategy: he starts from the leftmost point, then he goes strictly left to right to the rightmost point, and then he goes strictly right back to the starting point. It is known that the points have distinct x-coordinates.

Write a program that, given a set of n points in the plane, computes the shortest closed tour that connects the points according to John's strategy.

输入
The program input is from a text file. Each data set in the file stands for a particular set of points. For each set of points the data set contains the number of points, and the point coordinates in ascending order of the x coordinate. White spaces can occur freely in input. The input data are correct.

The number of points of each data set is at most 50, and each coordinate does not exceed 20000 by an absolute value.
输出
For each set of data, your program should print the result to the standard output from the beginning of a line. The tour length, a floating-point number with two fractional digits, represents the result. An input/output sample is in the table below. Here there are two data sets. The first one contains 3 points specified by their x and y coordinates. The second point, for example, has the x coordinate 2, and the y coordinate 3. The result for each data set is the tour length, (6.47 for the first data set in the given example).
样例输入
3
1 1
2 3
3 1
4
1 1
2 3
3 1
4 2
样例输出
6.47
7.89
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
using namespace std;

const int INF = 1<<29;
const int MAXN = 1100;
const double PI = acos(-1.0);
const double e = 2.718281828459;
const double eps = 1e-8;
struct node
{
    double x;
    double y;
}a[MAXN];
double dp[MAXN][MAXN];

int cmp(node a, node b)
{
    return a.x < b.x;
}

double dist(int i, int j)
{
    return sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int n;
    while(cin>>n)
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%lf %lf", &a[i].x, &a[i].y);
        }
        sort(a+1, a+1+n, cmp);
        dp[2][1] = dist(1, 2);
        for(int i = 3; i <= n; i++)
        {
            dp[i][i-1] = INF*1.0;
            for(int j = 1; j < i-1; j++)
            {
                dp[i][i-1] = min(dp[i][i-1], dp[i-1][j]+dist(i, j));
                dp[i][j] = dp[i-1][j]+dist(i-1, i);
            }
        }
        double ans = INF*1.0;
        for(int i = 1; i < n; i++)
        {
            ans = min(ans, dp[n][i]+dist(n, i));
        }
        printf("%.2f\n", ans);
    }
    return 0;
}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值