公共因子
Time Limit: 1000MS Memory limit: 65536K
题目描述
输入
输出
示例输入
acac ac aaa aa
示例输出
1 1
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
char s1[100007],s2[100007];
char ss[100007];
char x1[100007],x2[100007];
int gcd(int x,int y)
{
if(y==0)return x;
return gcd(y,x%y);
}
int main()
{
while(cin>>s1>>s2)
{
int len1=strlen(s1);
int len2=strlen(s2);
int len=gcd(len1,len2);//欧几里得求最大公约数
int flag,num=0,cnt,i,j,s,k;
for(i=1;i<=len;i++)
{
flag=0;
if(len%i==0)//如果是两个串的因数
{
for(j=0;j<i;j++)
ss[j]=s1[j];//将s1的前i部分存下来
for(j=i;j<=len1-i;j+=i)//判断分成的n部分是否都相同
{
cnt=0;
for(s=j;s<j+i;s++)
x1[cnt++]=s1[s];
for(k=0;k<i;k++)
if(ss[k]!=x1[k])//如果不相同,就结束
{
flag=1;
break;
}
if(flag)break;
}
if(flag)continue;
for(j=0;j<=len2-i;j+=i)//判断分成的m部分是否都相同
{
cnt=0;
for(s=j;s<j+i;s++)
x2[cnt++]=s2[s];
for(k=0;k<i;k++)
if(ss[k]!=x2[k])//如果不相同,就结束
{
flag=1;
break;
}
if(flag)break;
}
if(flag)continue;
num++;//如果全部相同,则公共因子加1
}
}
printf("%d\n",num);
}
return 0;
}
下沉的船
Time Limit: 1000MS Memory limit: 65536K
题目描述
身份优先级: woman = child > man > captain.
多组数据(少于组),每一组有一个整数(,代表船上有个人,接下来行,每行有一个名字和他的身份。名字字符长度小于。数据保证不存在重名,注意可能存在多名船长
输出
示例输入
6 Jack captain Alice woman Charlie man Teddy woman Bob child Julia woman
示例输出
Alice Teddy Bob Julia Charlie Jack
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
char s[10];
struct sa
{
int clas,num;
char name[10];
}chuan[107];
int cmp(const void*_a,const void*_b)
{
sa*a=(sa*)_a;
sa*b=(sa*)_b;
if(a->clas>b->clas)
return 1;
else if(a->clas==b->clas)//如果级别相同
{
return a->num-b->num;//则返回先输入的
}
return -1;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%s %s",chuan[i].name,s);
if(strcmp(s,"captain")==0)
chuan[i].clas=3;
else if(strcmp(s,"man")==0)
chuan[i].clas=2;
else chuan[i].clas=1;
chuan[i].num=i;
}
qsort(chuan,n,sizeof(chuan[0]),cmp);
for(int i=0;i<n;i++)
{
printf("%s\n",chuan[i].name);
}
}
return 0;
}
选夫婿1
Time Limit: 1000MS Memory limit: 32768K
题目描述
潘小姐不爱名利,只看人,第一关就是身高和体重要合格,即必须在其要求的范围内,否则直接排除在外,不允许参加下一轮的选拔。
作为一个程序员,你没有钱也没有权,擅长的也就是编程了。潘小姐也发现了这一点,所以把首轮根据身高体重进行选拔的任务交给了你,如果完成的好,你可以直接进入下一轮选拔,你笑了。
输入
潘小姐给你了所有报名男生的信息。输入数据的第一行是一个正整数N(0 < N < 1000)。然后N行数据,每行包含三部分,用空格隔开。第一部分是报名者的姓名name(长度小于20的字符串),然后是整数身高h(0 < h < 300),第三部分是整数体重w (0 < w < 200)。
最后一行是四个整数a,b,c,d.表示身高的合格范围是[a,b],体重的合格范围是[c,d](0 < a < b < 200, 0 < c < d < 300)。
输出
你需要把合格的男生信息按照身高从低到高输出,格式跟输入一样,也是每行三个信息,共N行,如果身高相同则按体重从轻到重输出,若没有合格人选则输出No,具体格式见样例。
示例输入
8武大郎 70 40西门庆 180 70李逵 160 150燕青 175 69鲁智深 180 100武松 180 75小泉纯一狼 30 20孙二娘 169 60165 190 60 90
示例输出
孙二娘 169 60燕青 175 69西门庆 180 70武松 180 75
#include<stdio.h>
#include<stdlib.h>
struct sa
{
char s[77];
int h,w;
}data[1000];
int cmp(const void*_a,const void*_b)
{
sa*a=(sa*)_a;
sa*b=(sa*)_b;
if(a->h>b->h)
return 1;
else if(a->h==b->h)
{
return a->w-b->w;
}
return -1;
}
int main()
{
int m;
scanf("%d",&m);
for(int a=1;a<=m;a++)
{
scanf("%s %d %d",&data[a].s,&data[a].h,&data[a].w);
}
int h1,h2,w1,w2,flag=0;
scanf("%d%d%d%d",&h1,&h2,&w1,&w2);
qsort(data+1,m,sizeof(data[1]),cmp);
for(int i=1;i<=m;i++)
{
if(data[i].h>=h1&&data[i].h<=h2&&data[i].w>=w1&&data[i].w<=w2)
{
printf("%s %d %d\n",data[i].s,data[i].h,data[i].w);
flag=1;
}
}
if(!flag)printf("No\n");
return 0;
}
图案打印
Time Limit: 1000MS Memory limit: 65536K
题目描述
输入
输出
示例输入
3
示例输出
* * * * * * * *
#include<stdio.h>
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=1; i<=n; i++)
{
if(i==1)
{
for(int j=1; j<=2*(n-1); j++)
printf(" ");
printf("*\n");
}
else
{
for(int j=1; j<=2*(n-i); j++)
printf(" ");
printf("*");
for(int j=1; j<=2*(i-1); j++)
printf(" ");
for(int j=1; j<2*(i-1); j++)
printf(" ");
printf("*\n");
}
}
for(int i=n-1; i>=1; i--)
{
if(i==1)
{
for(int j=1; j<=2*(n-1); j++)
printf(" ");
printf("*\n");
}
else
{
for(int j=1; j<=2*(n-i); j++)
printf(" ");
printf("*");
for(int j=1; j<=2*(i-1); j++)
printf(" ");
for(int j=1; j<2*(i-1); j++)
printf(" ");
printf("*\n");
}
}
}
return 0;
}
487-3279
Time Limit: 2000MS Memory limit: 65536K
题目描述
The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows:
A, B, and C map to 2
D, E, and F map to 3
G, H, and I map to 4
J, K, and L map to 5
M, N, and O map to 6
P, R, and S map to 7
T, U, and V map to 8
W, X, and Y map to 9
There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010.
Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.)
Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number.
输入
输出
No duplicates.
示例输入
12 4873279 ITS-EASY 888-4567 3-10-10-10 888-GLOP TUT-GLOP 967-11-11 310-GINO F101010 888-1200 -4-8-7-3-2-7-9- 487-3279
示例输出
310-1010 2 487-3279 4 888-4567 3
#include<stdio.h>
#include<map>
#include<string.h>
#include<string>
#include<iostream>
using namespace std;
char s[1007],ss[1007];
string z;
int main()
{
int n;
scanf("%d",&n);
map<string,int>mp;
for(int i=0;i<n;i++)
{
scanf("%s",s);
int len=strlen(s),a=0;
memset(ss,0,sizeof(ss));
for(int j=0;j<len;j++)
{
if(s[j]=='A'||s[j]=='B'||s[j]=='C')
ss[a++]='2';
else if(s[j]=='D'||s[j]=='E'||s[j]=='F')
ss[a++]='3';
else if(s[j]=='G'||s[j]=='H'||s[j]=='I')
ss[a++]='4';
else if(s[j]=='J'||s[j]=='K'||s[j]=='L')
ss[a++]='5';
else if(s[j]=='M'||s[j]=='N'||s[j]=='O')
ss[a++]='6';
else if(s[j]=='P'||s[j]=='R'||s[j]=='S')
ss[a++]='7';
else if(s[j]=='T'||s[j]=='U'||s[j]=='V')
ss[a++]='8';
else if(s[j]=='W'||s[j]=='X'||s[j]=='Y')
ss[a++]='9';
else if(s[j]>='0'&&s[j]<='9')
ss[a++]=s[j];
if(a==3)
ss[a++]='-';
//printf("%c",ss[a]);
}
//printf("\n");
z=ss;
mp[z]++;
}
int max=1,flag=0;
map<string,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++)
{
if(max<it->second)
{
flag=1;
cout<<it->first<<" "<<it->second<<endl;
}
}
if(!flag)
printf("No duplicates.\n");
return 0;
}
The area
Time Limit: 1000MS Memory limit: 32768K
题目描述
Note: The point P1 in the picture is the vertex of the parabola.
输入
Each test case contains three intersectant points which shows in the picture, they are given in the order of P1, P2, P3. Each point is described by two floating-point numbers X and Y(0.0<=X,Y<=1000.0).
输出
示例输入
2 5.000000 5.000000 0.000000 0.000000 10.000000 0.000000 10.000000 10.000000 1.000000 1.000000 14.000000 8.222222
示例输出
33.33 40.69
提示
来源
#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
int main()
{
int t;
double x1,y1,x2,y2,x3,y3;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf",&x2,&y2);
scanf("%lf%lf",&x1,&y1);
scanf("%lf%lf",&x3,&y3);
double a=(y3-y2)/((x3-x2)*(x3-x2));
double h=x2;
double p=y2;
double k=(y3-y1)/(x3-x1);
double b=y3-k*x3;
double A=a;
// printf("%lf\n",A);
double B=-2*h*a-k;
//printf("%lf\n",B);
double C=p-b+a*h*h;
//printf("%lf\n",C);
double s=1./3*A*(x3*x3*x3-x1*x1*x1)+1./2*B*(x3*x3-x1*x1)+C*(x3-x1);
printf("%.2f\n",s);
}
return 0;
}
GCC
Time Limit: 1000MS Memory limit: 65536K
题目描述
The GNU Compiler Collection (usually shortened to GCC) is a compiler system produced by the GNU Project supporting various programming languages. But it doesn’t contains the math operator “!”.
In mathematics the symbol represents the factorial operation. The expression n! means "the product of the integers from 1 to n". For example, 4! (read four factorial) is 4 × 3 × 2 × 1 = 24. (0! is defined as 1, which is a neutral element in multiplication, not multiplied by anything.)
We want you to help us with this formation: (0! + 1! + 2! + 3! + 4! + ... + n!)%m.
输入
The first line consists of an integer T, indicating the number of test cases.
Each test on a single consists of two integer n and m.
0 <= n < 10 100 (without leading zero)
0 < m < 1000000
输出
Output the answer of (0! + 1! + 2! + 3! + 4! + ... + n!)%m.
示例输入
1 10 861017
示例输出
593846
提示
#include<stdio.h>
#include<string.h>
char s[107];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long m,ans=1,count=1;
scanf("%s%lld",&s,&m);
int len=strlen(s);
long long number=0;
if(len>7)
{
number=m-1;
}
else
{
for(int i=0; i<len; i++)
{
number=number*10+(s[i]-'0');
}
}
if(number>=m)
number=m-1;
for(int i=1; i<=number; i++)
{
count=(i*count)%m;
ans=(ans+count)%m;
}
printf("%lld\n",ans%m);
}
return 0;
}
图形密码
Time Limit: 1000MS Memory limit: 65536K
题目描述
输入
输出
示例输入
3 5 1 1 2 1 3 1 3 2 3 3 8 1 1 1 2 1 3 2 3 3 3 3 2 3 1 2 1 7 1 1 1 2 1 3 2 2 3 1 3 2 3 3
示例输出
counterclockwise clockwise clockwise (3,1) counterclockwise
提示
来源
#include<stdio.h>
int x[107],y[107];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,flag1=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d%d",&x[i],&y[i]);
for(int i=2;i<=n-1;i++)
{
int flag=0;
int x1=x[i+1]-x[i];
int y1=y[i+1]-y[i];
int x2=x[i-1]-x[i];
int y2=y[i-1]-y[i];
if(x1*y2-x2*y1<0)
flag=1;
else if(x1*y2-x2*y1>0)
flag=2;
if(flag1==0)
flag1=flag;
if(flag1&&flag&&flag1!=flag)
{
if(flag1==1)
printf("clockwise (%d,%d) ",x[i],y[i]);
else if(flag1==2)
printf("counterclockwise (%d,%d) ",x[i],y[i]);
flag1=flag;
}
}
if(flag1==1)
printf("clockwise\n");
else
printf("counterclockwise\n");
}
}
France \'98
Time Limit: 1000MS Memory limit: 65536K
题目描述
Today the first round of the Soccer World Championship in France is coming to an end. 16 countries are remaining now, among which the winner is determined by the following tournament:
1 Brazil -----+ +-- ? --+ 2 Chile ------+ | +-- ? --+ 3 Nigeria ----+ | | +-- ? --+ | 4 Denmark ----+ | +-- ? --+ 5 Holland ----+ | | +-- ? --+ | | 6 Yugoslavia -+ | | | +-- ? --+ | 7 Argentina --+ | | +-- ? --+ | 8 England ----+ | +-- World Champion 9 Italy ------+ | +-- ? --+ | 10 Norway -----+ | | +-- ? --+ | 11 France -----+ | | | +-- ? --+ | | 12 Paraguay ---+ | | +-- ? --+ 13 Germany ----+ | +-- ? --+ | 14 Mexico -----+ | | +-- ? --+ 15 Romania ----+ | +-- ? --+ 16 Croatia ----+For each possible match A vs. B between these 16 nations, you are given the probability that team A wins against B. This (together with the tournament mode displayed above) is sufficient to compute the probability that a given nation wins the World Cup. For example, if Germany wins against Mexico with 80%, Romania against Croatia with 60%, Germany against Romania with 70% and Germany against Croatia with 90%, then the probability that Germany reaches the semi-finals is 80% * (70% * 60% + 90% * 40%) = 62.4%.
Your task is to write a program that computes the chances of the 16 nations to become the World Champion \'98.
输入
The first 16 lines of the input file give the names of the 16 countries, from top to bottom according to the picture given above.
Next, there will follow a 16 x 16 integer matrix P where element pijgives the probability in percent that country #i defeats country #j in a direct match. Country #i means the i-th country from top to bottom given in the list of countries. In the picture above Brazil is #1 and Germany is #13, so p1,13=55 would mean that in a match between Brazil and Germany, Brazil wins with a probability of 55%.
Note that matches may not end with a draw, i.e. pij + pji = 100 for all i,j.
输出
示例输入
Brazil Chile Nigeria Denmark Holland Yugoslavia Argentina England Italy Norway France Paraguay Germany Mexico Romania Croatia 50 65 50 60 55 50 50 65 45 55 40 55 40 55 50 50 35 50 35 45 40 35 35 50 30 40 25 40 25 40 35 35 50 65 50 60 55 50 50 65 45 55 40 55 40 55 50 50 40 55 40 50 45 40 40 55 35 45 30 45 30 45 40 40 45 60 45 55 50 45 45 60 40 50 35 50 35 50 45 45 50 65 50 60 55 50 50 65 45 55 40 55 40 55 50 50 50 65 50 60 55 50 50 65 45 55 40 55 40 55 50 50 35 50 35 45 40 35 35 50 30 40 25 40 25 40 35 35 55 70 55 65 60 55 55 70 50 60 45 60 45 60 55 55 45 60 45 55 50 45 45 60 40 50 35 50 35 50 45 45 60 75 60 70 65 60 60 75 55 65 50 65 50 65 60 60 45 60 45 55 50 45 45 60 40 50 35 50 35 50 45 45 60 75 60 70 65 60 60 75 55 65 50 65 50 65 60 60 45 60 45 55 50 45 45 60 40 50 35 50 35 50 45 45 50 65 50 60 55 50 50 65 45 55 40 55 40 55 50 50 50 65 50 60 55 50 50 65 45 55 40 55 40 55 50 50
示例输出
Brazil p=8.54% Chile p=1.60% Nigeria p=8.06% Denmark p=2.79% Holland p=4.51% Yugoslavia p=7.50% Argentina p=8.38% England p=1.56% Italy p=9.05% Norway p=3.23% France p=13.72% Paraguay p=3.09% Germany p=13.79% Mexico p=3.11% Romania p=5.53% Croatia p=5.53%
提示
来源
题意:题目只有一个case,有16个球队,给出名字,并且下面一个16*16的矩阵,p[i][j]是一个二位的整数,表示i球队打败j球队的概率,p[i][j]+p[j][i]=100,下面16行输出球队名字并且后面一个百分率是该球队获得总冠军的概率
DP递推或者记忆化搜索都可以
设dp[i][left][right]表示i这个球队在第left到第right球队里面得到冠军的概率,所以最后需要的是dp[i][1][16]。一个球队要在当面的阶段得到冠军必须在之前得到冠军
所以mid=(left+right)/2;
i和j在[left,right]里面争夺冠军(例如i和j在1到16里面争夺冠军,也就是争夺总冠军),那么i和j必定是来自于两个分区的
left<=i<=mid , 那么需要得到dp[i][left][mid],dp[j][mid+1][right]
mid+1<=i<=right,那么需要得到dp[i][mid+1][right] , dp[j][left][mid]
#include<stdio.h>
#include<string.h>
double dp[17][17][17],s[17][22];
char n[17][77];
int main()
{
for(int i=1;i<=16;i++)
{
scanf("%s",n[i]);
//printf("%s\n",s[i]);
}
double ss;
for(int i=1;i<=16;i++)
for(int j=1;j<=16;j++)
{
scanf("%lf",&ss);
s[i][j]=ss/100;
//printf("%lf",s[i][j]);
}
memset(dp,0,sizeof(dp));
for(int i=1;i<=16;i++)
dp[i][i][i]=1;
int left,mid,right;
for(int i=2;i<=16;i*=2)
{
for(left=1;left<=16;left+=i)
{
right=left+i-1;
mid=(left+right)/2;
for(int j=left;j<=mid;j++)
for(int k=mid+1;k<=right;k++)
{
dp[j][left][right]+=s[j][k]*dp[j][left][mid]*dp[k][mid+1][right];
dp[k][left][right]+=s[k][j]*dp[j][left][mid]*dp[k][mid+1][right];
}
}
}
for(int i=1;i<=16;i++)
printf("%-10s p=%.2f%%\n",n[i],dp[i][1][16]*100);
return 0;
}
魔幻数字47
Time Limit: 1000MS Memory limit: 32768K
题目描述
输入
输出
示例输入
2 1 147 1 2
示例输出
47 147 NONE
提示
来源
#include<stdio.h>
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
while(n--)
{
int a,b;
scanf("%d%d",&a,&b);
int tmp;
if(a>b)
{
tmp=a;
a=b;
b=tmp;
}
int flag=0;
for(int i=a; i<=b; i++)
{
if(flag==0)
{
int j=i;
int c=j%10;
j/=10;
int d=j%10;
if(c==7&&d==4)
{
flag=1;
printf("%d\n",i);
i+=99;
}
}
else
{
printf("%d\n",i);
i+=99;
}
}
if(!flag)
printf("NONE\n");
}
}
return 0;
}