PTA:浙⼤版《C语⾔程序设计(第3版)》题⽬集练习2-1 Programming in C is fun!
#include <stdio.h>
int main()
{
printf("Programming in C is fun!");
return 0;
}
复制代码
练习2-3 输出倒三⾓图案
#include <stdio.h>
int main()
{
printf("* * * *\n");
printf(" * * *\n");
printf("  * *\n");
printf("  *\n");
return 0;
}
复制代码
练习2-4 温度转换
#include <stdio.h>
int main()
{
int fahr,celsius;
fahr = 150;
celsius = 5 * (fahr - 32 ) / 9;
printf("fahr = %d, celsius = %d",fahr,celsius);
return 0;
}
复制代码
练习2-6 计算物体⾃由下落的距离(错误)
#include <stdio.h>
int main()
{
int t = 3;
c程序设计语言第几版最好printf("height = %lf",100-0.5*10*t*t);
return 0;
}
复制代码
练习2-8 计算摄⽒温度
int main(){
int far;
int cel;
scanf("%d",&far);
cel = 5 * ( far - 32)/9;
printf("Celsius = %d",cel);
return 0;
}
复制代码
练习2-9 整数四则运算
#include<stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d + %d = %d\n",a,b,a+b);  printf("%d - %d = %d\n",a,b,a-b);  printf("%d * %d = %d\n",a,b,a*b);  printf("%d / %d = %d\n",a,b,a/b);  return 0;
}
复制代码
练习2-10 计算分段函数[1]
#include<stdio.h>
int main(){
double x;
double f;
int type = 2;
scanf("%lf",&x);
if (x == 0) type = 0;
switch(type){
case 0:
f = 0;
break;
default:
f = 1/x;
break;
}
printf("f(%.1lf) = %.1lf",x,f);
return 0;
}
复制代码
练习2-11 计算分段函数[2]
#include<math.h>
int main(){
double x;
double f;
scanf("%lf",&x);
int type;
if (x >= 0 )
type = 1;
else type = 2;
switch(type){
case 1:
f = sqrt(x);
break;
case 2:
f = pow(x+1,2) + 2*x + 1/x;
break;
}
printf("f(%.2lf) = %.2lf",x,f);
return 0;
}
复制代码
练习2-12 输出华⽒-摄⽒温度转换表(错误)
#include<stdio.h>
int main(){
int lower,upper;
int fahr;
double cel;
scanf("%d %d",&lower,&upper);
if (lower <= upper){
fahr = lower;
printf("fahr celsius\n");
do{
cel = 5.0*(fahr-32)/9.0;
printf("%d  %6.1f\n",fahr,cel);
fahr +=2;
}while(fahr <= upper);
}
else
printf("Invalid.");
return 0;
}
复制代码
练习2-13 求N分之⼀序列前N项和(错误) #include<stdio.h>
int main(){
int n;
scanf("%d",&n);
double sum = 0.0;
int count = 1;
do{
sum = sum + 1/count;
count++;
}while(count <= n);
printf("sum = %.6lf",sum);
return 0;
}
复制代码
练习2-14 求奇数分之⼀序列前N项和(错误)#include<stdio.h>
int main(){
int n;
double sum = 0;
scanf("%d",&n);
do{
sum += 1/n;
n--;
}while(n > 0);
printf("%.6lf",sum);
return 0;
}
复制代码
练习2-15 求简单交错序列前N项和(错误)#include<stdio.h>
int main()
{
int n;
scanf("%d\n",&n);
double sum;
int i = 1;
int k = 1;
while(k < n){
sum += 1/i;
i += 3;
k++;
}
printf("sum = %.3f",sum);
return 0;
}
复制代码

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。