1题目描述
在n个元素的数组中,找到值为k的数字对去重后的个数
//参考代码 30% case
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++){
arr[i] = sc.nextInt();
}
System.out.println(solve(arr,k));
}
static int solve(int arr[], int k){
int len = arr.length;
ArrayList<Integer> rest = new ArrayList<>();
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
if (Math.abs(arr[i] - arr[j]) == k && (!rest.contains(arr[i] + arr[j]))) {
rest.add(arr[i] + arr[j]);
}
}
}
return rest.size();
}
}
2题目描述
打印输出下面的效果:
输入描述
第一行是一个整数n,接下来n行都是 由数字6构成的数学表达式(+,-,*法运算)
输出描述
用图形输出数学表达式的结果,如下效果图
//AC 代码
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String arr[] = new String[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.next();
}
for (int i = 0; i < n; i++) {
int num = cal(arr[i]);
String sNum = String.valueOf(num);
int k = sNum.length();
// System.out.println(sNum +"--"+k);
int len = 5 * k + (k - 1) * 2;
char[][] chars = new char[5][len];
for (int p = 0; p < 5; p++) {
for (int q = 0; q < len; q++) {
chars[p][q] = '.';
}
}
for (int j = 0; j < k; j++) {
// 取出字符后减去'0'(注意是字符0)就可以得到数值了,比如'1'-'0'=1;
int shu = sNum.charAt(j) - '0';
switch (shu) {
case 0:
for (int p = 0; p < 5; p++) {
for (int q = 7 * j; q <= 7 * j + 4; q++) {
if (p == 0 || p == 4) {
chars[p][q] = '6';
}
if (q == 7 * j || q == 7 * j + 4) {
chars[p][q] = '6';
}
}
}
break;
case 1:
for (int p = 0; p < 5; p++) {
chars[p][7 * j + 4] = '6';
}
break;
case 2:
for (int p = 0; p < 5; p++) {
for (int q = 7 * j; q <= 7 * j + 4; q++) {
if (p == 0 || p == 4 || p == 2) {
chars[p][q] = '6';
}
if (q == 7 * j && p > 1) {
chars[p][q] = '6';
}
if (p < 2 && q == 7 * j + 4) {
chars[p][q] = '6';
}
}
}
break;
case 3:
for (int p = 0; p < 5; p++) {
for (int q = 7 * j; q <= 7 * j + 4; q++) {
if (p == 0 || p == 4 || p == 2) {
chars[p][q] = '6';
}
if (q == 7 * j + 4) {
chars[p][q] = '6';
}
}
}
break;
case 4:
for (int p = 0; p < 5; p++) {
for (int q = 7 * j; q <= 7 * j + 4; q++) {
if (p == 2) {
chars[p][q] = '6';
}
if (q == 7 * j && p < 1) {
chars[p][q] = '6';
}
if (q == 7 * j + 2) {
chars[p][q] = '6';
}
}
}
break;
case 5:
for (int p = 0; p < 5; p++) {
for (int q = 7 * j; q <= 7 * j + 4; q++) {
if (p == 0 || p == 4 || p == 2) {
chars[p][q] = '6';
}
if (q == 7 * j && p < 1) {
chars[p][q] = '6';
}
if (p > 1 && q == 7 * j + 4) {
chars[p][q] = '6';
}
}
}
break;
case 6:
for (int p = 0; p < 5; p++) {
for (int q = 7 * j; q <= 7 * j + 4; q++) {
if (p == 0 || p == 4 || p == 2) {
chars[p][q] = '6';
}
if (q == 7 * j) {
chars[p][q] = '6';
}
if (p > 1 && q == 7 * j + 4) {
chars[p][q] = '6';
}
}
}
break;
case 7:
for (int p = 0; p < 5; p++) {
for (int q = 7 * j; q <= 7 * j + 4; q++) {
if (p == 0) {
chars[p][q] = '6';
}
if (q == 7 * j + 4) {
chars[p][q] = '6';
}
}
}
break;
case 8:
for (int p = 0; p < 5; p++) {
for (int q = 7 * j; q <= 7 * j + 4; q++) {
if (p == 0 || p == 4 || p == 2) {
chars[p][q] = '6';
}
if (q == 7 * j) {
chars[p][q] = '6';
}
if (q == 7 * j + 4) {
chars[p][q] = '6';
}
}
}
break;
case 9:
for (int p = 0; p < 5; p++) {
for (int q = 7 * j; q <= 7 * j + 4; q++) {
if (p == 0 || p == 2) {
chars[p][q] = '6';
}
if (q == 7 * j && p < 1) {
chars[p][q] = '6';
}
if (q == 7 * j + 4) {
chars[p][q] = '6';
}
}
}
break;
}
}
for (int ii = 0; ii < chars.length; ii++) {
System.out.println(new String(chars[ii]));
}
}
}
/**
* 计算表达式的值
*
* @param str
* @return
*/
static int cal(String str) {
char[] cs = str.toCharArray();
int ans = 6;
for (int i = 1; i < cs.length; i++) {
switch (cs[i]) {
case '+':
ans = ans + 6;
break;
case '-':
ans = ans - 6;
break;
case '*':
ans = ans * 6;
break;
case '6':
break;
}
}
return ans;
}
}
3题目描述
字符串操作
4题目描述