char i = s.charAt(1);
时间: 2024-05-22 14:13:16 浏览: 13
This line of code retrieves the character at index 1 from the string variable s and assigns it to the char variable i. The index of a string starts at 0, so this line retrieves the second character in the string.
相关问题
优化这段代码: public int romanToInt(String s) { int result=0,temp=0,i; for ( i=0; i<s.length();i++){ char c = s.charAt(i); int flag=1; if (i+1>s.length() || i+1==s.length()){ flag=0; } switch (c){ case 'I': if (flag==1 && s.charAt(i+1)=='V'){ temp = 4; i=i+1; break; } else if (flag==1 && s.charAt(i+1)=='X'){ temp = 9; i = i+1; break; } else temp=1; break; case 'V': temp = 5; break; case 'X': if (flag==1 && s.charAt(i+1)=='L'){ temp = 40; i=i+1; break; } else if (flag==1 && s.charAt(i+1)=='C'){ temp = 90; i=i+1; break; } else temp=10; break; case 'L': temp = 50; break; case 'C': if (flag==1 && s.charAt(i+1)=='D'){ temp = 400; i=i+1; break; } else if (flag==1 && s.charAt(i+1)=='M'){ temp = 900; i=i+1; break; } else temp=100; break; case 'D': temp = 500; break; case 'M': temp = 1000; break; } result = result + temp; } return result; }
) { char next = s.charAt(i+1); if (next=='V' || next=='X'){ temp=-1; } } result+=1+temp; temp=0; break; case 'V': result+=5; temp=0; break; case 'X': if (flag==1) { char next = s.charAt(i+1); if (next=='L' || next=='C'){ temp=-10; } } result+=10+temp; temp=0; break; case 'L': result+=50; temp=0; break; case 'C': if (flag==1) { char next = s.charAt(i+1); if (next=='D' || next=='M'){ temp=-100; } } result+=100+temp; temp=0; break; case 'D': result+=500; temp=0; break; case 'M': result+=1000; temp=0; break; } } return result; }
可以使用 HashMap 来存储罗马数字和阿拉伯数字的对应关系,避免使用 switch 语句,同时可以减少一些重复代码。代码如下:
public int romanToInt(String s) {
Map<Character, Integer> map = new HashMap<>();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
int result = 0;
int prev = 0;
for (int i = s.length() - 1; i >= 0; i--) {
int curr = map.get(s.charAt(i));
if (curr < prev) {
result -= curr;
} else {
result += curr;
}
prev = curr;
}
return result;
}
这段代码使用了倒序遍历字符串的方式,每次比较当前字符对应的数字和前一个字符对应的数字的大小关系,如果当前数字小于前一个数字,则减去当前数字,否则加上当前数字。这样可以避免使用 switch 语句,同时也可以减少一些重复代码。
public class FindSolution { private static boolean check(String s,String[] clauses){ for (String clause : clauses){ boolean clauseResult = false; for (int i = 0 ;i < clause.length(); i++){ char var = clause.charAt(i); boolean invert = false; if (var == '!'){ invert = true; var = clause.charAt(++i); } boolean value = (var == '1'); if (invert) value = !value; if (s.charAt(i) !=(value ? '1' : '0')){ clauseResult = false; break; }else { clauseResult = true; } } if (!clauseResult) return false; } return true; } public static void main(String[] args){ int n = 5 ; String[] clauses = new String[]{"!x1","x2","!x3x4","x5"}; StringBuilder sBuilder = new StringBuilder('0'.repeat(n)); while(true){ String s = sBuilder.toString(); if (check(s,clauses)){ System.out.println("Found a solution: " + s); return; } int carry = 1; for (int i = n-1;i >= 0; i--){ char c = sBuilder.charAt(i); if (c == '0'){ sBuilder.setCharAt(i,'1'); carry = 0; break; }else { sBuilder.setCharAt(i,'0'); } } if (carry == 1){ System.out.println("No solution found."); return; } } } }
这是一个Java实现,在给定的合取范式中搜索满足条件的二进制串:
```java
public class FindSolution {
private static boolean check(String s, String[] clauses) {
for (String clause : clauses) {
boolean clauseResult = false;
for (int i = 0; i < clause.length(); i++) {
char var = clause.charAt(i);
boolean invert = false;
if (var == '!') {
invert = true;
var = clause.charAt(++i);
}
boolean value = (var == '1');
if (invert)
value = !value;
if (s.charAt(i) != (value ? '1' : '0')) {
clauseResult = false;
break;
} else {
clauseResult = true;
}
}
if (!clauseResult)
return false;
}
return true;
}
public static void main(String[] args) {
int n = 5;
String[] clauses = new String[]{"!x1", "x2", "!x3x4", "x5"};
StringBuilder sBuilder = new StringBuilder('0'.repeat(n));
while (true) {
String s = sBuilder.toString();
if (check(s, clauses)) {
System.out.println("Found a solution: " + s);
return;
}
int carry = 1;
for (int i = n - 1; i >= 0; i--) {
char c = sBuilder.charAt(i);
if (c == '0') {
sBuilder.setCharAt(i, '1');
carry = 0;
break;
} else {
sBuilder.setCharAt(i, '0');
}
}
if (carry == 1) {
System.out.println("No solution found.");
return;
}
}
}
}
```
其中,`check`方法根据给定的合取范式和一个二进制串,判断这个二进制串是否满足合取范式。`main`方法是主函数,循环搜索二进制串并进行判断,直到找到一个解或者无解为止。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)