c语言新手帮帮忙
这个按位逆序怎么搞{:10_266:} #include <stdio.h>int main(void)
{
int a , b , c ;
printf("enter a integer : ") ;
scanf("%d" , & a) ;
for(b = 0 , c = a ; c ; c /= 10) b = b * 10 + c % 10 ;
printf("%d\n" , b) ;
}
编译、运行实况:
D:\00.Excise\C>g++ -o x x.c
D:\00.Excise\C>x
enter a integer : 123456
654321
D:\00.Excise\C>x
enter a integer : -123456
-654321
D:\00.Excise\C>x
#include<stdio.h>
int main(void)
{
int num;
int a, b, c;
printf("输入一个3位数:");
scanf("%d", &num);
a = num / 100;
b = num % 100 / 10;
c = num %10;
if (c != 0) {
printf("%d", c);
}
if (b != 0) {
printf("%d", b);
}
if (a != 0) {
printf("%d", a);
} else {
printf("输入的3位数有误!");
}
return 0;
}
jackz007 发表于 2021-10-16 22:48
编译、运行实况:
请问还有没有不用for的方法呢,基础点的{:10_254:} #include <stdio.h>
#include <string.h>
void reverse_number(size_t num, char *buff) {
sprintf(buff, "%lu", num);
int end = strlen(buff) - 1;
while(buff == '0') --end;
buff = '\0';
for(int start = 0; start < end; ++start, --end) {
char temp = buff;
buff = buff;
buff = temp;
}
}
int main(void) {
char buff;
reverse_number(1000, buff); puts(buff);
reverse_number(1200, buff); puts(buff);
reverse_number(1230, buff); puts(buff);
reverse_number(1234, buff); puts(buff);
reverse_number(1004, buff); puts(buff);
reverse_number(1030, buff); puts(buff);
reverse_number(103, buff); puts(buff);
return 0;
}
Boring1031 发表于 2021-10-16 22:55
请问还有没有不用for的方法呢,基础点的
不使用 for
#include <stdio.h>
#include <string.h>
void reverse_number(size_t num, char *buff) {
sprintf(buff, "%lu", num);
int end = strlen(buff) - 1;
while(buff == '0') --end;
buff = '\0';
int start = 0;
start_loop:
{
if(start >= end) goto end_loop;
char temp = buff;
buff = buff;
buff = temp;
++start, --end;
goto start_loop;
}
end_loop:
;
}
int main(void) {
char buff;
reverse_number(1000, buff); puts(buff);
reverse_number(1200, buff); puts(buff);
reverse_number(1230, buff); puts(buff);
reverse_number(1234, buff); puts(buff);
reverse_number(1004, buff); puts(buff);
reverse_number(1030, buff); puts(buff);
reverse_number(103, buff); puts(buff);
return 0;
}
人造人 发表于 2021-10-16 23:01
不使用 for
我刚学的作业应该没那么复杂吧{:10_266:} Boring1031 发表于 2021-10-16 23:04
我刚学的作业应该没那么复杂吧
不复杂吧?^_^ #include <stdio.h>
int main(void) {
int a, b, c, x;
scanf("%d", &x);
c = x % 10;
b = x / 10 % 10;
a = x / 100 % 10;
if(c != 0) printf("%d", c);
if(b != 0) printf("%d", b);
else if(c != 0) printf("%d", b);
printf("%d", a);
printf("\n");
return 0;
}
这样能看懂不?
#include <stdio.h>
int main(void)
{
int a, b, c, x;
scanf("%d", &x);
c = x % 10;
b = x / 10 % 10;
a = x / 100 % 10;
if(c != 0)
{
printf("%d", c);
}
if(b != 0)
{
printf("%d", b);
} else if(c != 0)
{
printf("%d", b);
}
printf("%d", a);
printf("\n");
return 0;
}
人造人 发表于 2021-10-16 23:13
这样能看懂不?
可以了可以了,谢谢大佬{:10_323:}
页:
[1]