由于这些代码也是我初学时写的代码,故其中的规范程度及简洁程度并不很好(此处我后来写的有可以参考一下->C语言代码规范),但是能很好的接近出初学者的水平,也更有参考价值!排版不易,喜欢就点个赞吧!如有问题,请勿吐槽,欢迎留言互相学习。
第7周编程题在线测试
代码实现
#include
#include
#include
double Y(double x, int n);
int main()
{
double x;
int n;
printf("Please input x and n:");
scanf("%lf,%d",&x,&n);
printf("Result=%.2f\n",Y(x,n));
return 0;
}
double Y(double x, int n)
{
double y;
for(y=0;n>0;n--)
{
y+=x;
y=sqrt(y);
}
return y;
}
代码实现
#include
#include
int Sum(int n);
int main()
{
int n;
printf("Please input n:");
scanf("%d",&n);
if(n>0)
{
printf("sum=%d\n",Sum(n));
}
else
{
printf("data error!\n");
}
return 0;
}
int Sum(int n)
{
int a;
for(a=0;n>0;n--)
{
a+=n;
}
return a;
}
代码实现
#include
#include
int Monkey(int n, int x);
int main()
{
int n,a;
printf("Input days n:");
scanf("%d",&n);
printf("x=%d\n",Monkey(n,a));
return 0;
}
int Monkey(int n, int x)
{
int a;
for(a=1;n>1;n--)
{
a=(a+1)*2;
}
return a;
}
代码实现
#include
#include
int main()
{
float p;
printf("Input payment:");
scanf("%f",&p);
if(p<100)
{
printf("price = %.1f\n",p);
}
else if(p<200)
{
p*=0.95;
printf("price = %.1f\n",p);
}
else if(p<500)
{
p*=0.92;
printf("price = %.1f\n",p);
}
else if(p<1000)
{
p*=0.90;
printf("price = %.1f\n",p);
}
else
{
p*=0.85;
printf("price = %.1f\n",p);
}
return 0;
}
练兵区——编程题
代码实现
#include
#include
double Calculate(double x,double y);
int main()
{
double a,b;
printf("Input two doubles:\n");
scanf("%lf%lf",&a,&b);
printf("1/((1/x+1/y)/2) = %0.3f\n",Calculate(a,b));
return 0;
}
double Calculate(double x,double y)
{
return 1/((1/x+1/y)/2);
}
代码实现
#include
#include
void Chline(char ch, int column, int row);
int main()
{
char a;
int b,c,i;
printf("input a char:\n");
scanf("%c",&a);
printf("input column and row:\n");
scanf("%d%d",&b,&c);
for(i=0;i<c;i++)
{
Chline(a,b,c);
printf("\n");
}
return 0;
}
void Chline(char ch, int column, int row)
{
int d;
for(d=0;d<column;d++)
{
printf("%c",ch);
}
}
代码实现
#include
#include
int Magic(int m);
int main()
{
int a;
scanf("%d",&a);
if(Magic(a)==0)
{
printf("The sum you calculated is wrong!\n");
}
else
{
printf("The number is %d\n",Magic(a));
}
return 0;
}
int Magic(int m)
{
int a,b,c,d;
for(d=100;d<=999;d++)
{
a = d/100,b = (d%100)/10,c = d%10;
if((a+b+b+c+c)*100+(a+a+b+c+c)*10+a+a+b+b+c==m)
{
return d;
}
else
continue;
}
return 0;
}
代码实现
#include
#include
int main()
{
int s,n,i,t=21;
for(s=0,i=1;i<=21;i++,t++)
{
s+=6;
if(s%5==0 && s<=105)
{
t--;
}
}
for(s=0,i=1;i<=21;i++,t++)
{
s+=7;
if(s%5==0 && s%6==0 && s<=105)
{
t--;
}
else if((s%5==0 && s<=105)||(s%6==0 && s<=126))
{
t--;
}
}
printf("n=%d",t);
return 0;
}
代码实现
#include
int divide(int n, int m);
static int people;
int main()
{
int i;
int n;
printf("Input n(1);
scanf("%d", &n);
people = n;
if (n <= 1 || n > 5)
{
printf("Error!\n");
}
else
{
for (i = 1; ; i++)
{
if (divide(i, n))
{
printf("y=%d\n", i);
break;
}
}
}
return 0;
}
int divide(int n, int m)
{
if (n / people == 0 || n % people != 1)
return 0;
if (m == 1)
return 1;
return divide(n - n / people - 1, m - 1);
}
代码实现
#include
#include
unsigned int ComputeAge(unsigned int n);
int main()
{
int a;
scanf("%u",&a);
printf("The person's age is %u",ComputeAge(a));
return 0;
}
unsigned int ComputeAge(unsigned int n)
{
int a=10;
for( ;n>1;n--)
{
a+=2;
}
return a;
}
代码实现
#include
#include
int God(int a, int b);
int main()
{
int a, b;
printf("Input a,b:");
scanf("%d,%d",&a,&b);
if(a<=0 || b<=0)
{
printf("Input error!\n");
}
else
{
printf("%d\n",God(a,b));
}
return 0;
}
int God(int a, int b)
{
if(a==b)
{
return a;
}
else if(a>b)
{
return God(a-b,b);
}
else
{
return God(a,b-a);
}
}
代码实现
#include
#include
int mid(int a, int b, int c);
int main()
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("The result is %d\n",mid(a,b,c));
return 0;
}
int mid(int a, int b, int c)
{
if(a>b && a<c)
{
return a;
}
else if(b>a && b<c)
{
return b;
}
else
{
return c;
}
}
代码实现
#include
#include
int main()
{
int x,y,z,d,n;
printf("Input n(n<1000):\n");
scanf("%d",&n);
for(x=1;x<=9;x++)
{
for(y=1;y<=9;y++)
{
for(z=1;z<=9;z++)
{
d=x*100+y*100+y*10+z*10+z*2;
if(d==n)
{
printf("X=%d,Y=%d,Z=%d\n",x,y,z);
goto END;
}
}
}
}
printf("Invalid\n");
END: ;
return 0;
}