@ "TOC" 演算法題訓練網站: "http://www.dotcpp.com" 1.簡單的a+b (1)題目地址: "https://www.dotcpp.com/oj/problem1000.html" (2)演算法解析: 首先要能夠接收到橫向用空格分開的數據,並知道當運行的時候,在什麼地方可以停 ...
目錄
@(這裡寫自定義目錄標題)
演算法題訓練網站:http://www.dotcpp.com
1.簡單的a+b
(1)題目地址:https://www.dotcpp.com/oj/problem1000.html
(2)演算法解析: 首先要能夠接收到橫向用空格分開的數據,並知道當運行的時候,在什麼地方可以停止。
(3)語法解析:
用java語法的時候scanner.nextInt();直接可以識別整數,scanner.hasNext()配合while迴圈可以一直等到輸入的最後一個整數;
而用python語法需要用到map函數。
(4)python代碼
while True:
try:
a,b=map(int,input().strip().split())
print(a+b)
except:
break
(5)java代碼
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = a + b;
System.out.println(c);
}
}
}
2.第一個HelloWorld程式!
(1)題目地址:https://www.dotcpp.com/oj/problem1001.html
(2)演算法解析: 直接輸出即可
(3)語法解析: 直接輸出即可
(4)python代碼
print('**************************')
print('Hello World!')
print('**************************')
(5)java代碼
public class Main {
public static void main(String[] args) {
System.out.println("**************************");
System.out.println("Hello World!");
System.out.println("**************************");
}
}
3.三個數最大值
(1)題目地址:https://www.dotcpp.com/oj/problem1002.html
(2)演算法解析: 設定一個中間變數,然後和三個數進行對比,最後把最大的數賦給中間變數即可。
(3)語法解析:
java代碼可以用數組或者直接三個數進行比較,可以配合三元表達式;
python代碼可以用max函數。
(4)python代碼
a,b,c = map(int,input().strip().split())
print(max(a,b,c))
(5)java代碼1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int max = 0;
if (a <= b) max = a;
else max = b;
if (max <= c) max = c;
System.out.println(max);
}
}
(5)java代碼2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a[] = new int[3],max = 0;
a[0] = scanner.nextInt();
a[1] = scanner.nextInt();
a[2] = scanner.nextInt();
for(int i=0;i < 3;i++)
{
max=(a[i] < max?max: a[i]);
}
System.out.println(max);
}
}
4.密碼破譯
(1)題目地址:https://www.dotcpp.com/oj/problem1003.html
(2)演算法解析: 字元串中的字元先轉化為ASCII碼,然後增加到需要的字元的ASCII碼後,再轉化為字元。這個就是有名的凱撒密碼。
(3)語法解析:
java代碼可以生成26個字母的數組,然後對比每個輸入字元,向後移動,也可以先轉化為ASCII碼,然後再進行轉化,這裡用第一種。
python代碼直接用ord函數和chr函數即可進行轉換。
(4)python代碼
a = input()
for i in a:
print(chr(ord(i) + 4),end="")
(5)java代碼
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String kk = scanner.nextLine();
char [] s = new char[52];
for(int i = 0;i<=25;i++){
s[i] = (char)(96+i+1);
s[i+26] = (char)(64+i+1);
}
for(int j = 0;j < kk.length();j++)
{
for(int i = 0;i<52;i++)
{
if(kk.charAt(j) == s[i])
{
System.out.print(s[i+4]);
}
}
}
}
}
5.母牛的故事
(1)題目地址:https://www.dotcpp.com/oj/problem1004.html
(2)演算法解析:
(3)語法解析:
利用上面的公式分段即可求解。
(4)python代碼
n = eval(input())
k = []
while n != 0:
if n > 4:
s = [i for i in range(0,5)]
for i in range(5,n+1):
s.append(s[i-3] + s[i-1])
k.append(s[-1])
else:
k.append(n)
n = eval(input())
for i in k:
print(i,end="\n")
(5)java代碼
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n;
while (scanner.hasNext()) {
n = scanner.nextInt();
if(n == 0)break;
System.out.println(result(n));
}
}
private static int result(int n) {
if(n<=4) {
return n;
}else {
return result(n-1)+result(n-3);
}
}
}
6.7.8.9.10
(1)題目地址:
第6題:https://www.dotcpp.com/oj/problem1005.html
第7題:https://www.dotcpp.com/oj/problem1006.html
第8題:https://www.dotcpp.com/oj/problem1007.html
第9題:https://www.dotcpp.com/oj/problem1008.html
第10題:https://www.dotcpp.com/oj/problem1009.html
(2)演算法解析: 6.7.8.9都是基礎語法題,這裡就不進行分析了,是很簡單的題目。直接上代碼:
(4)python代碼
第6題:
n = eval(input())
result = 5*(n-32)/9
print('c=%.2f' % result)
第7題:
a,b,c=map(int,input().strip().split())
print(max(a,b,c))
第8題:
x = eval(input())
y = x if x < 1 else 2 * x - 1 if x >= 1 and x < 10 else 3 * x - 11
print(y)
第9題:
x = eval(input())
s = {100:'A',90:'A',80:'B',70:'C',60:'D'}
if x < 60:
print('D')
else:
print(s[x // 10 * 10])
第10題:
n = input()
print(len(n))
print(' '.join(n))
print(n[::-1])
(5)java代碼
第6題
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
double result = 5 * (a-32)/9;
System.out.println(String.format("c=%.2f", result));
}
}
第7題:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a[] = new int[3],max = 0;
a[0] = scanner.nextInt();
a[1] = scanner.nextInt();
a[2] = scanner.nextInt();
for(int i=0;i < 3;i++)
{
max=(a[i] < max?max: a[i]);
}
System.out.println(max);
}
}
第8題
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int b = x < 1?x:(x >= 1 && x < 10 ?2*x-1:3*x-11);
System.out.println(b);
}
}
第9題
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
if (a >= 90)System.out.println('A');
else if (a >= 80 && a < 90)System.out.println('B');
else if (a >= 70 && a < 80)System.out.println('C');
else if (a >= 60 && a < 70)System.out.println('D');
else System.out.println('D');
}
}
第10題
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String a = scanner.nextLine();
System.out.println(a.length());
for(int i = 0;i < a.length() - 1;i++)
{
char s = a.charAt(i);
System.out.print(s + " ");
}
System.out.print(a.charAt(a.length() - 1));
System.out.println();
for(int j = a.length() - 1;j >= 0;j--)
{
char s = a.charAt(j);
System.out.print(s);
}
}
}