当前位置:   article > 正文

PTA函数部分20题全部答案_pta函数题答案

pta函数题答案

//6-1 使用函数计算两点间的距离

  1. #include <stdio.h>
  2. #include <math.h>
  3. double dist(double x1, double y1, double x2, double y2);
  4. int main()
  5. {
  6. double x1, y1, x2, y2;
  7. scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
  8. printf("dist = %.2f\n", dist(x1, y1, x2, y2));
  9. return 0;
  10. }

  1. double dist(double x1, double y1, double x2, double y2)
  2. {
  3. return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
  4. }

//6-2 符号函数2

  1. #include <stdio.h>
  2. int sign(int x);
  3. int main()
  4. {
  5. int x;
  6. scanf("%d", &x);
  7. printf("sign(%d) = %d\n", x, sign(x));
  8. return 0;
  9. }
  1. int sign(int x)
  2. {
  3. if (x == 0)
  4. return 0;
  5. return (x > 0) ? 1 : -1;
  6. }

//6-3 使用函数求Fibonacci数
 

  1. #include <stdio.h>
  2. int fib(int n);
  3. int main()
  4. {
  5. int n;
  6. scanf("%d", &n);
  7. printf("%d\n", fib(n));
  8. return 0;
  9. }

1.递归,但会超时

  1. Vint fib(int n)
  2. {
  3. if (n == 1 || n == 2)
  4. return 1;
  5. else
  6. return fib(n - 1) + fib(n - 2);
  7. }

2.一般方法

  1. int fib(int n)
  2. {
  3. if (n == 1 || n == 2)
  4. return 1;
  5. int a = 1, b = 1;
  6. int tmp = 0;
  7. for (int i = 3; i <= n; i++)
  8. {
  9. tmp = b;
  10. b = a + b;
  11. a = tmp;
  12. }
  13. return b;
  14. }

//6-4 输出每个月的天数
 

  1. #include <stdio.h>
  2. int MonthDays(int year, int month);
  3. int main()
  4. {
  5. int ndays, month, year;
  6. scanf("%d", &year);
  7. for (month = 1; month <= 12; month++) {
  8. ndays = MonthDays(year, month);
  9. printf("%d ", ndays);
  10. }
  11. return 0;
  12. }
  1. int MonthDays(int year, int month)
  2. {
  3. int flag = 1;
  4. if (year % 4 == 0 && year % 100 != 0)
  5. flag = 0;
  6. if (year % 400 == 0)
  7. flag = 0;
  8. switch (month)
  9. {
  10. case 1:return 31;
  11. case 2:if (flag) return 28; else return 29;
  12. case 3:return 31;
  13. case 4:return 30;
  14. case 5:return 31;
  15. case 6:return 30;
  16. case 7:return 31;
  17. case 8:return 31;
  18. case 9:return 30;
  19. case 10:return 31;
  20. case 11:return 30;
  21. case 12:return 31;
  22. }
  23. }

//6-5 使用函数求素数和
 

  1. #include <stdio.h>
  2. #include <math.h>
  3. int prime(int p);
  4. int PrimeSum(int m, int n);
  5. int main()
  6. {
  7. int m, n, p;
  8. scanf("%d %d", &m, &n);
  9. printf("Sum of ( ");
  10. for (p = m; p <= n; p++) {
  11. if (prime(p) != 0)
  12. printf("%d ", p);
  13. }
  14. printf(") = %d\n", PrimeSum(m, n));
  15. return 0;
  16. }

1.

  1. int prime(int n)
  2. {
  3. int flag = 1;
  4. if (n <= 1)
  5. return 0;
  6. for (int i = 2; i < n; i++)
  7. {
  8. if (n % i == 0)
  9. {
  10. flag = 0;
  11. break;
  12. }
  13. }
  14. if (flag == 1)
  15. return n;
  16. else
  17. return 0;
  18. }

2.

  1. int PrimeSum(int m, int n)
  2. {
  3. int sum = 0;
  4. for (int ret = 0, j = m; j <= n; j++)
  5. {
  6. ret = prime(j);
  7. if (ret)
  8. {
  9. sum += ret;
  10. }
  11. }
  12. return sum;
  13. }

//6-6 字符金字塔

  1. #include <stdio.h>
  2. void CharPyramid(int n, char ch);
  3. int main()
  4. {
  5. int n;
  6. char ch;
  7. scanf("%d %c", &n, &ch);
  8. CharPyramid(n, ch);
  9. return 0;
  10. }
  1. void CharPyramid(int n, char ch) {
  2. int i, j, k;
  3. for (i = 1; i <= n; i++) {
  4. for (j = 1; j <= n - i; j++) {
  5. printf(" ");
  6. }for (k = 1; k <= i; k++) {
  7. printf("%c ", ch);
  8. }printf("\n");
  9. }
  10. }

//6-7 使用函数计算两个复数之积
 

  1. #include<stdio.h>
  2. double result_real, result_imag;
  3. void complex_prod(double x1, double y1, double x2, double y2);
  4. int main(void)
  5. {
  6. double imag1, imag2, real1, real2;
  7. scanf("%lf %lf", &real1, &imag1);
  8. scanf("%lf %lf", &real2, &imag2);
  9. complex_prod(real1, imag1, real2, imag2);
  10. printf("product of complex is (%f)+(%f)i\n", result_real, result_imag);
  11. return 0;
  12. }

  1. void complex_prod(double x1, double y1, double x2, double y2)
  2. {
  3. result_real = (x1 * x2 - y1 * y2);
  4. result_imag = (x1 * y2 + x2 * y1);
  5. }

//6-8 使用函数求余弦函数的近似值
 

//思路一定要清晰
 

  1. #include <stdio.h>
  2. #include <math.h>
  3. double funcos(double e, double x);
  4. int main()
  5. {
  6. double e, x;
  7. scanf("%lf %lf", &e, &x);
  8. printf("cos(%.2f) = %.6f\n", x, funcos(e, x));
  9. return 0;
  10. }

  1. double funcos(double e, double x)
  2. {
  3. double res = 1, nn = 2,tmp=e,sum=1.0,p=1.0;
  4. int flag = -1;
  5. double i = 0;
  6. while (tmp >= e)
  7. {
  8. for (i = nn-1; i <= nn; i++)
  9. {
  10. p *= x;
  11. res *= i;
  12. }
  13. nn += 2;
  14. tmp = p / res;
  15. sum = sum + flag * tmp;
  16. flag = -flag;
  17. }
  18. return sum;
  19. }

//6-9 使用函数统计指定数字的个数

  1. #include <stdio.h>
  2. int CountDigit(int number, int digit);
  3. int main()
  4. {
  5. int number, digit;
  6. scanf("%d %d", &number, &digit);
  7. printf("Number of digit %d in %d: %d\n", digit, number, CountDigit(number, digit));
  8. return 0;
  9. }
  1. int CountDigit(int number, int digit)
  2. {
  3. int pows = 0, n = number;
  4. if (n < 0)
  5. n = -n;
  6. else if (n == 0 && digit == 0)
  7. return 1;
  8. while (n > 0)
  9. {
  10. if (n % 10 == digit)
  11. pows++;
  12. n /= 10;
  13. }
  14. return pows;
  15. }

//6-10 空心的数字金字塔
 

  1. #include <stdio.h>
  2. void hollowPyramid(int n);
  3. int main()
  4. {
  5. int n;
  6. scanf("%d", &n);
  7. hollowPyramid(n);
  8. return 0;
  9. }
  1. void hollowPyramid(int n)
  2. {
  3. for (int i = 0; i < n-1; i++)
  4. {
  5. for (int j = n-i-1; j > 0; j--)
  6. {
  7. printf(" ");
  8. }
  9. printf("%d", i+1);
  10. for (int k = 2 * i - 1; k >= 1; k--)
  11. {
  12. printf(" ");
  13. }
  14. if (i)
  15. {
  16. printf("%d", i+1);
  17. }
  18. printf("\n");
  19. }
  20. for (int i = 0; i < 2 * n - 1; i++)
  21. {
  22. printf("%d", n);
  23. }
  24. }

//6-11 使用递归函数计算1到n之和
 

  1. #include <stdio.h>
  2. int sum(int n);
  3. int main()
  4. {
  5. int n;
  6. scanf("%d", &n);
  7. printf("%d\n", sum(n));
  8. return 0;
  9. }
  1. int sum(int n)
  2. {
  3. if (n > 0)
  4. return n + sum(n - 1);
  5. else
  6. return 0;
  7. }

//6-12 递归实现指数函数
 

  1. #include <stdio.h>
  2. double calc_pow(double x, int n);
  3. int main()
  4. {
  5. double x;
  6. int n;
  7. scanf("%lf %d", &x, &n);
  8. printf("%.0f\n", calc_pow(x, n));
  9. return 0;
  10. }
  1. double calc_pow(double x, int n)
  2. {
  3. if (n > 1)
  4. return x * calc_pow(x, n - 1);
  5. else
  6. return x;
  7. }

//6-13 递归计算Ackermenn函数
 

  1. #include <stdio.h>
  2. int Ack(int m, int n);
  3. int main()
  4. {
  5. int m, n;
  6. scanf("%d %d", &m, &n);
  7. printf("%d\n", Ack(m, n));
  8. return 0;
  9. }
  1. int Ack(int m, int n)
  2. {
  3. if (!m)
  4. return n + 1;
  5. else if (n == 0 && m > 0)
  6. return Ack(m - 1, 1);
  7. else if (m > 0 && n > 0)
  8. return Ack(m - 1, Ack(m, n - 1));
  9. }

//6-14 递归求Fabonacci数列
 

  1. #include <stdio.h>
  2. int f(int n);
  3. int main()
  4. {
  5. int n;
  6. scanf("%d", &n);
  7. printf("%d\n", f(n));
  8. return 0;
  9. }
  1. int f(int n)
  2. {
  3. if (n == 0)
  4. return 0;
  5. if (n<=2&&n>0)
  6. return 1;
  7. else
  8. return f(n - 1) + f(n - 2);
  9. }

//6 - 15 递归求简单交错幂级数的部分和

  1. double fn(double x, int n);
  2. int main()
  3. {
  4. double x;
  5. int n;
  6. scanf("%lf %d", &x, &n);
  7. printf("%.2f\n", fn(x, n));
  8. return 0;
  9. }
  1. double fn(double x, int n)
  2. {
  3. if (n == 1)
  4. return x;
  5. else
  6. return -x * fn(x, n - 1)+x;
  7. }

//6-16 递归实现顺序输出整数

  1. void printdigits(int n);
  2. int main()
  3. {
  4. int n;
  5. scanf("%d", &n);
  6. printdigits(n);
  7. return 0;
  8. }
  1. void printdigits(int n)
  2. {
  3. int k = 1;
  4. if (n > 9)
  5. {
  6. k = n % 10;
  7. printdigits(n / 10);
  8. printf("%d\n", k);
  9. }
  10. else
  11. printf("%d\n", n);
  12. }

//6-17 判断满足条件的三位数

  1. #include <stdio.h>
  2. #include <math.h>
  3. int search(int n);
  4. int main()
  5. {
  6. int number;
  7. scanf("%d", &number);
  8. printf("count=%d\n", search(number));
  9. return 0;
  10. }

1.普通方法

  1. int search(int n)
  2. {
  3. int i, cnt = 0;
  4. for (i = 101; i <= n; i++) {
  5. double digit = sqrt(i);
  6. int a = i / 100;
  7. int b = i / 10 % 10;
  8. int c = i % 10;
  9. if ((digit == (int)digit) && (a == b || a == c || b == c))
  10. cnt++;
  11. }
  12. return cnt;
  13. }

 2.递归!!

  1. int search(int n)
  2. {
  3. if (n > 100 && (sqrt(n) ==(int) sqrt(n))&&(n/100==n%10||n/100==n/10%10||n%10==n/10%10))
  4. return 1 + search(n - 1);
  5. else if (n > 100)
  6. return 0 + search(n - 1);
  7. else
  8. return 0;
  9. }
  10. //错误点,else if中的定义错误了
  11. //******************************
  12. //** (n>100&&sqrt(n)!=(int)sqrt(n))
  13. ///开什么玩笑这是判断平方数的,不是题目中的返li

//6-18 递归求逆序数
 

  1. #include <stdio.h>
  2. # include <math.h>
  3. unsigned int reverse(unsigned int number);
  4. int main()
  5. {
  6. unsigned int n;
  7. scanf("%u", &n);
  8. printf("%u\n", reverse(n));
  9. return 0;
  10. }
  1. 失败品
  2. //unsigned int reverse(unsigned int number)
  3. //{
  4. // //static int k = 1 ;
  5. //
  6. // double p = 0.1;
  7. // while (m > 9)
  8. // {
  9. // k *= 10;
  10. // m /= 10;
  11. // }
  12. // if (number > 9)
  13. // {
  14. // p *= 10;
  15. // return number / k * (int)p + reverse(number-k*number);
  16. // }
  17. // else
  18. // return m;
  19. //}
  20. unsigned int reverse(unsigned int number)
  21. {
  22. int po = 0, res = number;
  23. while (res) res /= 10, po++;
  24. if (number < 10) return number;
  25. return reverse(number / 10) + (number % 10) * pow(10, po - 1);
  26. }

//6-19 十进制转换二进制
 

  1. #include <stdio.h>
  2. void dectobin(int n);
  3. int main()
  4. {
  5. int n;
  6. scanf("%d", &n);
  7. dectobin(n);
  8. return 0;
  9. }
  1. void dectobin(int n)
  2. {
  3. if (n > 1)
  4. {
  5. dectobin(n / 2);
  6. printf("%d", n % 2);
  7. }
  8. else
  9. printf("%d", n);
  10. }

//6-20 数组循环右移
 

  1. #include <stdio.h>
  2. #define MAXN 10
  3. void ArrayShift(int a[], int n, int m);
  4. int main()
  5. {
  6. int a[MAXN], n, m;
  7. int i;
  8. scanf("%d %d", &n, &m);
  9. for (i = 0; i < n; i++) scanf("%d", &a[i]);
  10. ArrayShift(a, n, m);
  11. for (i = 0; i < n; i++) {
  12. if (i != 0) printf(" ");
  13. printf("%d", a[i]);
  14. }
  15. printf("\n");
  16. return 0;
  17. }
  1. void ArrayShift(int a[], int n, int m) {
  2. while (m--)
  3. {
  4. for (int i = n-1; i > 0 ; i--)
  5. {
  6. int tmp = a[i];
  7. a[i] = a[i -1];
  8. a[i -1] = tmp;
  9. }
  10. }
  11. }

终于结束了,继续加油哦

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/935797
推荐阅读
相关标签
  

闽ICP备14008679号