鱼C论坛

 找回密码
 立即注册
查看: 1815|回复: 10

[已解决]c语言新手帮帮忙

[复制链接]
发表于 2021-10-16 22:21:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

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

x
这个按位逆序怎么搞
最佳答案
2021-10-16 22:48:10
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         int a , b , c                                        ;
  5.         printf("enter a integer : ")                         ;
  6.         scanf("%d" , & a)                                    ;
  7.         for(b = 0 , c = a ; c ; c /= 10) b = b * 10 + c % 10 ;
  8.         printf("%d\n" , b)                                   ;
  9. }
复制代码

        编译、运行实况:
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. enter a integer : 123456
  4. 654321

  5. D:\00.Excise\C>x
  6. enter a integer : -123456
  7. -654321

  8. D:\00.Excise\C>x
复制代码
Screenshot_2021_1016_221811.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-10-16 22:48:10 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.         int a , b , c                                        ;
  5.         printf("enter a integer : ")                         ;
  6.         scanf("%d" , & a)                                    ;
  7.         for(b = 0 , c = a ; c ; c /= 10) b = b * 10 + c % 10 ;
  8.         printf("%d\n" , b)                                   ;
  9. }
复制代码

        编译、运行实况:
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. enter a integer : 123456
  4. 654321

  5. D:\00.Excise\C>x
  6. enter a integer : -123456
  7. -654321

  8. D:\00.Excise\C>x
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-16 22:49:23 | 显示全部楼层

  1. #include<stdio.h>

  2. int main(void)
  3. {
  4.     int num;
  5.     int a, b, c;
  6.     printf("输入一个3位数:");
  7.     scanf("%d", &num);

  8.     a = num / 100;
  9.     b = num % 100 / 10;
  10.     c = num %10;

  11.     if (c != 0) {

  12.         printf("%d", c);
  13.     }
  14.     if (b != 0) {

  15.         printf("%d", b);
  16.     }
  17.     if (a != 0) {

  18.         printf("%d", a);
  19.     } else {

  20.         printf("输入的3位数有误!");
  21.     }

  22.     return 0;
  23. }


复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-16 22:55:03 | 显示全部楼层
jackz007 发表于 2021-10-16 22:48
编译、运行实况:

请问还有没有不用for的方法呢,基础点的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-16 22:57:00 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <string.h>

  3. void reverse_number(size_t num, char *buff) {
  4.     sprintf(buff, "%lu", num);
  5.     int end = strlen(buff) - 1;
  6.     while(buff[end] == '0') --end;
  7.     buff[end + 1] = '\0';
  8.     for(int start = 0; start < end; ++start, --end) {
  9.         char temp = buff[start];
  10.         buff[start] = buff[end];
  11.         buff[end] = temp;
  12.     }
  13. }

  14. int main(void) {
  15.     char buff[1024];
  16.     reverse_number(1000, buff); puts(buff);
  17.     reverse_number(1200, buff); puts(buff);
  18.     reverse_number(1230, buff); puts(buff);
  19.     reverse_number(1234, buff); puts(buff);
  20.     reverse_number(1004, buff); puts(buff);
  21.     reverse_number(1030, buff); puts(buff);
  22.     reverse_number(103, buff); puts(buff);
  23.     return 0;
  24. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-16 23:01:49 | 显示全部楼层
Boring1031 发表于 2021-10-16 22:55
请问还有没有不用for的方法呢,基础点的

不使用 for
  1. #include <stdio.h>
  2. #include <string.h>

  3. void reverse_number(size_t num, char *buff) {
  4.     sprintf(buff, "%lu", num);
  5.     int end = strlen(buff) - 1;
  6.     while(buff[end] == '0') --end;
  7.     buff[end + 1] = '\0';
  8.     int start = 0;
  9. start_loop:
  10.     {
  11.         if(start >= end) goto end_loop;

  12.         char temp = buff[start];
  13.         buff[start] = buff[end];
  14.         buff[end] = temp;

  15.         ++start, --end;
  16.         goto start_loop;
  17.     }
  18. end_loop:
  19.     ;
  20. }

  21. int main(void) {
  22.     char buff[1024];
  23.     reverse_number(1000, buff); puts(buff);
  24.     reverse_number(1200, buff); puts(buff);
  25.     reverse_number(1230, buff); puts(buff);
  26.     reverse_number(1234, buff); puts(buff);
  27.     reverse_number(1004, buff); puts(buff);
  28.     reverse_number(1030, buff); puts(buff);
  29.     reverse_number(103, buff); puts(buff);
  30.     return 0;
  31. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-16 23:04:49 | 显示全部楼层

我刚学的作业应该没那么复杂吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-16 23:05:51 | 显示全部楼层
Boring1031 发表于 2021-10-16 23:04
我刚学的作业应该没那么复杂吧

不复杂吧?^_^
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-16 23:11:30 | 显示全部楼层
  1. #include <stdio.h>

  2. int main(void) {
  3.     int a, b, c, x;
  4.     scanf("%d", &x);
  5.     c = x % 10;
  6.     b = x / 10 % 10;
  7.     a = x / 100 % 10;
  8.     if(c != 0) printf("%d", c);
  9.     if(b != 0) printf("%d", b);
  10.     else if(c != 0) printf("%d", b);
  11.     printf("%d", a);
  12.     printf("\n");
  13.     return 0;
  14. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-16 23:13:16 | 显示全部楼层
这样能看懂不?
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.     int a, b, c, x;
  5.     scanf("%d", &x);
  6.     c = x % 10;
  7.     b = x / 10 % 10;
  8.     a = x / 100 % 10;
  9.     if(c != 0)
  10.     {
  11.         printf("%d", c);
  12.     }
  13.     if(b != 0)
  14.     {
  15.         printf("%d", b);
  16.     } else if(c != 0)
  17.     {
  18.         printf("%d", b);
  19.     }
  20.     printf("%d", a);
  21.     printf("\n");
  22.     return 0;
  23. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-16 23:17:20 | 显示全部楼层

可以了可以了,谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 02:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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