數字加密(可手動輸入或以亂數取得四位數),將每個數字加7後除以10取餘數,然後第一個和第三個數字調換,第二個和第四個數字對調。印出加密過的數,及加密後解密回原數字。
#include
int main(void) {
int a,b,c,d,t;
srand((unsigned)time(NULL));
int num =1000+rand()%9000;
//scanf("%d:",&num);這裡可以改成手動輸入四位數
printf("%d 加密後為:",num);
a = num/1000;
b = (num-(a*1000))/100;
c = (num-a*1000-b*100)/10;
d = (num-a*1000-b*100-c*10);
//printf("分解數字%d,%d,%d,%d\n",a,b,c,d);
a = (a+7)%10;
b = (b+7)%10;
c = (c+7)%10;
d = (d+7)%10;
//printf("運算後%d,%d,%d,%d\n",a,b,c,d);
t=a;
a=c;
c=t;
t=b;
b=d;
d=t;
//printf("調換後%d,%d,%d,%d\n",a,b,c,d);
t=a*1000+b*100+c*10+d;
printf("%d\n",t);
int main(void) {
int a,b,c,d,t;
srand((unsigned)time(NULL));
int num =1000+rand()%9000;
//scanf("%d:",&num);這裡可以改成手動輸入四位數
printf("%d
a = num/1000;
b = (num-(a*1000))/100;
c = (num-a*1000-b*100)/10;
d = (num-a*1000-b*100-c*10);
//printf("分解數字%d,%d,%d,%d\n",a,b,c,d);
a = (a+7)%10;
b = (b+7)%10;
c = (c+7)%10;
d = (d+7)%10;
//printf("運算後%d,%d,%d,%d\n",a,b,c,d);
t=a;
a=c;
c=t;
t=b;
b=d;
d=t;
//printf("調換後%d,%d,%d,%d\n",a,b,c,d);
t=a*1000+b*100+c*10+d;
printf("%d\n",t);
//下面這段為解密
printf("%d解密後為:",t);
a = t/1000;
b = (t-(a*1000))/100;
c = (t-a*1000-b*100)/10;
d = (t-a*1000-b*100-c*10);
//printf("分解數字%d,%d,%d,%d\n",a,b,c,d);
num=a;
a=c;
c=num;
num=b;
b=d;
d=num;
//printf("調換後%d,%d,%d,%d\n",a,b,c,d);
a = (a+3)%10;
b = (b+3)%10;
c = (c+3)%10;
d = (d+3)%10;
//printf("運算後%d,%d,%d,%d\n",a,b,c,d);
num=a*1000+b*100+c*10+d;
printf("%d\n",num);
return 0;
}
printf("%d解密後為:",t);
a = t/1000;
b = (t-(a*1000))/100;
c = (t-a*1000-b*100)/10;
d = (t-a*1000-b*100-c*10);
//printf("分解數字%d,%d,%d,%d\n",a,b,c,d);
num=a;
a=c;
c=num;
num=b;
b=d;
d=num;
//printf("調換後%d,%d,%d,%d\n",a,b,c,d);
a = (a+3)%10;
b = (b+3)%10;
c = (c+3)%10;
d = (d+3)%10;
//printf("運算後%d,%d,%d,%d\n",a,b,c,d);
num=a*1000+b*100+c*10+d;
printf("%d\n",num);
return 0;
}
0 意見:
張貼留言