1. 字符串最后一個單詞的長度
計算字符串最后一個單詞的長度鸽疾,單詞以空格隔開。
知識點 字符串,循環(huán)
運行時間限制 0M
內(nèi)存限制 0
輸入
一行字符串,長度小于128。
輸出
整數(shù)N犀暑,最后一個單詞的長度。
樣例輸入 hello world
樣例輸出 5
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String tmpStr = sc.nextLine();
String[] arr = tmpStr.split(" ");
System.out.println(arr[arr.length - 1].length());
}
sc.close();
}
}
2.合唱隊
計算最少出列多少位同學烁兰,使得剩下的同學排成合唱隊形
說明:
N位同學站成一排耐亏,音樂老師要請其中的(N-K)位同學出列,使得剩下的K位同學排成合唱隊形沪斟。
合唱隊形是指這樣的一種隊形:設K位同學從左到右依次編號為1广辰,2…,K主之,他們的身高分別為T1择吊,T2,…槽奕,TK干发, 則他們的身高滿足存在i(1<=i<=K)使得Ti<T2<......<Ti-1<Ti>Ti+1>......>TK。
你的任務是史翘,已知所有N位同學的身高枉长,計算最少需要幾位同學出列,可以使得剩下的同學排成合唱隊形琼讽。
知識點 循環(huán)
運行時間限制 0M
內(nèi)存限制 0
輸入
整數(shù)N
一行整數(shù)必峰,空格隔開,N位同學身高
輸出
最少需要幾位同學出列
樣例輸入 8 186 186 150 200 160 130 197 200
樣例輸出 4
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0;i < n;i++){
arr[i] = sc.nextInt();
}
int[] inc = new int[n];
inc[0] = 1;
for(int i = 1;i < n;i ++){
inc[i] = 1;
for(int j = 0;j < i;j ++){
if(arr[j] < arr[i] && inc[j] + 1 > inc[i]){
inc[i] = inc[j] + 1;
}
}
}
int[] des = new int[n];
des[n - 1] = 1;
for(int i = n -2;i >=0;i --){
des[i] = 1;
for(int j = n - 1;j > i;j --){
if(arr[j] < arr[i] && des[j] + 1 > des[i]){
des[i] = des[j] + 1;
}
}
}
int max = 0;
for(int i = 0;i < n;i ++){
if(inc[i] + des[i] - 1> max){
max = inc[i] + des[i] - 1;
}
}
System.out.println(n - max);
}
sc.close();
}
}
3.圖片整理
Lily上課時使用字母數(shù)字圖片教小朋友們學習英語單詞钻蹬,每次都需要把這些圖片按照大泻鹨稀(ASCII碼值從小到大)排列收好。請大家給Lily幫忙问欠,通過C語言解決肝匆。
知識點 字符串
運行時間限制 0M
內(nèi)存限制 0
輸入
Lily使用的圖片包括"A"到"Z"、"a"到"z"顺献、"0"到"9"旗国。輸入字母或數(shù)字個數(shù)不超過1024。
輸出
Lily的所有圖片按照從小到大的順序輸出
樣例輸入 Ihave1nose2hands10fingers
樣例輸出 0112Iaadeeefghhinnnorsssv
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String str = sc.nextLine();
char[] chs = str.toCharArray();
Arrays.sort(chs);
System.out.println(chs);
}
sc.close();
}
}
4.名字的漂亮度
給出一個名字注整,該名字有26個字符串組成能曾,定義這個字符串的“漂亮度”是其所有字母“漂亮度”的總和度硝。
每個字母都有一個“漂亮度”,范圍在1到26之間寿冕。沒有任何兩個字母擁有相同的“漂亮度”蕊程。字母忽略大小寫。
給出多個名字驼唱,計算每個名字最大可能的“漂亮度”藻茂。
知識點 字符串
運行時間限制 0M
內(nèi)存限制 0
輸入
整數(shù)N,后續(xù)N個名字
N個字符串玫恳,每個表示一個名字
輸出
每個名稱可能的最大漂亮程度
樣例輸入 2 zhangsan lisi
樣例輸出 192 101
import java.util.Scanner;
import java.util.Arrays;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()){
int cnt = Integer.parseInt(sc.nextLine());
int[] nums = new int[cnt];
for(int j= 0;j < cnt;j++){
String str = sc.nextLine().toUpperCase();
int[] ints = new int[26];
for(int i = 0;i < str.length();i++){
ints[str.charAt(i) - 'A'] ++;
}
Arrays.sort(ints);
int result = 0;
for(int i = 0;i < 26;i++)
result += (i+1)*ints[i];
nums[j] = result;
}
for(int i = 0;i < cnt;i++)
System.out.println(nums[i]);
}
sc.close();
}
}
5.(練習用)挑7
輸出7有關(guān)數(shù)字的個數(shù)捌治,包括7的倍數(shù),還有包含7的數(shù)字(如17纽窟,27肖油,37...70,71臂港,72森枪,73...)的個數(shù)
知識點 循環(huán)
運行時間限制 0M
內(nèi)存限制 0
輸入
一個正整數(shù)N。(N不大于30000)
輸出
不大于N的與7有關(guān)的數(shù)字個數(shù)审孽,例如輸入20县袱,與7有關(guān)的數(shù)字包括7,14,17.
樣例輸入 20
樣例輸出 3
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int N = sc.nextInt();
int sum = 0;
for(int i = 1;i <= N;i ++){
if(i % 7 == 0 || String.valueOf(i).toString().contains("7")) {
sum ++;
}
}
System.out.println(sum);
}
sc.close();
}
}
6.字符串加解密
題目描述
1、對輸入的字符串進行加解密佑力,并輸出式散。
2加密方法為:
當內(nèi)容是英文字母時則用該英文字母的后一個字母替換,同時字母變換大小寫,如字母a時則替換為B打颤;字母Z時則替換為a暴拄;
當內(nèi)容是數(shù)字時則把該數(shù)字加1,如0替換1编饺,1替換2乖篷,9替換0;
其他字符不做變化透且。
3撕蔼、解密方法為加密的逆過程。
接口描述:
實現(xiàn)接口秽誊,每個接口實現(xiàn)1個基本操作:
void Encrypt (char aucPassword[], char aucResult[]):在該函數(shù)中實現(xiàn)字符串加密并輸出
說明:
1鲸沮、字符串以\0結(jié)尾。
2锅论、字符串最長100個字符讼溺。
int unEncrypt (char result[], char password[]):在該函數(shù)中實現(xiàn)字符串解密并輸出
說明:
1、字符串以\0結(jié)尾棍厌。
2肾胯、字符串最長100個字符。
知識點 字符串
運行時間限制 10M
內(nèi)存限制 128
輸入
輸入說明
輸入一串要加密的密碼
輸入一串加過密的密碼
輸出
輸出說明
輸出加密后的字符
輸出解密后的字符
樣例輸入 abcdefg BCDEFGH
樣例輸出 BCDEFGH abcdefg
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str1 = sc.next();
String str2 = sc.next();
char aucPassword[] = str1.toCharArray();
char aucResult[] = new char[aucPassword.length];
Encrypt(aucPassword, aucResult);
System.out.println(aucResult);
char result[] = str2.toCharArray();
char password[] = new char[result.length];
unEncrypt(result, password);
System.out.println(password);
}
sc.close();
}
public static void Encrypt(char aucPassword[], char aucResult[]) {
for (int i = 0; i < aucPassword.length; i++) {
char c = aucPassword[i];
if (c >= 'a' & c <= 'z') {
aucResult[i] = (char) ((c + 1 - 'a') % 26 + 'A');
}
if (c >= 'A' & c <= 'Z') {
aucResult[i] = (char) ((c + 1 - 'A') % 26 + 'a');
}
if (c >= '0' & c <= '9') {
aucResult[i] = (char) ((c + 1 - '0') % 10 + '0');
}
}
}
public static int unEncrypt(char result[], char password[]) {
for (int i = 0; i < result.length; i++) {
char c = result[i];
if (c >= 'a' & c <= 'z') {
password[i] = (char) ((26 + c - 1 - 'a') % 26 + 'A');
}
if (c >= 'A' & c <= 'Z') {
password[i] = (char) ((26 + c - 1 - 'A') % 26 + 'a');
}
if (c >= '0' & c <= '9') {
password[i] = (char) ((10 + c - 1 - '0') % 10 + '0');
}
}
return 0;
}
}
7.蛇形矩陣
蛇形矩陣是由1開始的自然數(shù)依次排列成的一個矩陣上三角形耘纱。
樣例輸入
5
樣例輸出
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11
接口說明
原型
void GetResult(int Num, char * pResult);
輸入?yún)?shù):
int Num:輸入的正整數(shù)N
輸出參數(shù):
int * pResult:指向存放蛇形矩陣的字符串指針
指針指向的內(nèi)存區(qū)域保證有效
返回值:
void
知識點 數(shù)組
運行時間限制 10M
內(nèi)存限制 128
輸入
輸入正整數(shù)N(N不大于100)
輸出
輸出一個N行的蛇形矩陣敬肚。
樣例輸入 4
樣例輸出 1 3 6 10 2 5 9 4 8 7
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
int sum = n * (n + 1) / 2;
int rowStart = 1;
for (int i = 1; i <= n; i++) {
rowStart = 1 + i * (i - 1) / 2;
System.out.print(rowStart);
int tmp = 1 + i;
rowStart += tmp;
while (rowStart <= sum) {
System.out.print(" " + rowStart);
tmp++;
rowStart += tmp;
}
System.out.println();
}
}
}
}
8.字符串加密
有一種技巧可以對數(shù)據(jù)進行加密,它使用一個單詞作為它的密匙束析。下面是它的工作原理:首先艳馒,選擇一個單詞作為密匙,如TRAILBLAZERS员寇。如果單詞中包含有重復的字母弄慰,只保留第1個,其余幾個丟棄〉妫現(xiàn)在陆爽,修改過的那個單詞死于字母表的下面,如下所示:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
T R A I L B Z E S C D F G H J K M N O P Q U V W X Y
上面其他用字母表中剩余的字母填充完整扳缕。在對信息進行加密時慌闭,信息中的每個字母被固定于頂上那行,并用下面那行的對應字母一一取代原文的字母(字母字符的大小寫狀態(tài)應該保留)躯舔。因此驴剔,使用這個密匙,Attack AT DAWN(黎明時攻擊)就會被加密為Tpptad TP ITVH粥庄。
請實現(xiàn)下述接口丧失,通過指定的密匙和明文得到密文。
詳細描述:
接口說明
原型:
voidencrypt(char * key,char * data,char * encrypt);
輸入?yún)?shù):
char * key:密匙
char * data:明文
輸出參數(shù):
char * encrypt:密文
返回值:
void
知識點 字符串
運行時間限制 10M
內(nèi)存限制 128
輸入
先輸入key和要加密的字符串
輸出
返回加密后的字符串
樣例輸入 nihao ni
樣例輸出 le
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String key = sc.nextLine();
String data = sc.nextLine();
char[] result = encrypt(key, data);
System.out.println(result);
}
}
public static char[] encrypt(String key, String data) {
key = key.toUpperCase();
String keyDic = "";
for (int i = 0; i < key.length(); i++) {
char c = key.charAt(i);
if (!keyDic.contains(String.valueOf(c)))
keyDic += c;
}
for (int i = 0; i < 26; i++) {
char c = (char) ('A' + i);
if (!keyDic.contains(String.valueOf(c)))
keyDic += c;
}
char[] result = data.toCharArray();
for (int i = 0; i < data.length(); i++) {
char c = data.charAt(i);
if (c >= 'A' && c <= 'Z')
result[i] = (char) (keyDic.charAt(c - 'A'));
if (c >= 'a' && c <= 'z')
result[i] = (char) (keyDic.charAt(c - 'a') - 'A' + 'a');
}
return result;
}
}
9. 公共字串計算
題目標題:
計算兩個字符串的最大公共字串的長度惜互,字符不區(qū)分大小寫
詳細描述:
接口說明
原型:
int getCommonStrLength(char * pFirstStr, char * pSecondStr);
輸入?yún)?shù):
char * pFirstStr //第一個字符串
char * pSecondStr//第二個字符串
知識點 字符串,查找
運行時間限制 10M
內(nèi)存限制 128
輸入
輸入兩個字符串
輸出
輸出一個整數(shù)
樣例輸入 asdfas werasdfaswer
樣例輸出 6
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str1 = sc.next();
String str2 = sc.next();
int len = str1.length();
int result = 0;
for (int i = 0; i < len; i++) {
for (int j = 0; j <= i; j++) {
String subStr = str1.substring(j, j + len - i);
if (str2.contains(subStr)) {
result = len - i;
break;
}
}
if (result > 0)
break;
}
System.out.println(result);
}
sc.close();
}
}
10. 多線程
問題描述:有4個線程和1個公共的字符數(shù)組布讹。線程1的功能就是向數(shù)組輸出A,線程2的功能就是向字符輸出B训堆,線程3的功能就是向數(shù)組輸出C炒事,線程4的功能就是向數(shù)組輸出D。要求按順序向數(shù)組賦值ABCDABCDABCD蔫慧,ABCD的個數(shù)由線程函數(shù)1的參數(shù)指定挠乳。[注:C語言選手可使用WINDOWS SDK庫函數(shù)]
接口說明:
void init(); //初始化函數(shù)
void Release(); //資源釋放函數(shù)
unsignedint__stdcall ThreadFun1(PVOID pM) ; //線程函數(shù)1,傳入一個int類型的指針[取值范圍:1 – 250姑躲,測試用例保證]睡扬,用于初始化輸出A次數(shù),資源需要線程釋放
unsignedint__stdcall ThreadFun2(PVOID pM) ;//線程函數(shù)2黍析,無參數(shù)傳入
unsignedint__stdcall ThreadFun3(PVOID pM) ;//線程函數(shù)3卖怜,無參數(shù)傳入
Unsigned int __stdcall ThreadFunc4(PVOID pM);//線程函數(shù)4,無參數(shù)傳入
char g_write[1032]; //線程1,2,3,4按順序向該數(shù)組賦值阐枣。不用考慮數(shù)組是否越界马靠,測試用例保證
知識點 字符串,循環(huán),鏈表,隊列,棧,查找,搜索,排序,樹,圖,數(shù)組,函數(shù),指針,枚舉,位運算,結(jié)構(gòu)體,聯(lián)合體,文件操作,遞歸
運行時間限制 10M
內(nèi)存限制 128
輸入
輸入一個int整數(shù)
輸出
輸出多個ABCD
樣例輸入 10
樣例輸出 ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
long start = System.currentTimeMillis();
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
// System.out.print("ABCD");
for (int j = 0; j < 4; j++) {
Thread t = new MyThread((char) ('A' + j));
t.start();
t.join();
}
}
System.out.println();
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
}
class MyThread extends Thread {
private char c;
public MyThread(char c) {
this.c = c;
}
@Override
public void run() {
System.out.print(c);
}
}