取得每一列最大值加起来即可,同一个人只能取一次
两重循环用hashset存储不同的非空字符。
import java.util.HashSet;
import java.util.Scanner;
/*
*0100110001010001
100
*/
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String string = in.nextLine();
HashSet<String> set = new HashSet<String>();
int count = 0;
for(int i = 0;i<string.length();i++) {
for(int j = i;j<string.length();j++) {
if(!set.contains(string.substring(i, j+1))) {
set.add(string.substring(i, j+1));
count++;
}
}
}
System.out.println(count);
}
}
迭代求得20190324的项
/*
* 20190324
* 4659
*/
public class Main{
static int mod = 10000;
public static void main(String[] args) {
int a = 1;
int b = 1;
int c = 1;
int n = 4;
while (n <= 20190324) {
int temp = (a + b + c) % mod;
a = b;
b = c;
c = temp;
n++;
}
System.out.println(c);
}
}
三重循环分别枚举每个数,且i<j<k从而确保不会出现重复的解法
public class Main{
public static void main(String[] args) {
int count = 0;
for(int i = 1;i<=2019;i++) {
if(check(i+" ")) continue;
for(int j = i+1;j<=2019;j++) {
if(check(j+"")) continue;
for(int k = j+1;k<=2019;k++) {
if(check(k+"")) continue;
if(i +j +k==2019) {
count++;
}
}
}
}
System.out.println(count);
}
static boolean check(String str) {
if (str.contains("2") || str.contains("4"))
return true;
else
return false;
}
}
从1->n枚举每一位数判断其是否包含2、0、1、9,将累加器+1,
直到遍历到n即可
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner inScanner = new Scanner(System.in);
int n = inScanner.nextInt();
int res = 0;
for(int i =1 ;i<=n;i++) {
if(Checked(i + " ")) {
res+=i;
}
}
System.out.println(res);
}
static boolean Checked(String str) {
if(str.contains("1")||str.contains("2")||str.contains("0")||str.contains("9")) {
return true;
}
return false;
}
}
set存储优先级的店铺
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int m = in.nextInt();
int T = in.nextInt();
int f[][] = new int[m + 1][2];
for (int i = 1; i <= m; i++) {
for (int j = 0; j < 2; j++) {
f[i][j] = in.nextInt();
}
}
HashSet<Integer> set =new HashSet<Integer>();
int []sign = new int[N+1];
int []priority = new int[N +1];
for(int i= 1;i<=T;i++) {
for(int j = 1;j<=m;j++) {
if(f[j][0] == i) {
priority[f[j][1]]+=2;
if(priority[f[j][1]]> 5 && !set.contains(f[j][1])) {
set.add(f[j][1]);
}
sign[f[j][1]] =1;
}
}
for(int k = 1;k<= N;k++) {
if(sign[k] == 0 && priority[k] >0) {
priority[k]--;
}
if(priority[k]<=3) {
set.remove(k);
}
}
sign = new int[N +1];
}
System.out.println(set.size());
}
}
先将数组中的元素进行排序。
当负号个数为0时最大结果即为所有数之和。
当负数的个数为n+m+1,肯定会留一个负数,加上负数中最大的那个
如果负数的个数不等于n+m+1的,可以通过加括号的形式,将所有的减号变为加号.
如果全为正数则减去最小的那个数。
package 蓝桥杯101;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m =in.nextInt();
long a[] = new long[n+m+1];
for(int i = 0;i<n+m+1;i++) {
a[i] = in.nextLong();
}
Arrays.sort(a);
long sum = 0;
int count = 0;
for(int i = 0;i<n + m +1;i++) {
sum+=a[i];
if(a[i] < 0) count ++;
}
if(m == 0) System.out.println(sum);
else {
if(count>0) {
if(count==n+m+1)
for(int i =0 ;i<count ;i++)
sum-=2*a[i];
else {
for(int i = 0;i<count;i++)
sum-=2*a[i];
}
}else
sum-=2*a[0];
System.out.println(sum);
}
}
}
能力有限,不会