工作室新生練習題專題 [Cloned]
專題鏈接:https://vjudge.net/contest/335547
A - 計算球體積
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2002
代碼:
#include <stdio.h>
#include <stdlib.h>
#define PI 3.1415927
int main()
{
double r,v;
while(~scanf("%lf",&r))
{
v=(4*PI*r*r*r)/3;
printf("%.3lf\n",v);
}
return 0;
}
B - 第幾天?
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2005
代碼:
#include <stdio.h>
int main()
{
int y,m,d,total;
while(~scanf("%d/%d/%d",&y,&m,&d))
{
if((y%400==0)||(y%4==0&&y%100!=0))
{
switch(m)
{
case 1:
total=d;
break;
case 2:
total=31+d;
break;
case 3:
total=31+29+d;
break;
case 4:
total=31+29+31+d;
break;
case 5:
total=31+29+31+30+d;
break;
case 6:
total=31+29+31+30+31+d;
break;
case 7:
total=31+29+31+30+31+30+d;
break;
case 8:
total=31+29+31+30+31+30+31+d;
break;
case 9:
total=31+29+31+30+31+30+31+30+d;
break;
case 10:
total=31+29+31+30+31+30+31+30+30+d;
break;
case 11:
total=31+29+31+30+31+30+31+31+30+31+d;
break;
case 12:
total=31+29+31+30+31+30+31+31+30+31+30+d;
break;
}
printf("%d\n",total);
}
else
{
switch(m)
{
case 1:
total=d;
break;
case 2:
total=31+d;
break;
case 3:
total=31+28+d;
break;
case 4:
total=31+28+31+d;
break;
case 5:
total=31+28+31+30+d;
break;
case 6:
total=31+28+31+30+31+d;
break;
case 7:
total=31+28+31+30+31+30+d;
break;
case 8:
total=31+28+31+30+31+30+31+d;
break;
case 9:
total=31+28+31+30+31+30+31+30+d;
break;
case 10:
total=31+28+31+30+31+30+31+30+30+d;
break;
case 11:
total=31+28+31+30+31+30+31+31+30+31+d;
break;
case 12:
total=31+28+31+30+31+30+31+31+30+31+30+d;
break;
}
printf("%d\n",total);
}
}
return 0;
}
C - 求奇數的乘積
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2006
代碼:
#include <stdio.h>
int main()
{
int n,i,a;
while(~scanf("%d",&n))
{
int age=1;
for(i=0;i<n;i++)
{
scanf("%d",&a);
if(a%2==1)
{
age*=a;
}
}
printf("%d\n",age);
}
return 0;
}
D - 平方和與立方和
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2007
代碼:
#include <stdio.h>
int main()
{
int m,n,i,t;
while(~scanf("%d %d",&m,&n))
{
int sum1=0,sum2=0;
if(m>n)
{
t=m;
m=n;
n=t;
}
for(i=m;i<=n;i++)
{
if(i%2==0)
{
sum1=sum1+i*i;
}
else
{
sum2=sum2+i*i*i;
}
}
printf("%d %d\n",sum1,sum2);
}
return 0;
}
E - 數值統計
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2008
代碼:
#include <stdio.h>
int main()
{
int n,i;
double x;
while(~scanf("%d",&n))
{
int a=0,b=0,c=0;
if(n==0)
{
break;
}
for(i=0;i<n;i++)
{
scanf("%lf",&x);
if(x<0)
{
a++;
}
else if(x==0)
{
b++;
}
else
{
c++;
}
}
printf("%d %d %d\n",a,b,c);
}
return 0;
}
F - 求數列的和
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2009
代碼:
#include <stdio.h>
#include <math.h>
int main()
{
int m,i;
double n;
while(~scanf("%lf %d",&n,&m))
{
double sum=n;
for(i=1;i<m;i++)
{
n=sqrt(n);
sum+=n;
}
printf("%.2lf\n",sum);
}
return 0;
}
G - 多項式求和
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2011
代碼:
#include <stdio.h>
int main()
{
int m,n,i;
double sum;
scanf("%d",&m);
while(m--)
{
sum=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
{
sum-=1.0/i;
}
else
{
sum+=1.0/i;
}
}
printf("%.2lf\n",sum);
}
return 0;
}
H - 素數判定
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2012
代碼:
#include <stdio.h>
#include <math.h>
int main()
{
int x,y,i,j,t,a;
while(~scanf("%d %d",&x,&y))
{
int c=0;
if(x==0&&y==0)
{
break;
}
if(x>y)
{
t=x;
x=y;
y=t;
}
for(i=x; i<=y; i++)
{
a=i*i+i+41;
for(j=2; j<=sqrt(a); j++)
{
if(a%j==0)
c++;
}
}
if(c==0)
{
printf("OK\n");
}
else
{
printf("Sorry\n");
}
}
return 0;
}
I - 蟠桃記
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2013
代碼:
#include <stdio.h>
int main()
{
int n,i;
while(~scanf("%d",&n))
{
int x=1;
for(i=n;i>1;i--)
{
x=2*(x+1);
}
printf("%d\n",x);
}
return 0;
}
J - 青年歌手大獎賽_評委會打分
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2014
代碼:
#include <stdio.h>
double a[105];
int main()
{
int n,i,j,t,sum,x;
double avg;
while(~scanf("%d",&n)&&n>2&&n<=100)
{
x=n;
for(i=0;i<n;i++)
{
scanf("%lf",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
sum=0;
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
avg=(sum-a[0]-a[n-1])/(x-2);
printf("%.2lf\n",avg);
}
return 0;
}
K - 偶數求和
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2015
代碼:
#include<stdio.h>
int a[105];
int b[105];
int main()
{
int i,j,k;
for(i=1; i<=100; i++)
{
a[i]=2*i;
}
int n,m;
while(~scanf("%d %d",&n,&m))
{
if(n>m && n%m==0)
{
j=n/m;
b[1]=m+1;
for(i=2; i<=j; i++)
{
b[i]=b[i-1]+m*2;
}
for(i=1; i<=j; i++)
{
if(i!=j)
{
printf("%d ",b[i]);
}
else
printf("%d",b[i]);
}
printf("\n");
}
else if(n>m && n%m!=0)
{
j=n/m;
k=n-j*m;
b[1]=m+1;
for(i=2; i<=j; i++)
{
b[i]=b[i-1]+m*2;
}
b[j+1]=(2*n-k+1);
for(i=1; i<=j+1; i++)
{
if(i!=j+1)
{
printf("%d ",b[i]);
}
else
printf("%d",b[i]);
}
printf("\n");
}
else if(n==m)
{
printf("%d",m+1);
printf("\n");
}
}
return 0;
}
L - 數據的交換輸出
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2016
代碼:
#include <stdio.h>
int main()
{
int a[110];
int n,i,j,min,k;
while(~scanf("%d",&n))
{
min=0;
if(n==0)
{
break;
}
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
min=a[0];
for(i=0;i<n;i++)
{
if(a[i]<=min)
{
min=a[i];
k=i;
}
}
j=a[k];
a[k]=a[0];
a[0]=j;
for(i=0;i<n;i++)
{
if(i!=n-1)
{
printf("%d ",a[i]);
}
else
{
printf("%d",a[i]);
}
}
printf("\n");
}
return 0;
}
M - 字符串統計
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2017
代碼:
#include <stdio.h>
#include <string.h>
char str[100000];
int main()
{
int i,n,len;
scanf("%d",&n);
while(n--)
{
int c=0;
scanf("%s",str);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]>='0'&&str[i]<='9')
{
c++;
}
}
printf("%d\n",c);
}
return 0;
}
N - 母牛的故事
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2018
代碼:
#include <stdio.h>
int a[60];
int main()
{
int n,i;
while(~scanf("%d",&n))
{
a[1]=1;
a[2]=2;
a[3]=3;
if(n==0)
{
break;
}
else if(n==1)
{
printf("1\n");
}
else if(n==2)
{
printf("2\n");
}
else if(n==3)
{
printf("3\n");
}
else
{
for(i=4;i<=n;i++)
{
a[i]=a[i-1]+a[i-3];
}
printf("%d\n",a[n]);
}
}
return 0;
}
O - 數列有序!
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2019
代碼:
#include <stdio.h>
int a[110];
int main()
{
int n,m,i,j,t;
while(~scanf("%d %d",&n,&m))
{
if(n==0&&m==0)
{
break;
}
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
a[n]=m;
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(i=0;i<=n;i++)
{
if(i!=n)
{
printf("%d ",a[i]);
}
else
{
printf("%d",a[i]);
}
}
printf("\n");
}
return 0;
}
P - 絕對值排序
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2020
代碼:
#include <stdio.h>
#include <math.h>
int a[110];
int main()
{
int n,i,j,t;
while(~scanf("%d",&n))
{
if(n==0)
{
break;
}
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(abs(a[j])>abs(a[j+1]))
{
t=a[j];
a[i]=a[j+1];
a[j+1]=t;
}
}
}
for(i=0;i<n;i++)
{
if(i!=n-1)
{
printf("%d ",a[i]);
}
else
{
printf("%d",a[i]);
}
}
printf("\n");
}
return 0;
}
Q - 發工資咯:)
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2021
代碼:
#include <stdio.h>
int a[105];
int main()
{
int n,i;
int b,c,d,e,f,g;
while(~scanf("%d",&n))
{
int sum=0;
if(n==0)
{
break;
}
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
b=a[i]/100;
c=(a[i]%100)/50;
d=(a[i]%100)%50/10;
e=(a[i]%100)%50%10/5;
f=(a[i]%100)%50%10%5/2;
g=(a[i]%100)%50%10%5%2;
sum=sum+b+c+d+e+f+g;
}
printf("%d\n",sum);
}
return 0;
}
R - 海選女主角
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2022
代碼:
#include <stdio.h>
#include <math.h>
int main()
{
int n,m,x,y,i,j;
double a,s;
while(~scanf("%d%d",&n,&m))
{
a=0,x=0,y=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%lf",&s);
if(fabs(s)>fabs(a))
{
a=s;
x=i;
y=j;
}
}
}
printf("%d %d %.0lf\n",x+1,y+1,a);
}
return 0;
}
S - 求平均成績
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2023
代碼:
#include <stdio.h>
int main()
{
int a[100][20];
double b[20];
int n,m,i,j,k;
double avg1,avg2;
double sum1,sum2;
while(~scanf("%d%d",&n,&m))
{
avg1=avg2=0;
sum1=sum2=0;
int c=0,x=0;
for( i=0;i<n;i++)
{
for( j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
for( i=0;i<n;i++)
{
for( j=0;j<m;j++)
{
sum1+=a[i][j];
}
avg1=sum1/m;
if(i!=0)
{
printf(" ");
}
printf("%.2lf",avg1);
sum1=0;
}
printf("\n");
for( i=0;i<m;i++)
{
for( j=0;j<n;j++)
{
sum2+=a[j][i];
}
avg2=sum2/n;
if(i!=0)
{
printf(" ");
}
printf("%.2lf",avg2);
sum2=0;
b[c++]=avg2;
}
printf("\n");
for(i=0;i<n;i++)
{
k=0;
for(j=0;j<m;j++)
{
if(a[i][j] >= b[j])
{
k++;
}
else
{
break;
}
}
if(k==m)
{
x++;
}
}
printf("%d\n",x);
printf("\n");
}
return 0;
}
T - C語言合法標識符
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2024
代碼:
#include <stdio.h>
#include <string.h>
int main()
{
int n,i;
char ch[55];
scanf("%d",&n);
getchar();
while(n--)
{
memset(ch,'\0',sizeof(ch));
int flag=1;
gets(ch);
int len=strlen(ch);
if(ch[0]=='_'||(ch[0]>='a'&&ch[0]<='z')||(ch[0]>='A'&&ch[0]<='Z'))
{
flag=0;
for(i=1;i<len;i++)
{
if(ch[i]=='_' || (ch[i]>='a'&&ch[i]<='z') || (ch[i]>='A'&&ch[i]<='Z') || (ch[i]>='0'&&ch[i]<='9'))
{
flag=0;
}
else
{
flag=1;
break;
}
}
if(flag==0)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
else
{
printf("no\n");
}
}
return 0;
}
U - 查找最大元素
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2025
代碼:
#include <stdio.h>
#include <string.h>
char str[105];
int main()
{
int i;
char max;
while(gets(str)!=NULL)
{
int len=strlen(str);
max=str[0];
for(i=0;i<len;i++)
{
if(max<str[i])
{
max=str[i];
}
}
for(i=0;i<len;i++)
{
printf("%c",str[i]);
if(max==str[i])
{
printf("(max)");
}
}
printf("\n");
}
return 0;
}
V - 首字母變大寫
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2026
代碼:
#include <stdio.h>
#include <string.h>
char str[105];
int main()
{
int i;
while(gets(str)!=NULL)
{
int len=strlen(str);
str[0]-=32;
for(i=0;i<len;i++)
{
if(str[i]==' ')
{
str[i+1]-=32;
}
}
puts(str);
}
return 0;
}
W - 統計元音
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2027
代碼:
#include <stdio.h>
#include <string.h>
char str[105];
int main()
{
int n,i,j;
scanf("%d",&n);
getchar();
for(j=0;j<n;j++)
{
gets(str);
int a=0,b=0,c=0,d=0,e=0;
int len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]=='a')
{
a++;
}
if(str[i]=='e')
{
b++;
}
if(str[i]=='i')
{
c++;
}
if(str[i]=='o')
{
d++;
}
if(str[i]=='u')
{
e++;
}
}
printf("a:%d\n",a);
printf("e:%d\n",b);
printf("i:%d\n",c);
printf("o:%d\n",d);
printf("u:%d\n",e);
if(j!=n-1)
{
printf("\n");
}
}
return 0;
}
X - Lowest Common Multiple Plus
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2028
代碼:
#include <stdio.h>
int gcd (int a,int b)
{
return b==0?a:gcd(b,a%b); //求最大公約數函數
}
int gbd(int a,int b)
{
return a/gcd(a,b)*b; //防溢出,求最小公倍數
}
int main()
{
int n,i,s,a;
while(~scanf("%d",&n))
{
scanf("%d",&s);
for(i=1;i<n;i++) //因爲已經輸入一個數s,所以在輸入n-1位數
{
scanf("%d",&a);
s=gbd(s,a); //每求一次最小公倍數
}
printf("%d\n",s);
}
return 0;
}
Y - Palindromes _easy version
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2029
代碼:
#include <stdio.h>
#include <string.h>
char str[10000000];
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf("%s",str);
int flag=0;
int i;
int len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]!=str[len-i-1])
{
flag =1;
break;
}
}
if(flag==0)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
return 0;
}
Z - 漢字統計
原題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2030
代碼:
#include <stdio.h>
#include <string.h>
char str[1000000000];
int main()
{
int n,i;
scanf("%d",&n);
getchar();
while(n--)
{
int c=0;
gets(str);
int len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]<0)
{
c++;
}
}
printf("%d\n",c/2);
}
return 0;
}