OK呀!家人们我又来了 , 想我了没 ?及时更新哦。话不多数,我们直接开始!
第十六题 求平均年龄
题目描述:
班上有学生若干名,给出每名学生的年龄(整数),求班上所有学生的平均年龄,保留到小数点后两位。
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt(); // 学生人数
int age; // 每个学生的年龄
double ans = 0; // 平均年龄,初始值为0
/* 从第1个学生开始,到第n个学生为止 */
for (int i = 1; i <= n; i++) {
age = input.nextInt(); // 首先输入当前学生的年龄
ans = ans + age; // 然后将当前学生的年龄累加到ans上
}
ans = ans / n; // 求平均年龄
System.out.printf("%.2f", ans); // 保留两位小数输出ans
}
}
第十七题 整数序列的元素最大跨度值
题目描述:
给定一个长度为n的非负整数序列,请计算序列的最大跨度值(最大跨度值 = 最大值减去最小值)。
输入
一共2行,第一行为序列的个数n(1 <= n <= 1000),第二行为序列的n个不超过1000的非负整数,整数之间以一个空格分隔。
输出
输出一行,表示序列的最大跨度值。
代码:
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
int [] a= new int[n];
for(int i = 0 ; i <a.length ; i++)
{
a[i] = sc.nextInt();
}
Arrays.sort(a);
System.out.print(a[n-1] - a[0]);
sc.close();
}
}
第十八题 级数求和
题目描述:
已知:Sn= 1+1/2+1/3+…+1/n。显然对于任意一个整数K,当n足够大的时候,Sn大于K。
现给出一个整数K(1<=k<=15),要求计算出一个最小的n;使得Sn>K。
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int K = input.nextInt();
double Sn = 0;
int n = 0;
while (Sn <= K) {
n++;
Sn = Sn + 1.0 / n;
}
System.out.print(n);
input.close();
}
}
第十九题 画矩形
题目描述:
根据参数,画出矩形。
输入:
输入一行,包括四个参数:前两个参数为整数,依次代表矩形的高和宽(高不少于3行不多于10行,宽不少于5列不多于10列);第三个参数是一个字符,表示用来画图的矩形符号;第四个参数为1或0,0代表空心,1代表实心。
输出:
输出画出的图形。
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n , m;
n = input.nextInt();
m = input.nextInt();
char a = input.next().charAt(0);
int b ;
b = input.nextInt();
for(int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= m ; j++)
{
if(b == 0)
{
if(i == 1 || i == n || j == 1 || j == m )
{
System.out.print(a);
}
else {
System.out.print(' ');
}
}
if(b == 1)
{
System.out.print(a);
}
}
System.out.println();
}
input.close();
}
}
第二十题 与7无关的数
题目描述:
一个正整数,如果它能被7整除,或者它的十进制表示法中某一位上的数字为7,则称其为与7相关的数.现求所有小于等于n(n < 100)的与7无关的正整数的平方和.
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int t; // 暂存量
int ans = 0; // 与7无关的正整数的平方和
for (int i = 1; i <= n; i++) {
t = i; // 将当前数i暂存于t中
if (t % 7 != 0)
{
while (t != 0)
{
if (t % 10 == 7)
{
break;
}
else
{
t = t / 10;
}
}
if (t == 0)
{
ans += i * i;
}
}
}
System.out.print(ans);
input.close();
}
}
第二十一题 输出n以内的所有质数
题目描述:
输出n以内的所有质数。
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for(int i = 2 ; i <= n ;i++)
{
boolean is = true;
for(int j = 2 ; j<i; j++)
{
if(i % j == 0)
{
is = false;
break;
}
}
if(is)
{
System.out.print(i + " ");
}
}
input.close();
}
}
第二十二题 正整数位数判定并统计有多少个素数
题目描述:
程序设计基础实验要考试了,lcr发现有n个正整数,希望同学们帮忙统计一下,一共有多少个素数。输出每一个素数并输出该素数有多少位。
代码:
import java.util.Scanner;
public class Main {
//判断素数
public static boolean isPrime(int num) {
if (num < 2) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
//判断数位
public static int geshu(int numb) {
int count = 0;
while (numb != 0) {
numb /= 10;
count++;
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = sc.nextInt();
}
int p = 0;
for (int i = 0; i < n; i++) {
if (isPrime(a[i])) {
p++;
}
}
System.out.println(p);
for (int i = 0; i < n; i++) {
if (isPrime(a[i])) {
System.out.println(a[i] + " " + geshu(a[i]));
}
}
sc.close();
}
}
第二十三题 求最大公约数问题
题目描述:
给定两个正整数,求它们的最大公约数。
本题采用了辗转相除法!
代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int t;
if (m>=n)
{
t=n;
}
else
{
t=m;
}
while (true)
{
if (m%t==0)
{
if (n%t==0)
{
System.out.println(t);
break;
}
else
{
t = t - 1;
}
}
else
{
t=t-1;
}
}
sc.close();
}
}
总结:
这是本次更新的学校OJ的一些题目, 请大家借鉴 , 并批评指正!有好的建议请打在评论区 ,我会积极改正!