[C.C++] 20201022-成信大-C语言程序设计-20201学期《C语言程序设计B》C-trainingExercises10

914 0
Honkers 2025-3-25 03:55:18 | 显示全部楼层 |阅读模式

20201022-成信大-C语言程序设计-20201学期《C语言程序设计B》C-trainingExercises10

P125

  1. /*
  2.   编写一程序P125.C实现以下功能
  3.   从键盘读入两个字符cBegin和cEnd,要求输出≤cBegin且≥cEnd的所有字符。
  4. 编程可用素材:
  5. printf("Please Input two char: ");
  6. printf("\nResult: ");
  7.   程序的运行效果应类似地如图1所示,图1中的MA是从键盘输入的内容。
  8. Please Input two char: MA
  9. Result: MLKJIHGFEDCBA
  10. 知识点:
  11. 1. 循环起点cBegin
  12. 2. 循环终点cEnd
  13. 3. 循环趋势:越来越小
  14. */
  15. #include <stdio.h>
  16. int main(void)
  17. {
  18. char cBegin, cEnd;
  19. int i;
  20. printf("Please Input two char: ");
  21. scanf("%c%c", &cBegin, &cEnd);
  22. printf("\nResult: ");
  23. for (i = (int)cBegin; i >= (int)cEnd; i--)
  24. {
  25. printf("%c", (char)i);
  26. }
  27. return 0;
  28. }
复制代码

P127

  1. /*
  2.   编写一程序P127.C实现以下功能
  3.   从键盘读入一个字符cBegin和一个数iCount,要求输出≤cBegin的iCount个字符。
  4. 编程可用素材:
  5. printf("Please Input a char and a number: ");
  6. printf("\nResult: ");
  7.   程序的运行效果应类似地如图1所示,图1中的M 9是从键盘输入的内容。
  8. Please Input a char and a number: M 9
  9. Result: MLKJIHGFE
  10. */
  11. #include <stdio.h>
  12. int main(void)
  13. {
  14. char cBegin;
  15. int iCount, i;
  16. printf("Please Input a char and a number: ");
  17. scanf("%c %i", &cBegin, &iCount);
  18. printf("\nResult: ");
  19. // 要特别注意这里的循环
  20. /*
  21. 起点:cBegin,包括端点
  22. 终点:用输入的计数来计算终点iCount个
  23. 步长:1
  24. char类型本质上就是int型,也可以不用强转,可以自动转化的
  25. */
  26. for (i = (int)cBegin; i > (int)cBegin - iCount; i--)
  27. {
  28. printf("%c", (char)i);
  29. }
  30. return 0;
  31. }
复制代码

P232

  1. /*
  2.   编写一程序P232.C实现以下功能
  3.   从键盘读入一个整数Num,按从小到大的顺序依次输出所有满足条件的3位数:“该数各位数字的平方和”再加上“该数/2”等于Num。
  4. 编程可用素材:
  5. printf("Please Input a number: ");
  6. printf("\nResult: ");
  7. printf("%5d"…);
  8. printf("not Find!\n");
  9.   程序的运行效果应类似地如图1和图2所示,图1中的293和图2中的199是从键盘输入的内容。
  10. Please Input a number: 293
  11. Result: 289 368 455 504
  12. 图1 程序运行效果示例
  13. Please Input a number: 199
  14. Result: not Find!
  15. */
  16. #include <stdio.h>
  17. int main(void)
  18. {
  19. int data, gw, sw, bw;
  20. int i, count = 0;
  21. printf("Please Input a number: ");
  22. scanf("%d", &data);
  23. printf("\nResult: ");
  24. for (i = 100; i <= 999; i++)
  25. {
  26. /*
  27. 分离出各位上的数字
  28. 使用整数的除法和求余算法
  29. */
  30. gw = i % 10;
  31. sw = i / 10 % 10;
  32. bw = i / 100;
  33. // “该数各位数字的平方和”再加上“该数/2”等于Num
  34. if (i / 2 + gw * gw + sw * sw + bw * bw == data)
  35. {
  36. count++;
  37. printf(" %5d", i); // 注意输出格式宽度控制
  38. }
  39. }
  40. if (count == 0)
  41. {
  42. printf("not Find!\n");
  43. }
  44. return 0;
  45. }
复制代码

P753

  1. /*
  2.   编写一程序P753.C实现以下功能
  3.   计算X的Y次方,其中Y为整数(可以是负整数或0),X为实型。
  4. 注意,程序中不能使用库函数pow或使用同名的变量、函数、单词。
  5. 编程可用素材:
  6. printf("Input x, y: ");
  7. printf("\nResult: …^…=…);
  8.   程序的运行效果应类似地如图1和图2所示,图1中的3.7,5和图2中的4,-2是从键盘输入的内容。
  9. Input x, y: 3.7,5
  10. Result: 3.700000^5=693.439570
  11. 图1 程序运行效果示例
  12. Input x, y: 4,-2
  13. Result: 4.000000^-2=0.062500
  14. 看注释部分,可以看出,重构一下程序,程序看起来更简单,符合代码重用思想
  15. */
  16. #include <stdio.h>
  17. int main(void)
  18. {
  19. double datax, result = 1.0;
  20. int datay, i;
  21. printf("Input x, y: ");
  22. scanf("%lf,%d", &datax, &datay);
  23. if (datay == 0)
  24. {
  25. // 不考虑datax也为0的情况
  26. ;
  27. }
  28. else if (datay > 0)
  29. {
  30. for (i = 0; i < datay; i++)
  31. result = result * datax;
  32. // printf("\nResult: %lf^%d=%lf", datax, datay, result);
  33. }
  34. else
  35. {
  36. for (i = 0; i < (-1)*datay; i++)
  37. result = result / datax;
  38. // printf("\nResult: %lf^%d=%lf", datax, datay, result);
  39. }
  40. printf("\nResult: %lf^%d=%lf", datax, datay, result);
  41. return 0;
  42. }
复制代码

P716

  1. /*
  2.   编写一程序P716.C实现以下功能
  3.   求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字(可取1~9之间的一个值)。
  4. 例如2+22+222+2222+22222(此时共有5个数相加),其中a值和有几个数相加由键盘输入控制。
  5. 注意s的值有可能超出int的范围,
  6. 编程可用素材:
  7. printf("Please input a,n: ");
  8. printf("\na+aa+...=…);
  9.   程序的运行效果应类似地如图1所示,图1中的2,3是从键盘输入的内容。
  10. Please input a,n: 2,3
  11. a+aa+...=246
  12. 提示:
  13. 1. 这样的题,自己做完后测试,一定要主动测试一下自己创造的数据,看结果符不符合预期
  14. 2. 首先题提供的测试数据要能通过
  15. 3. 变化后的数据,可以检验算法的通用性
  16. */
  17. #include <stdio.h>
  18. int main(void)
  19. {
  20. int dataa, count, i;
  21. double aaa;
  22. double result = 0;
  23. printf("Please input a,n: ");
  24. scanf("%d,%d", &dataa, &count);
  25. aaa = dataa;
  26. /*
  27. 注意循环的范围 ,特别是终点
  28. */
  29. for (i = 0; i < count; i++)
  30. {
  31. result = result + aaa; // 累加还有一种常用的写法 result += aaa;
  32. aaa = aaa * 10 + dataa; // 构造a aa aaa ... 数据
  33. }
  34. printf("\na+aa+...=%.0lf", result);
  35. return 0;
  36. }
复制代码

P724

  1. /*
  2.   编写一程序P724.C实现以下功能
  3.   从键盘输入1~9之间的一个数,根据输入的数,输出对应的下三角乘法口诀表。
  4. 要求积的输出占3个宽度,且左对齐。
  5. 编程可用素材:
  6. printf("input a number(1~9): ");
  7.   程序的运行效果应类似地如图1和图2所示,图1中的3和图2中的7是从键盘输入的内容。
  8. input a number(1~9): 3
  9. 1*1=1
  10. 2*1=2 2*2=4
  11. 3*1=3 3*2=6 3*3=9
  12. 图1 程序运行效果示例
  13. input a number(1~9): 7
  14. 1*1=1
  15. 2*1=2 2*2=4
  16. 3*1=3 3*2=6 3*3=9
  17. 4*1=4 4*2=8 4*3=12 4*4=16
  18. 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
  19. 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
  20. 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
  21. 提示
  22. 1. 首先要弄清楚行的控制
  23. 2. 再弄清楚列的控制
  24. 3. 常用的双重循环的行列的控制,特别是里面一层循环的起点和终点
  25. */
  26. #include <stdio.h>
  27. int main(void)
  28. {
  29. int i, j, data;
  30. printf("input a number(1~9): ");
  31. scanf("%d", &data);
  32. for (i = 1; i <= data; i++)
  33. {
  34. for (j = 1; j <= i; j++)
  35. {
  36. printf("%d*%d=%-3d", i, j, i * j);
  37. }
  38. printf("\n");
  39. }
  40. return 0;
  41. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4010
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行