鱼C论坛

 找回密码
 立即注册
查看: 4117|回复: 7

oj显示答案错误,检查了半天没发现错误

[复制链接]
发表于 2018-11-25 12:55:41 | 显示全部楼层 |阅读模式

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

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

x
问题描述:
某个公司想用电话来传送数据,但又担心被人窃听。数据都是由四位数字组成的整数。为了使数据传送更加安全一些,公司要你编写一个加密数据和解密数据的程序。程序读取一个四位整数,然后按如下规则加密:每位数字都加上7,然后用和除以10的余数取代该数字;再把第1位与第3位交换,第2位与第4位交换。说明:将最高位视作第1位。如整数1234,则第1位是1,第4位是4。

输入与输出要求:
输入两个整数n和f,n代表待加密或解密的整数(0000<=n<=9999),f为标志整数,当f为1时代表将整数n加密,当f为0时代表将整数n解密。输出加密数字或解密数字,加密结果输出为“After encrypting the number is XXXX”, 解密结果输出为“After decrypting the number is XXXX”。

程序运行效果:
Sample 1:
1890 1↙
After encrypting the number is 6785↙
Sample 2:
6785 0↙
After decrypting the number is 1890↙


代码如下:


#include<stdio.h>
#include<string.h>
int main()
{
        int i,j;
        scanf("%d %d",&i,&j);
        //printf("%d,%d",i,j);
        int temp=0;
        int a,b,c,d;
        if(j==1)
        {
                a=i/1000;
                 b=i/100-a*10;
                 c=i/10-b*10-a*100;
                 d=i-a*1000-b*100-c*10;
                
                a=a+7;
                b=b+7;
                c=c+7;
                d=d+7;
                a=a%10;
                b=b%10;
                c=c%10;
                d=d%10;
                temp=a;
                a=c;
                c=temp;
                temp=b;
                b=d;
                d=temp;
                i=a*1000+b*100+c*10+d;
                if(a==0)
                {
                        printf("After encrypting the number is 0%d",i);
                }
                else{
               
                printf("After encrypting the number is %d",i);
         }
         }
         if(j==0)
         {
                 a=i/1000;
                 b=i/100-a*10;
                 c=i/10-b*10-a*100;
                 d=i-a*1000-b*100-c*10;
                
                
                 temp=a;
                 a=c;
                 c=temp;
                 temp=b;
                 b=d;
                 d=temp;
                
                 a=((a==7||a==8||a==9))?a-7:(a+3);
                 b=((b==7||b==8||b==9))?b-7:(b+3);
                 c=((c==7||c==8||c==9))?c-7:(c+3);
                 d=((d==7||d==8||d==9))?d-7:(d+3);
                 //printf("%d,%d,%d,%d",a,b,c,d);
                 i=a*1000+b*100+c*10+d;
                 printf("After decrypting the number is %d",i);
         }
         return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-11-25 16:09:43 | 显示全部楼层
求助
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-11-25 20:21:06 | 显示全部楼层
救命啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-12-6 22:10:50 | 显示全部楼层
求助
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-12-7 13:05:21 | 显示全部楼层
#include <iostream>
#include <string>

const std::string Encrypting(const std::string &s_num)
{
        std::string result;
        for(const auto &i: s_num)
        {
                size_t num = i - '0';
                result += ((num + 7) % 10) + '0';
        }

        std::swap(result[0], result[2]);
        std::swap(result[1], result[3]);
        return result;
}

const std::string Decrypting(const std::string &s_num)
{
        std::string result;
        for(const auto &i : s_num)
        {
                size_t num = i - '0';
                if(num >= 7)
                        result += (char(num) - 7) + '0';
                else
                        result += (char(num) + 10 - 7) + '0';
        }

        std::swap(result[0], result[2]);
        std::swap(result[1], result[3]);
        return result;
}

int main()
{
        std::string code;
        bool flag;
        std::cin >> code >> flag;

        if(flag)
        {
                std::cout << "After encrypting the number is " << Encrypting(code) << std::endl;
        }
        else
        {
                std::cout << "After decrypting the number is " << Decrypting(code) << std::endl;
        }

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-7 13:06:20 | 显示全部楼层
1890 1
After encrypting the number is 6785
请按任意键继续. . .
6785 0
After decrypting the number is 1890
请按任意键继续. . .
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2018-12-17 16:36:35 | 显示全部楼层

可是我还是没有发现我哪里错误了
emm
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-17 21:35:39 | 显示全部楼层
lovesukie 发表于 2018-12-17 16:36
可是我还是没有发现我哪里错误了
emm

要描述清楚问题,你的程序有问题吗?有什么问题?
1.png
2.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 02:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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