A - ASCII码排序
输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
Input
输入数据有多组,每组占一行,有三个字符组成,之间无空格。
Output
对于每组输入数据,输出一行,字符中间用一个空格分开。
Sample
Inputcopy | Outputcopy |
---|---|
qwe asd zxc | e q w a d s c x z |
总结:将值以字符串的形式输入,然后再用c++内置的sort()函数从小到大排序即可,特别注意一下输出的格式。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{ char a[3];
string t;
while(cin>>t){
for(int i=0;i<3;i++)
a[i]=t[i];
sort(a,a+3);
for(int i=0;i<2;i++)
cout<<a[i]<<" ";
cout<<a[2]<<endl;
}
return 0;
}
输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。
Input
输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数据之间用空格隔开。
Output
对于每组输入数据,输出一行,结果保留两位小数。
Sample
Inputcopy | Outputcopy |
---|---|
0 0 0 1 0 1 1 0 | 1.00 1.41 |
总结:注意输出格式。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<cmath>
using namespace std;
int main()
{
double x1,y1,x2,y2,s;
while(cin>>x1>>y1>>x2>>y2)
{
s=sqrt(pow(x1-x2,2)+pow(y1-y2,2));
printf("%.2f\n",s);
}
return 0;
}
C - 计算球体积
根据输入的半径值,计算球的体积。
Input
输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。
Output
输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。
Sample
Inputcopy | Outputcopy |
---|---|
1 1.5 | 4.189 14.137 |
总结:问题不大。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<cmath>
#define PI 3.1415927
using namespace std;
int main()
{
double r,s;
while(cin>>r)
{
s=PI*4/3*(pow(r,3));
printf("%.3f\n",s);
}
return 0;
}
D - 求绝对值
求实数的绝对值。
Input
输入数据有多组,每组占一行,每行包含一个实数。
Output
对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。
Sample
Inputcopy | Outputcopy |
---|---|
123 -234.00 | 123.00 234.00 |
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<cmath>
using namespace std;
int main()
{
double r,s;
while(cin>>r)
{
s=abs(r);
printf("%.2f\n",s);
}
return 0;
}
输入一个百分制的成绩t,将其转换成对应的等级,具体转换规则如下:
90~100为A;
80~89为B;
70~79为C;
60~69为D;
0~59为E;
Input
输入数据有多组,每组占一行,由一个整数组成。
Output
对于每组输入数据,输出一行。如果输入数据不在0~100范围内,请输出一行:“Score is error!”。
Sample
Inputcopy | Outputcopy |
---|---|
56 67 100 123 | E D A Score is error! |
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#define PI 3.1415927
using namespace std;
int main()
{
int r;
while(cin>>r)
{
if(0<=r&&r<=59)
r=0;
if(60<=r&&r<=69)
r=1;
if(70<=r&&r<=79)
r=2;
if(80<=r&&r<=89)
r=3;
if(90<=r&&r<=100)
r=4;
switch(r)
{
case 0:
cout<<"E"<<endl;
break;
case 1:
cout<<"D"<<endl;
break;
case 2:
cout<<"C"<<endl;
break;
case 3:
cout<<"B"<<endl;
break;
case 4:
cout<<"A"<<endl;
break;
default:
cout<<"Score is error!"<<endl;
}
}
return 0;
}
给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample
Inputcopy | Outputcopy |
---|---|
1985/1/20 2006/3/12 | 20 71 |
总结:输入可以采用scanf("%d/%d/%d",&y,&m,&d),也可以采用switch结构,但是for 循环更快捷一点,需要注意判断润年的条件。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
using namespace std;
int main()
{
int y,m,d1,s;
while(~scanf("%d/%d/%d",&y,&m,&d1))
{ s=0;
d[2]=28;
if((y%100!=0&&y%4==0)||y%400==0)
{
d[2]=29;
}
else
d[2]=28;
for(int i=1;i<m;i++)
{
s+=d[i];
}
s+=d1;
cout<<s<<endl;
}
return 0;
}
G - 求奇数的乘积
给你n个整数,求他们中所有奇数的乘积。
Input
输入数据包含多个测试实例,每个测试实例占一行,每行的第一个数为n,表示本组数据一共有n个,接着是n个整数,你可以假设每组数据必定至少存在一个奇数。
Output
输出每组数中的所有奇数的乘积,对于测试实例,输出一行。
Sample
Inputcopy | Outputcopy |
---|---|
3 1 2 3 4 2 3 4 5 | 3 15 |
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int a[10000];
int main()
{
int n,s;
while(cin>>n){
s=1;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]%2==0)
continue;
else
{
s*=a[i];
}
}
cout<<s<<endl;
}
return 0;
}
给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。
Input
输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成。
Output
对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。
你可以认为32位整数足以保存结果。
Sample
Inputcopy | Outputcopy |
---|---|
1 3 2 5 | 4 28 20 152 |
总结:这个题目我们需要判断输入的值的大小,题目要求需要保存32位整数,使用对变量需要使用long long型。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<cmath>using namespace std;
int main()
{
long long x1,x,y1,y,o,l;
while(cin>>x1>>y1){
x=min(x1,y1);
y=max(x1,y1);
o=0;
l=0;
for(long i=x;i<=y;i++)
{
if(i%2==0)
o+=i*i;
else
l+=i*i*i;
}
cout<<o<<" "<<l<<endl;
}
return 0;
}
统计给定的n个数中,负数、零和正数的个数。
Input
输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数;如果n=0,则表示输入结束,该行不做处理。
Output
对于每组输入数据,输出一行a,b和c,分别表示给定的数据中负数、零和正数的个数。
Sample
Inputcopy | Outputcopy |
---|---|
6 0 1 2 3 -1 0 5 1 2 3 4 0.5 0 | 1 2 3 0 0 5 |
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
double a[10000];
using namespace std;
int main()
{
int n,f,o,z;
while(cin>>n)
{
if(n==0)
{
break;
}
else{
f=0,o=0,z=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]<0)
f++;
if(a[i]==0)
o++;
if(a[i]>0)
z++;
}
cout<<f<<" "<<o<<" "<<z<<endl;
}
}
return 0;
}
J - 求数列的和
数列的定义如下:
数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和。
Input
输入数据有多组,每组占一行,由两个整数n(n<10000)和m(m<1000)组成,n和m的含义如前所述。
Output
对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保留2位小数。
Sample
Inputcopy | Outputcopy |
---|---|
81 4 2 2 | 94.73 3.41 |
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
double a[100000];
using namespace std;
int main()
{
int n,m;
double s;
while(cin>>n>>m)
{
s=0;
a[0]=n;
for(int i=1;i<m;i++)
{
a[i]=sqrt(a[i-1]);
s+=a[i];
}
printf("%.2f\n",s+a[0]);
}
return 0;
}
K - 水仙花数
春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3。
现在要求输出所有在m和n范围内的水仙花数。
Input
输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。
Output
对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。
Sample
Inputcopy | Outputcopy |
---|---|
100 120 300 380 | no 370 371 |
总结:注意格式即可。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
int a[10000];
using namespace std;
int main()
{
int m,n;
while(cin>>m>>n)
{ int b=0,s=0,g=0,tem=0,t=0;
for(int i=m;i<=n;i++)
{
tem=i;
b=tem/100;
g=tem%10;
tem=tem/10;
s=tem%10;
if(b*b*b+s*s*s+g*g*g==i)
{
a[t]=i;
t++;
}
}
if(t==0)
{
cout<<"no"<<endl;
}
else
{
for(int i=0;i<t-1;i++)
{
cout<<a[i]<<" ";
}
cout<<a[t-1]<<endl;
}
}
return 0;
}
多项式的描述如下:
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ...
现在请你求出该多项式的前n项的和。
Input
输入数据由2行组成,首先是一个正整数m(m<100),表示测试实例的个数,第二行包含m个正整数,对于每一个整数(不妨设为n,n<1000),求该多项式的前n项的和。
Output
对于每个测试实例n,要求输出多项式前n项的和。每个测试实例的输出占一行,结果保留2位小数。
Sample
Inputcopy | Outputcopy |
---|---|
2 1 2 | 1.00 0.50 |
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
int a[1000];
using namespace std;
int main()
{
int m;
double s;
while(cin>>m)
{
for(int i=0;i<m;i++)
{
cin>>a[i];
s=0;
int k=1;
for(int j=1;j<=a[i];j++)
{
s+=k*1.0/j;
k=-1*k;
}
printf("%.2f\n",s);
}
}
return 0;
}
对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数。
Input
输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。
Output
对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。
Sample
Inputcopy | Outputcopy |
---|---|
0 1 0 0 | OK |
总结:审清题目求表达式的值是否为素数。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
int a[1000];
using namespace std;
int main()
{
int x,y,s,flag;
while(cin>>x>>y)
{
if(x==y&&y==0)
break;
s=0;
flag=1;
for(int i=x;i<=y;i++)
{
s=i*i+i+41;
for(int j=2;j<s;j++)
{
if(s%j==0)
{ flag=0;
break;}
}
if(flag==0)
break;
}
if(flag==1)
cout<<"OK"<<endl;
else
cout<<"Sorry"<<endl;
}
return 0;
}
喜欢西游记的同学肯定都知道悟空偷吃蟠桃的故事,你们一定都觉得这猴子太闹腾了,其实你们是有所不知:悟空是在研究一个数学问题!
什么问题?他研究的问题是蟠桃一共有多少个!
不过,到最后,他还是没能解决这个难题,呵呵^-^
当时的情况是这样的:
第一天悟空吃掉桃子总数一半多一个,第二天又将剩下的桃子吃掉一半多一个,以后每天吃掉前一天剩下的一半多一个,到第n天准备吃的时候只剩下一个桃子。聪明的你,请帮悟空算一下,他第一天开始吃的时候桃子一共有多少个呢?
Input
输入数据有多组,每组占一行,包含一个正整数n(1<n<30),表示只剩下一个桃子的时候是在第n天发生的。
Output
对于每组输入数据,输出第一天开始吃的时候桃子的总数,每个测试实例占一行。
Sample
Inputcopy | Outputcopy |
---|---|
2 4 | 4 22 |
总结:逆着推,然后求表达式。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
int a[1000];
using namespace std;
int main()
{
int n,s;
while(cin>>n)
{
s=1;
for(int i=0;i<n-1;i++)
s=(s+1)*2;
cout<<s<<endl;
}
return 0;
}
青年歌手大奖赛中,评委会给参赛选手打分。选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分。
Input
输入数据有多组,每组占一行,每行的第一个数是n(2<n<=100),表示评委的人数,然后是n个评委的打分。
Output
对于每组输入数据,输出选手的得分,结果保留2位小数,每组输出占一行。
Sample
Inputcopy | Outputcopy |
---|---|
3 99 98 97 4 100 99 98 97 | 98.00 98.50 |
总结:可以用*max_element(a,a+n)!=a[j]以求得数组最大值。最小值*min_element(a,a+n)!=a[j]。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
double a[1000];
using namespace std;
int main()
{
int n;
double s;
while(cin>>n)
{
s=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int j=0;j<n;j++)
{
if(*max_element(a,a+n)!=a[j]&&*min_element(a,a+n)!=a[j])
{
s+=a[j];
}
}
printf("%.2f\n",s/(n-2));
}
return 0;
}
有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。编程输出该平均值序列。
Input
输入数据有多组,每组占一行,包含两个正整数n和m,n和m的含义如上所述。
Output
对于每组输入数据,输出一个平均值序列,每组输出占一行。
Sample
Inputcopy | Outputcopy |
---|---|
3 2 4 2 | 3 6 3 7 |
总结:首先判断输入的值的数目是否满足为m的倍数,然后直接求解,如果不满足则先处理前(n/m)*m个数,然后再处理剩余的数。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
int a[1000],b[1000];
using namespace std;
int main()
{
int n,m;
while(cin>>n>>m)
{
int t=0,j=0;
b[0]=0;
for(int i=1;i<=n;i++)
{
b[i]=0;
a[i]=2*i;
}
if(n%m==0)
{
for(int i=1;i<=n;i++)
{
t++;
b[j]+=a[i];
if(t==m)
{ b[j]=b[j]/m;
j++;
t=0;
}
}
j--;
}
else
{
for(int i=1;i<=(n/m)*m;i++)
{
t++;
b[j]+=a[i];
if(t==m)
{
b[j]=b[j]/m;
j++;
t=0;
}
}
for(int i=(n/m)*m+1;i<=n;i++)
{t++;
b[j]+=a[i];}
b[j]=b[j]/t;
}
for(int i=0;i<j;i++)
{
cout<<b[i]<<" ";
}
cout<<b[j]<<endl;
}
return 0;
}
Q - 字符串统计
对于给定的一个字符串,统计其中数字字符出现的次数。
Input
输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。
Output
对于每个测试实例,输出该串中数值的个数,每个输出占一行。
Sample
Inputcopy | Outputcopy |
---|---|
2 asdfasdf123123asdfasdf asdf111111111asdfasdfasdf | 6 9 |
总结:直接判断他们的ascall码是否在0~9之间。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<string>
int a[1000],b[1000];
using namespace std;
int main()
{
int s=0,n;
string p;
while(cin>>n)
{
for(int y=0;y<n;y++){
s=0;
cin>>p;
for(int i=0;i<p.size();i++)
{
if(p[i]-'0'>=0&&p[i]-'0'<=9)
s++;
}
cout<<s<<endl;
}
}
return 0;
}
R - 母牛的故事
有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
Input
输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
n=0表示输入数据的结束,不做处理。
Output
对于每个测试实例,输出在第n年的时候母牛的数量。
每个输出占一行。
Sample
Inputcopy | Outputcopy |
---|---|
2 4 5 0 | 2 4 6 |
总结:这是一个类似于斐波那契数列,我们可以试试求一下前面6个的值,不难推出表达式为 a[i]=a[i-2]+a[i-3]+a[i-4];
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<string>
int a[1000];
using namespace std;
int main()
{
a[1]=1;
a[2]=2;
a[3]=3;
a[4]=4;
int n;
while(cin>>n)
{
if(n==0)
break;
if(n<=4)
{
cout<<a[n]<<endl;
}
else
{
for(int i=5;i<=n;i++)
a[i]=a[i-2]+a[i-3]+a[i-4];
cout<<a[n]<<endl;
}
}
return 0;
}
S - 数列有序!
有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。
Input
输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。
Output
对于每个测试实例,输出插入新的元素后的数列。
Sample
Inputcopy | Outputcopy |
---|---|
3 3 1 2 4 0 0 | 1 2 3 4 |
总结:我们可以先遍历找到第一大于它的值的下标i,然后再将i和后面的值依次往后面移动一个位置,最后将插入的值放入下标为i的数组里面.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<string>
int a[1000],b[100];
using namespace std;
int main()
{
int n,m,t,j;
while(cin>>n>>m)
{
j=0;
if(n==m&&m==0)
break;
for(int i=0;i<n;i++)
{cin>>a[i];
if(a[i]>m)
{
b[j++]=i;
}
}
for(int i=n-1;i>=b[0];i--)
a[i+1]=a[i];
a[b[0]]=m;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<a[n]<<endl;
}
return 0;
}
T - 绝对值排序
输入n(n<=100)个整数,按照绝对值从大到小排序后输出。题目保证对于每一个测试实例,所有的数的绝对值都不相等。
Input
输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整数,n=0表示输入数据的结束,不做处理。
Output
对于每个测试实例,输出排序后的结果,两个数之间用一个空格隔开。每个测试实例占一行。
Sample
Inputcopy | Outputcopy |
---|---|
3 3 -4 2 4 0 1 2 -3 0 | -4 3 2 -3 2 1 0 |
总结:引入cmp函数,完成绝对值排序。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<string>
int a[1000],b[100];
using namespace std;
int cmp(int x,int y)
{
return abs(x)>abs(y);
}
int main()
{
int n;
while(cin>>n)
{
if(n==0)
break;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n,cmp);
for(int i=0;i<n-1;i++)
cout<<a[i]<<" ";
cout<<a[n-1]<<endl;
}
return 0;
}
作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了,呵呵
但是对于学校财务处的工作人员来说,这一天则是很忙碌的一天,财务处的小胡老师最近就在考虑一个问题:如果每个老师的工资额都知道,最少需要准备多少张人民币,才能在给每位老师发工资的时候都不用老师找零呢?
这里假设老师的工资都是正整数,单位元,人民币一共有100元、50元、10元、5元、2元和1元六种。
Input
输入数据包含多个测试实例,每个测试实例的第一行是一个整数n(n<100),表示老师的人数,然后是n个老师的工资。
n=0表示输入的结束,不做处理。
Output
对于每个测试实例输出一个整数x,表示至少需要准备的人民币张数。每个输出占一行。
Sample
Inputcopy | Outputcopy |
---|---|
3 1 2 3 0 | 4 |
总结:采用动态规划方法(查表法),行表示钱的面值c[i],列表示需要换的钱数j(从0开始)if(c[i]<=j),则表示可以换取面额为c[i]的纸币,但是要选择最小的张数所以 b[i][j]=min(b[i-1][j],b[i][j-c[i]]+1);如果c[i]>j,则表示换不了c[i]纸币,纸币的数量还是 b[i][j]=b[i-1][j];需要注意我们要将吧b[i][j]进行初始化,第一列的值全为0,因为需要换取的钱为0,第一行的值全为无穷大,因为要换取的钱的面值为0。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<string>
int a[1000],b[10000][10000];
using namespace std;
int main()
{
int n,c[7]={0,1,2,5,10,50,100};
while(cin>>n)
{
memset(b,0,10000);
if(n==0)
break;
int s=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
for(int i2=0;i2<=a[i];i2++)
b[0][i2]=10000000;
for(int i1=1;i1<=6;i1++)
{
for(int j=1;j<=a[i];j++)
{
if(c[i1]>j)
b[i1][j]=b[i1-1][j];
else
b[i1][j]=min(b[i1-1][j],b[i1][j-c[i1]]+1);
}
}
s+=b[6][a[i]];
}
cout<<s<<endl;
}
return 0;
}
V - 海选女主角
potato老师虽然很喜欢教书,但是迫于生活压力,不得不想办法在业余时间挣点外快以养家糊口。
“做什么比较挣钱呢?筛沙子没力气,看大门又不够帅...”potato老师很是无奈。
“张艺谋比你还难看,现在多有钱呀,听说还要导演奥运开幕式呢!你为什么不去娱乐圈发展呢?”lwg在一旁出主意。
嗯,也是,为了生存,就委屈点到娱乐圈混混吧,马上就拍一部激光电影《杭电记忆――回来我的爱》。
说干就干,马上海选女主角(和老谋子学的,此举可以吸引媒体的眼球,呵呵),并且特别规定,演员必须具有ac的基本功,否则直接out!
由于策划师风之鱼(大师级水王)宣传到位,来应聘的MM很多,当然包括nit的蛋糕妹妹等呼声很高的美女,就连zjut的jqw都男扮女装来应聘(还好被安全顾问hdu_Bin-Laden认出,给轰走了),看来娱乐圈比acm还吸引人哪...
面试那天,刚好来了m*n个MM,站成一个m*n的队列,副导演Fe(OH)2为每个MM打了分数,分数都是32位有符号整数。
一开始我很纳闷:分数怎么还有负的?Fe(OH)2解释说,根据选拔规则,头发染成黄色、化妆太浓、穿的太少等等都要扣分数的,扣的多了就可能是负分了,当然,如果发现话语中夹有日语,就直接给-2147483648分了。
分数送上来了,是我做决定的时候了,我的一个选拔原则是,要选一个面试分数绝对值(必须还是32位整数)最大的MM。
特别说明:如果不幸选中一个负分的MM,也没关系,因为我觉得,如果不能吸引你,那要想法恶心你。
Input
输入数据有多组,每组的第一行是两个整数m和n,表示应聘MM的总共的行列数,然后是m行整数,每行有n个,m和n的定义见题目的描述。
Output
对于每组输入数据,输出三个整数x,y和s,分别表示选中的MM的行号、列号和分数。
note:行号和列号从一开始,如果有多个MM的分数绝对值一样,那么输出排在最前面的一个(即行号最小的那个,如果行号相同则取列号最小的那个)。
Sample
Inputcopy | Outputcopy |
---|---|
2 3 1 4 -3 -7 3 0 | 2 1 -7 |
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<cmath>
int a[100][100];
using namespace std;
int main()
{
int m,n;
while(cin>>m>>n)
{
int maxlen=0,h=0,l=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>a[i][j];
if(abs(a[i][j])>abs(maxlen))
{
maxlen=a[i][j];
h=i;
l=j;
}
}
}
cout<<h+1<<" "<<l+1<<" "<<maxlen<<endl;
}
return 0;
}
假设一个班有n(n<=50)个学生,每人考m(m<=5)门课,求每个学生的平均成绩和每门课的平均成绩,并输出各科成绩均大于等于平均成绩的学生数量。
Input
输入数据有多个测试实例,每个测试实例的第一行包括两个整数n和m,分别表示学生数和课程数。然后是n行数据,每行包括m个整数(即:考试分数)。
Output
对于每个测试实例,输出3行数据,第一行包含n个数据,表示n个学生的平均成绩,结果保留两位小数;第二行包含m个数据,表示m门课的平均成绩,结果保留两位小数;第三行是一个整数,表示该班级中各科成绩均大于等于平均成绩的学生数量。
每个测试实例后面跟一个空行。
Sample
Inputcopy | Outputcopy |
---|---|
2 2 5 10 10 20 | 7.50 15.00 7.50 15.00 1 |
总结:这个题目不难,但是我却交了3次,第一次是超时,我将所有的循环都分开写,然后再提交,提示格式错误,原来是每个测试实例后面跟一个空行,一定要审题仔细,然后就是要注意一下格式以及数据类型。
//W - 求平均成绩
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int main(){
int n,m;
while(cin>>n>>m){
double a[50][5];
double savg[5]={};
double avg[50]={};
int num=0;
memset(a,0,50);
memset(savg,0,5);
memset(avg,0,50);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>a[i][j];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
avg[i]=avg[i]+a[i][j];
}
avg[i]=(double)avg[i]/m*1.0;
if(i<n-1){
printf("%.2f ",avg[i]);}
else
{
printf("%.2f\n",avg[n-1]);
}
}
for(int j=0;j<m;j++){
for(int i=0;i<n;i++){
savg[j]=savg[j]+a[i][j];
}
savg[j]=(double)savg[j]/n*1.0;
if(j<m-1)
{
printf("%.2f ",savg[j]);
}
else
{
printf("%.2f\n",savg[m-1]);
}
}
for(int i=0;i<n;i++){
int flag=0;
for(int j=0;j<m;j++){ if(a[i][j]<savg[j]) {flag=1;break;}}
if(flag==0){num++; flag=0;}
}
cout<<num<<endl;
cout<<endl;
}
return 0;
}
输入一个字符串,判断其是否是C的合法标识符。
Input
输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。
Output
对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出"yes",否则,输出“no”。
Sample
Inputcopy | Outputcopy |
---|---|
3 12ajf fi8x_a ff ai_2 | no yes no |
总结:1,所有的字符都为大小写字母以及数字和下划线,不能含有空格。
2.第一个字符不能是数字.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstdlib>
using namespace std;
int main()
{
int n,t;
string s;
while(cin>>n)
{ getchar();
for(int j=0;j<n;j++){
getline(cin,s);
int flag=1;
for(int i=0;i<s.size();i++)
{
if(('a'<=s[i]&&s[i]<='z')||('A'<=s[i]&&s[i]<='Z')||('0'<=s[i]&&s[i]<='9')||s[i]=='_')
{
if((0<=s[0]-'0'&&s[0]-'0'<=9))
{
cout<<"no"<<endl;
flag=0;
break;
}
}
else
{
flag=0;
cout<<"no"<<endl;
break;
}
}
if(flag==1)
{
cout<<"yes"<<endl;
}
}
}
return 0;
}
Y - 查找最大元素
对于输入的每个字符串,查找其中的最大字母,在该字母后面插入字符串“(max)”。
Input
输入数据包括多个测试实例,每个实例由一行长度不超过100的字符串组成,字符串仅由大小写字母构成。
Output
对于每个测试实例输出一行字符串,输出的结果是插入字符串“(max)”后的结果,如果存在多个最大的字母,就在每一个最大字母后面都插入"(max)"。
Sample
Inputcopy | Outputcopy |
---|---|
abcdefgfedcba xxxxx | abcdefg(max)fedcba x(max)x(max)x(max)x(max)x(max) |
总结:首先找到最大的字母是哪个,然后依次遍历数组如果等于最大的字母就在其后面输出(max).
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstdlib>
using namespace std;
int main()
{
string s;
char a;
char b[1000];
int index;
while(cin>>s)
{
index=0;
a=s[0];
for(int i=0;i<s.size();i++)
{ b[i]=s[i];
if((b[i])>(a))
{
a=b[i];
index=i;
}
}
for(int i=0;i<s.size();i++)
{
if(s[i]==a)
{
cout<<s[i]<<"(max)";
}
else{
cout<<s[i];
}
}
cout<<endl;
}
return 0;
}
Z - 首字母变大写
输入一个英文句子,将每个单词的第一个字母改成大写字母。
Input
输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行。
Output
请输出按照要求改写后的英文句子。
Sample
Inputcopy | Outputcopy |
---|---|
i like acm i want to get an accepted | I Like Acm I Want To Get An Accepted |
总结:这个题目我们的输入应该采用gets(),这样空格也会被接收,如果是cin>>就会跳过空格,输出格式就会出现问题,题目思路是这样的首先将第一个字符变成大写,然后依次遍历字符串,如果遇到空格就将后面的字符变成大写。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int main()
{
char a[1000];
while(gets(a))
{
for(int i=0;i<sizeof(a);i++ )
{
if(i==0)
a[i]=a[i]-32;
if(a[i]==' ')
{
a[i+1]=a[i+1]-32;
}
}
cout<<a<<endl;
}
return 0;
结束语:加油吧!