优快云竞赛六期

优快云竞赛六期

优快云编程竞赛报名地址:https://edu.youkuaiyun.com/contest/detail/16

题目1

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;
	}
}
题目2

鬼画符门,每年都会统计自己宗门鬼画符消耗的数量,往年一直是大师兄管理, 但是这次鬼艺接手了, 你能帮鬼艺写一个程序统计每年消耗数量最多的鬼画符吗?

思路
  1. 统计每种的数量
  2. 寻找数量最多的种类
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;
	}
}
题目3

已知字符串str,str表示邮箱的不标准格式。 其中”.”会被记录成”dot”,”@”记录成”at”。 写一个程序将str转化成可用的邮箱格式。(可用格式中字符串中除了开头结尾所有”dot”,都会被转换,”at”只会被转化一次,开头结尾的不转化)

思路
  1. 判断是否以dot开头或者结尾
    判断是否以at开头或者结尾
  2. 确定要转换的开始索引和结束索引位置
    截取子串
  3. 转换
  4. 拼接–>形成最终的结果
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;
	}
}
题目4

给一个无序数组,求最长递增的区间长度。如:[5,2,3,8,1,9] 最长区间 2,3,8 长度为 3

思路
  1. 确定从每一个索引开始的递增的区间的长度
  2. 遍历获取最大的长度
#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暴风雨中的白杨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值