130道基础OJ编程题之: 68~77 - Rainbow
130道基础OJ编程题之: 68~77
@
目录
- 130道基础OJ编程题之: 68~77
- 68:BC72 平均身高
- 69:BC74 HTTP状态码
- 70:BC75 数字三角形
- 71:BC76 公务员面试
- 72:BC77 有序序列插入一个数
- 73:BC78 筛选法求素数
- 74: BC79 图像相似度
- 75: BC80 登录验证
- 76: BC85 包含数字9的数
- 77:BC86 奇偶统计
- 最后:
68:BC72 平均身高
平均身高_牛客题霸_牛客网
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
double sum = 0;
for(int i = 0; i < 5; i++) {
sum += in.nextDouble();
}
double avg = sum / 5;
System.out.println(String.format("%.2f",avg));
}
}
69:BC74 HTTP状态码
HTTP状态码_牛客题霸_牛客网
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int state = in.nextInt();
switch(state) {
case 200:
System.out.println("OK");
break;
case 202:
System.out.println("Accepted");
break;
case 400:
System.out.println("Bad Request");
break;
case 403:
System.out.println("Forbidden");
break;
case 404:
System.out.println("Not Found");
break;
case 500:
System.out.println("Internal Server Error");
break;
case 502:
System.out.println("Bad Gateway");
break;
}
}
}
}
70:BC75 数字三角形
数字三角形_牛客题霸_牛客网
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int n = in.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
}
71:BC76 公务员面试
公务员面试_牛客题霸_牛客网
import java.util.Scanner;
import java.util.Arrays;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int[] arr = new int[7];
int sum = 0;
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
sum += arr[i];
}
Arrays.sort(arr); // 排序
double averScoure = (sum - arr[0] - arr[arr.length - 1]) / 5.0;
System.out.printf("%.2f
", averScoure);
}
}
}
72:BC77 有序序列插入一个数
有序序列插入一个数_牛客题霸_牛客网
import java.util.Scanner;
import java.util.Arrays;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
int arr[] = new int[n+1];
for(int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
arr[n] = in.nextInt();
Arrays.sort(arr);
for(int num : arr) {
System.out.print(num + " ");
}
}
}
}
73:BC78 筛选法求素数
筛选法求素数_牛客题霸_牛客网
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
/*
判断该数是不是素数
*/
public static int isPrime(int n) {
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return 0; // 是素数
}
}
return 1; // 不是素数
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
int arr[] = new int[n];
for(int i = 2; i < n; i++) {
arr[i] = i;
}
int count = 1;
for(int i = 2; i < arr.length; i++) {
if(isPrime(arr[i]) == 1) {
System.out.printf("%d ",arr[i]);
} else {
count++;
}
}
System.out.printf("
%d
",count);
}
}
}
74: BC79 图像相似度
图像相似度_牛客题霸_牛客网
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
int[][] array = new int[m][n]; // 第一幅图
int[][] brray = new int[m][n]; // 第二幅图
// 读取第一幅图的像素
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
array[i][j] = in.nextInt();
}
}
// 读取第二幅图的像素
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
brray[i][j] = in.nextInt();
}
}
// 计算对比两幅图的黑白像素
double count = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (array[i][j] == brray[i][j]) {
count++;
}
}
}
// System.out.printf("%d
",count);
// System.out.printf("%.2f",count/(m*n)*100.0);
System.out.printf("%.2f", count / (m * n) * 100);
}
}
75: BC80 登录验证
登录验证_牛客题霸_牛客网
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String name = in.next();
String password = in.next();
if("admin".equals(name) && "admin".equals(password)) {
System.out.println("Login Success!");
} else {
System.out.println("Login Fail!");
}
}
}
}
76: BC85 包含数字9的数
包含数字9的数_牛客题霸_牛客网
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = 0;
for(int i = 0; i <= 2019; i++) {
int temp = i;
while(temp > 0) {
if( temp % 10 == 9) { // 判断该数是否含有 9
count++;
break;
}
temp = temp / 10; // 将数值向左移动,去掉,最后一个数值
}
}
System.out.println(count);
}
}
77:BC86 奇偶统计
奇偶统计_牛客题霸_牛客网
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int odd = 0; // 奇数的个数
int even = 0; // 偶数的个数
for (int i = 1; i <= num; i ++) { // 注意: 这里包括 num 本身
if(i % 2 == 0) { // 偶数
even++;
} else {
odd++;
}
}
System.out.printf("%d %d",odd,even);
}
}
最后:
“在这个最后的篇章中,我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉,我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底,继续在其他的领域奋斗。感谢你们,我们总会在某个时刻再次相遇。”
原文地址:https://www.cnblogs.com/TheMagicalRainbowSea/p/18756942