藍橋杯校內選拔(牛客ACM競賽平臺)
阿新 • • 發佈:2020-12-21
題目1:
小明很喜歡寫文章,他想知道自己寫的文章標題中有多少個字元。
注意:標題中可能含有的字元:英文字母(大小寫),數字,空格。統計時,空格不計算在內。
程式碼:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
char arr;
int count=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)!=' '){
count++;
}
}
System.out.println(count);
}
}
題目2:
資料庫中的資料都是長度不超過10000的字串,如果對於兩個字串s和t,他們其中一個在倒置後與另一個相等,那麼我們就定義s和t是“異常資料對”。
- 輸入描述:
輸入多組資料。
第一行輸入一個整數T,表示接下來有T組資料。
每組資料是兩行,每行一個字串分別表示s和t。
- 輸出描述:
示例1
輸入
4
chuang
chuang
xin
nix
chuang
gnauhc
ye
ye
輸出
TAT
QAQ
QAQ
TAT
程式碼:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Scanner scc = new Scanner(System.in);
int z = sc.nextInt();
if (z>=1 & z<=1000){
String[] a = new String[z*2];
for (int i = 0; i < z*2 ; i++) {
String s = scc.nextLine();
if(s.length()>=1 & s.length()<=10000) {
a[i] = s;
}
}
for (int i = 0; i < a.length ; i+=2) {
String ss = a[i + 1].toString();
StringBuilder sb = new StringBuilder(ss);
if(a[i].equals(sb.reverse().toString())){
System.out.println("QAQ");
}else{
System.out.println("TAT");
}
}
}
}
}
- 出現問題截圖:
- 問題解決:
題目3:
只含有“b”和“c”的字串,且字串中不能出現兩個連續的“c”。請你求出有幾種長度為n的字串是計信院同學喜歡的字串。答案對1000000007取模。
- 輸入描述:
輸入一個整數n。
1<=n<=100000
- 輸出描述:
輸出一個整數表示答案。
示例1
輸入
3
輸出
5
說明
cbb,cbc,bbb,bbc,bcb
示例2
輸入
1
輸出
2
說明
c,b
- 程式碼:
不怎麼理解題目,當時的程式碼
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int count=1;
if(a>=1 & a <=100000){
int[] arr = new int[a+2];
for(int i=2; i<a+2;i++){
arr[0]=1;
arr[1]=1;
arr[i]=arr[i-2]+arr[i-1];
count=arr[i];
}
System.out.println(count);
}
}
}