Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用字符串将字母分配给素数_Java_Arrays_String_Encryption_Prime Factoring - Fatal编程技术网

在Java中使用字符串将字母分配给素数

在Java中使用字符串将字母分配给素数,java,arrays,string,encryption,prime-factoring,Java,Arrays,String,Encryption,Prime Factoring,我对编程相当陌生,我的家庭作业有点麻烦。 这是我在说明书中看到的: 加密: “嗨”=>h->23,i->29=>h*i=>23*29=667 解密: 667=>(素因子分解)23*29=>23->h 29->i=>“嗨” 错误处理 如果有人输入了一个分解为其他素数(列表外)的数字该怎么办? 请选择模式(加密[e]/解密[d]):d 请输入一个整数:234234 空的 我有一个代码: import java.util.Scanner; public class Homework3 { sta

我对编程相当陌生,我的家庭作业有点麻烦。 这是我在说明书中看到的:

加密:
“嗨”=>h->23,i->29=>h*i=>23*29=667
解密:
667=>(素因子分解)23*29=>23->h 29->i=>“嗨”
错误处理
如果有人输入了一个分解为其他素数(列表外)的数字该怎么办?
请选择模式(加密[e]/解密[d]):d
请输入一个整数:234234
空的

我有一个代码:

import java.util.Scanner;


public class Homework3 {

static int[] primes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103};
static String alphabet = "abcdefghijklmnopqrstuvwxyz";

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    System.out.print("Please choose the mode (Encrypt [e] / Decrypt [d]): ");
    String mode = scan.next();
    String s = "";
    if (mode.equals("e")) {
        System.out.print("Please enter a String: ");
        s = scan.next();
        System.out.println(">> " + encrypt(s));
    }
    else if (mode.equals("d")) {
        System.out.print("Please enter an integer: ");
        s = scan.next();
        System.out.println(">> " + decrypt(s));
    }

}

// Examples:
// for s = "hi", return "667"
public static String encrypt(String s) {
    // Fill in the blanks
    return "";
}

// Examples:
// for x = "667", return "hi"
// for x = "327", return null
public static String decrypt(String x) {
    // Fill in the blanks
    return "";
}

}

我这辈子没这么快做过家庭作业。请检查边缘箱

public class Homework3 {

static int[] primes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103};
static String alphabet = "abcdefghijklmnopqrstuvwxyz";
Map<Integer, Character> encryptMap = new HashMap<Integer, Character>();
Map<Character, Integer> dencryptMap = new HashMap<Character, Integer>();
public static void main(String[] args) {
   for(int i = 0; i < alphabet.length(); i++){
     Character c = alphabet.charAt(i);
     encryptMap.put(primes[i], c);
     dencryptMap.put(c, primes[i]);
  } 

  //---your code


}
  public int encrypt(String s){
    if(s.length()==0) return 1;
    return encryptMap.get(s.charAt(0)) * encrypt(s.substring(1)); 
  }
  public String dencrypt(String s){
    Integer asInt = Integer.parseInt(s);
    List<Integer> factors = findFactors(asInt);
    StringBuilder builder = new StringBuilder();
    for(Integer i: factors){
        builder.append(decryptMap.get(i));
    }
   String result = builder.toString();
   return result.length() == 0 ? null : result;
  }
   public List<Integer> findFactors(int number){
        int n = number;
    List<Integer> factors = new ArrayList<Integer>();
    for (int i = 2; i <= n; i++) {
      while (n % i == 0) {
        factors.add(i);
        n /= i;
      }
    }
    return factors;
  }
}
公共课家庭作业3{
静态int[]素数={3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103};
静态字符串字母表=“abcdefghijklmnopqrstuvxyz”;
Map encryptMap=newhashmap();
Map dencryptMap=newhashmap();
公共静态void main(字符串[]args){
对于(int i=0;i对于(inti=2;i,这里有一个简单的解决方案,有几个注意事项

无效输入 它不受解密非数字请求的保护。 它将抛出一个异常-通常不是对用户错误的可接受响应

它似乎仍然有一个bug,因为当我加密“cat”时,我得到7*3*73=1533,这很好

但当我解密1533时,我得到的“act”是3*7*73。Shome mishtake Shurly!:)

我不会解释
encrypt()
。这很琐碎

decrypt()
只需对素数进行运算,查看每个素数可以按顺序获得多少因子,直到数字达到1(或零,尽管这是无效的输入..)。 这就是你能除以3的次数。这就是你有多少个“a”。 除以5表示“b”的数量。 等等

记住,只有当你没有素数列表时,素数分解才是困难的

// Examples:
// for s = "hi", return "667"
public static String encrypt(String s) {
    int result=1;
    for(int i=0;i<s.length();++i){
        int index=alphabet.indexOf(s.charAt(i));
        if(index!=-1){//Brutally ignore unknown characters.
            result=result*primes[index];
        }
    }
    return Integer.toString(result);
}

// Examples:
// for x = "667", return "hi"
// for x = "327", return null
public static String decrypt(String x) {
    if(x.equals("1")){
        return "";//Special case - the empty string.
    }
    StringBuilder builder=new StringBuilder(x.length()*2);
    int bix=Integer.parseInt(x);
    for(int i=0;i<primes.length;++i){
        int p=primes[i];
        for(;;){
            if(bix%p==0){
                builder.append(alphabet.charAt(i));
                bix=bix/p;
                if(bix==1){
                    break;
                }   
            }else{
                break;
            }
        }
        if(bix<=1){
            break;
        }       
    }
    if(bix!=1){
        //something went wrong.
        builder.append("?");//Do what you like with errors...
    }
    return builder.toString();
}
//示例:
//对于s=“hi”,返回“667”
公共静态字符串加密(字符串s){
int结果=1;

对于(int i=0;iNikola)。存在两个编译错误。您正在从
static
main
方法访问非
static
变量。您还调用了解密映射
dencryptMap
(原文如此)但是稍后将其称为
decrypt
。您也可以使用
findFactors
方法来浪费时间,试图找到那些不是素数且不存在的因子。例如,当
i==6
时,它将在循环的早期迭代中已经去掉2和3的任何因子。无害的,但您已经在plate.谢谢,但我认为这对于我们正在做的事情来说太复杂了。不过,非常感谢!首先,非常感谢!你提到的问题根本不是问题,我可以有这个bug。但是,我们有一个测试代码给我们评分,我有一个问题。你编写的代码完美地打印出了简短的单词和整数,howev呃,当你写一个较长的单词时,例如计算机,它会输出一个负值,而当你输入一个较长的整数时,它根本不会运行。那么我应该使用biginteger还是其他什么呢?再次,非常感谢!我认为这可能是可以接受的行为。让它成为真正的编码的一个明显方法是遵循库尔特·哥德尔提供的模型并使用p了解代码的威力并使用算术的基本定理。对于较长的消息,该方法将溢出。实际上,我从
biginger
开始就这样做了,但我认为这可能超出了您的工作范围。我不希望您交上不理解的作业。我将其修改为
biginger
你真正的家庭作业。或者你可以把它重新贴在Stackoverflow上。。。