纯小白,用c写这道题
输入一个三位数,将其加密后输出。加密方法是:对该数的每一位数字将其加6后除以10取余数,作为该位上的新数字,再交换个位数字和百位数字。 本帖最后由 jackz007 于 2021-10-20 00:46 编辑#include <stdio.h>
int main(void)
{
int a , b , c , d , e ;
for(;;) {
printf("enter an integer : ") ;
scanf("%d" , & d) ;
if(d > 99 && d < 1000) break ;
else printf("\n") ;
}
/* 加密 */
a = ((d / 100) + 6) % 10 ;
b = (((d % 100) / 10) + 6) % 10 ;
c = ((d % 10) + 6) % 10 ;
e = c * 100 + b * 10 + a ;
printf("e = %d\n" , e) ;
/* 解密 */
a = (e / 100) >= 6 ? e / 100 - 6 : e / 100 + 10 - 6 ;
b = ((e % 100) / 10) >= 6 ? ((e % 100) / 10) - 6 : (e % 100) / 10 + 10 - 6 ;
c = (e % 10) >= 6 ? (e % 10) - 6 : (e % 10) + 10 - 6 ;
d = c * 100 + b * 10 + a ;
printf("d = %d\n" , d) ;
}
编译、运行实况:
D:\00.Excise\C>g++ -o x x.c
D:\00.Excise\C>x
enter an integer : 708
e = 463
d = 708
D:\00.Excise\C>x
enter an integer : 987
e = 345
d = 987
D:\00.Excise\C>x
enter an integer : 169
e = 527
d = 169
D:\00.Excise\C> jackz007 发表于 2021-10-20 00:36
编译、运行实况:
括号里那个void是啥意思?
ZXPoo 发表于 2021-10-20 07:53
括号里那个void是啥意思?
表示函数没有输入参数。 jackz007 发表于 2021-10-20 09:09
表示函数没有输入参数。
懂了,谢谢
ZXPoo 发表于 2021-10-20 09:34
懂了,谢谢
如果问题已经得到解决,别忘记设置为 "最佳答案" #include <stdio.h>
int encryption(int password)
{
int a = ((password/100)+6)%10;
int b = (((password%100)/10)+6)%10;
int c = ((password%10)+6)%10;
return c*100+b*10+a;
}
int main()
{
int password;
scanf("%d", &password);
printf("%d", encryption(password));
return 0;
}
页:
[1]