鱼C论坛

 找回密码
 立即注册
查看: 1358|回复: 5

[已解决]素数输出后边的空格要怎么删除?

[复制链接]
发表于 2019-12-21 00:42:32 | 显示全部楼层 |阅读模式
5鱼币
写了好多题输出都不知道要咋消空格,问了几种方法有时候也使不上,大佬帮忙解决的时候能否顺便说下思路
代码是下边这个100到200之间的素数,我想要最后的输出结果101后边没有空格,最前边也不能有  麻烦了!
  1. #include <stdio.h>

  2. int main()
  3. {
  4.         int i,n;
  5.         for(n=200;n>=100;n--)
  6.         {
  7.                 for(i=2;i<=n;i++)
  8.                 {
  9.                         if(n%i==0)
  10.                         break;
  11.                 }
  12.                 if(i==n)
  13.                 printf("%d ",n);
  14.         }
  15.         return 0;
  16. }
复制代码
最佳答案
2019-12-21 00:42:33
  1. #include <stdio.h>

  2. void output(int arr[], size_t height, size_t width)
  3. {
  4.         static char separator[2] = {'\0', '\0'};
  5.         for(size_t y = 0; y < height; ++y)
  6.         {
  7.                 for(size_t x = 0; x < width; ++x)
  8.                 {
  9.                         printf("%s%d", separator, arr[y * width + x]);
  10.                         separator[0] = ' ';
  11.                 }
  12.                 separator[0] = '\0';
  13.                 printf("\n");
  14.         }
  15. }

  16. int main(void)
  17. {
  18.         int arr[3][5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}};
  19.         output((int *)arr, 3, 5);
  20.         return 0;
  21. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-12-21 00:42:33 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>

  2. void output(int arr[], size_t height, size_t width)
  3. {
  4.         static char separator[2] = {'\0', '\0'};
  5.         for(size_t y = 0; y < height; ++y)
  6.         {
  7.                 for(size_t x = 0; x < width; ++x)
  8.                 {
  9.                         printf("%s%d", separator, arr[y * width + x]);
  10.                         separator[0] = ' ';
  11.                 }
  12.                 separator[0] = '\0';
  13.                 printf("\n");
  14.         }
  15. }

  16. int main(void)
  17. {
  18.         int arr[3][5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}};
  19.         output((int *)arr, 3, 5);
  20.         return 0;
  21. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-12-21 09:34:40 | 显示全部楼层
printf("%d ",n);把输出语句的空格去掉 ,改为printf("%d",n);
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-12-21 12:21:49 | 显示全部楼层
为什么要删呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-12-21 14:12:44 | 显示全部楼层
很久之前遇到过类似的问题,因为太过久远,已经不记得出处了
大体上就是上面那个样子
我隐约记得之前的那个问题是添加逗号,要求就是尾部不能有,开头不能有,和这个问题很类似
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-12-21 14:13:56 | 显示全部楼层
  1. #include <stdio.h>

  2. void output(int arr[], size_t height, size_t width)
  3. {
  4.         static char separator[2] = {'\0', '\0'};
  5.         for(size_t y = 0; y < height; ++y)
  6.         {
  7.                 for(size_t x = 0; x < width; ++x)
  8.                 {
  9.                         printf("%s%d", separator, arr[y * width + x]);
  10.                         separator[0] = ',';
  11.                 }
  12.                 separator[0] = '\0';
  13.                 printf("\n");
  14.         }
  15. }

  16. int main(void)
  17. {
  18.         int arr[3][5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}};
  19.         output((int *)arr, 3, 5);
  20.         return 0;
  21. }
复制代码

  1. 1,2,3,4,5
  2. 6,7,8,9,10
  3. 11,12,13,14,15
  4. 请按任意键继续. . .
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-11 21:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表