CSDN编程竞赛报名地址:https://edu.csdn.net/contest/detail/16
X国最近开始严管枪火。 像是“ak”,“m4a1”,“skr”。都是明令禁止的。 现在小Q查获了一批违禁物品其中部分是枪支。小Q想知道自己需要按照私藏枪火来关押多少人。 (只有以上三种枪被视为违法)
遍历 + 判断
import java.util.ArrayList;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str_0 = scan.nextLine().trim();
int n = Integer.parseInt(str_0);
ArrayList<String> vector = new ArrayList<>();
for (int i = 0; i < n; i++){
String str_1 = scan.nextLine().trim();
vector.add(str_1);
}
scan.close();
int result = solution(n, vector);
System.out.println(result);
}
public static int solution(int n, ArrayList<String> vector){
int result = 0;
String name = "";
for(int i=0;i < vector.size();i++){
name = vector.get(i);
if("ak".equals(name) || "m4a1".equals(name) || "skr".equals(name)){
result ++;
}
}
return result;
}
}
鬼画符门,每年都会统计自己宗门鬼画符消耗的数量,往年一直是大师兄管理, 但是这次鬼艺接手了, 你能帮鬼艺写一个程序统计每年消耗数量最多的鬼画符吗?
import java.util.ArrayList;
import java.util.Scanner;
class GuiHuaFu{
String name;
int num;
GuiHuaFu(String name,int num ){
this.name = name;
this.num = num;
}
}
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str_0 = scan.nextLine().trim();
int n = Integer.parseInt(str_0);
ArrayList<String> vector = new ArrayList<>();
for (int i = 0; i < n; i++){
String str_1 = scan.nextLine().trim();
vector.add(str_1);
}
scan.close();
String result = solution(n, vector);
System.out.println(result);
}
public static String solution(int n, ArrayList<String> vector){
ArrayList<GuiHuaFu> res = new ArrayList<>();
String result = "";
for(int i=0;i< vector.size();i++){
result = vector.get(i);
for( int j =0;j< res.size();j++){
if(result.equals(res.get(j).name)){
res.get(j).num ++;
continue;
}
}
res.add(new GuiHuaFu(result,1));
}
int maxIndex = 0;
for(int j=1;j<res.size();j++){
if(res.get(maxIndex).num < res.get(j).num){
maxIndex = j;
}
}
result = res.get(maxIndex).name;
return result;
}
}
已知字符串str,str表示邮箱的不标准格式。 其中”.”会被记录成”dot”,”@”记录成”at”。 写一个程序将str转化成可用的邮箱格式。(可用格式中字符串中除了开头结尾所有”dot”,都会被转换,”at”只会被转化一次,开头结尾的不转化)
import java.util.ArrayList;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str_0 = scan.nextLine().trim();
String str = str_0;
scan.close();
String result = solution(str);
System.out.println(result);
}
public static String solution(String str){
String result = "";
int startIndex = 0;
int endIndex = str.length()-1;
boolean startWithAt = false;
boolean startWithDot = false;
boolean endWithAt = false;
boolean endWithDot = false;
if(str.startsWith("at")){
startIndex += 2;
startWithAt = true;
}
if(str.startsWith("dot")){
startIndex += 3;
startWithDot = true;
}
if(str.endsWith("at")){
endIndex -= 2;
endWithAt = true;
}
if(str.endsWith("dot")){
endIndex -= 3;
endWithDot = true;
}
String sub = str.substring(startIndex,endIndex+1);
result = sub.replace("dot",".");
result = result.replaceFirst("at","@");
if(startWithAt){
result = "at" + result;
}else if(startWithDot){
result = "dot" + result;
}
if(endWithAt){
result = result + "at";
}else if(endWithDot){
result = result + "dot";
}
return result;
}
}
给一个无序数组,求最长递增的区间长度。如:[5,2,3,8,1,9] 最长区间 2,3,8 长度为 3
#include <stdio.h>
#include <stdlib.h>
int solution(int n, int arr[]){
int res[10000] = {0};
int result = 0;
for(int i =0;i< n;i++){
res[i] ++;
for(int j = i;j< n;j++){
if( arr[j+1] > arr[j] ) res[i] ++;
else break;
}
}
result = res[0];
for(int i =1;i< n;i++){
if(result < res[i]){
result = res[i];
}
}
return result;
}
int main() {
int n;
scanf("%d", &n);
int *arr;
arr = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int result = solution(n, arr);
printf("%d", result);
return 0;
}