文章目录
P2433 【深基1-2】小学数学 N 合一
P5709 【深基2.习6】Apples Prologue / 苹果和虫子
P5710 【深基3.例2】数的性质
P5711 【深基3.例3】闰年判断
P5712 【深基3.例4】Apples
P5713 【深基3.例5】洛谷团队系统
P5714 【深基3.例7】肥胖问题
P5715 【深基3.例8】三位数排序
P5716 【深基3.例9】月份天数
P1085 [NOIP2004 普及组] 不高兴的津津
P1909 [NOIP2016 普及组] 买铅笔
P5717 【深基3.习8】三角形分类
P1422 小玉家的电费
P1424 小鱼的航程(改进版)
P1888 三角函数
P1046 [NOIP2005 普及组] 陶陶摘苹果
P4414 [COCI2006-2007#2] ABC
P1055 [NOIP2008 普及组] ISBN 号码
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a, b, c, d;
double e, f;
double pi = 3.141593;
switch (n) {
case 1:
System.out.println("I love Luogu!");
break;
case 2:
a = 2 + 4;
b = 10 - a;
System.out.printf("%d %d", a, b);
break;
case 3:
a = 14 / 4;
b = a * 4;
c = 14 - b;
System.out.println(a);
System.out.println(b);
System.out.println(c);
break;
case 4:
e = 500;
f = 3;
BigDecimal g = new BigDecimal(String.valueOf(e / f));
MathContext mc = new MathContext(6);// 有效数字位数位6
System.out.println(g.divide(BigDecimal.ONE, mc));// 让e/f的结果除1,并且保留6位小数
break;
case 5:
a = 260 + 220;
b = 12 + 20;
c = a / b;
System.out.println(c);
break;
case 6:
e = Math.sqrt(6 * 6 + 9 * 9);
System.out.printf("%.4f", e);
break;
case 7:
a = 100 + 10;
b = a - 20;
c = 0;
System.out.printf("%d\n%d\n%d", a, b, c);
break;
case 8:
int r = 5;
e = 2 * r * pi;
f = pi * r * r;
double v = (4 / 3.0) * pi * r * r * r;
System.out.printf("%.4f\n%.4f\n%.3f", e, f, v);
break;
case 9:
a = (((2 * 2) + 1) * 2 + 1) * 2;
System.out.println(a);
break;
case 10:
System.out.println(9);
break;
case 11:
e = 100 / 3.0;
System.out.printf("%.4f", e);
break;
case 12:
a = 'A';
b = 'M' - 'A' + 1;
char q = (char) (a + 18 - 1);
System.out.println(b);
System.out.println(q);
break;
case 13:
e = (4 / 3.0 * 4 * 4 * 4 * pi) + (4 / 3.0 * 10 * 10 * 10 * pi);
f = Math.cbrt(e);
System.out.println((int) (Math.floor(f)));
break;
case 14:
a = 110;
b = 10;
c = d = 0;
a--;
for (; a > 0; a--) {
b++;
if (a * b > 3500)
c = a;
if (a * b < 3500 && a < c) {
d = a;
if ((d - 1) * (b + 1) < 3500)
break;
}
}
int b1 = 120 - c;
int b2 = 120 - d;
int b3 = (b1 + b2) / 2;
int w;
if (b3 * (c + d) / 2 > 3500)
w = d;
else if (b3 * (c + d) / 2 < 3500)
w = c;
else
w = (c + d) / 2;
System.out.println(w);
break;
}
}
}
Tips:题目中提到了cout,在c++中cout的默认输出是保留六位有效数字,大家可以注意一下。
还有就是这里面用到了高精度的东西,这个东西到了【算法1-1】详细说
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
float b = sc.nextFloat();
int c = sc.nextInt();
if (b == 0) {
b = a;
a = 0;
if(c == 0)
a = (int)b;
} else {
a -= Math.ceil(c / b);
if (a < 0)
a = 0;
}
System.out.println(a);
}
}
这题改了?我怎么记得我做的时候没有说是三个非负整数 (x_x)
注意点:如果吃一个苹果需要0min,但时间只过去0min,那么它一个苹果也没吃上(
但如果时间比0min稍微多一点,哪怕多一秒,他也能把苹果都吃完,毕竟一个苹果只需要吃0min
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x[] = new int[4];//x数组分别表示四个人
x[0] = x[1] = 0;
x[2] = 1;
x[3] = 2;
int a = sc.nextInt();
if ((a%2) == 0) {
x[0] += 1;
x[1] += 2;
x[2] += 3;
x[3] = 0;
}
if(a>4 && a<=12) {
x[0] += 1;
x[1] += 2;
x[2] += 3;
x[3] = 0;
}
for(int i = 0;i
我承认我这题当初做的是凑出来的,所以我的代码大家应该看不太懂,我稍微解释一下,看看大家能不能对上我的电波喵(
先不看初始值,我们先分析题干,第一个人喜欢两个条件同时成立,不妨假设满足一个条件,这个人对应的数组值就+1,那么当且仅当x[0] == 2的时候才满足需求;
这里可以看出来,按照这样来看,当个人的值为奇数1时,他就只满足一个条件,有了这个思路我们再去看第二个人,他喜欢至少符合一种,也就是只满足一个,或者两个都满足,在这种情况下,如果每个题目还是只+1的话,那么还是1,2都有可能出现,显然,出现奇数1并不方便我们之后的判断,最好是符合要求的都是偶数,故每项+2,保证出现偶数,就是满足条件
第三个人只喜欢满足一个条件,为了凑出偶数,只能将初始值设定为奇数,每个条件加的值为偶数即可,这里 x[2]+=1也可
第四个人一个都不喜欢,这个好办,只要让他初始满足偶数,只要满足一个条件就让他不符合条件即可,最简单的就是只要满足一个条件就让值为0,不用再去考虑加减的问题了
以上就是这个题的思路,总体来看就是用奇偶来区分
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if (((a % 4 == 0) && (a % 100 != 0)) || a % 400 == 0)
System.out.println(1);
else
System.out.println(0);
}
}
经典老题:
闰年判断条件:
能被400整除,或者能被4整除但不能被100整除的都是闰年,其余的年份均为平年
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if(a <= 1)
System.out.printf("Today, I ate %d apple.",a);
else
System.out.printf("Today, I ate %d apples.",a);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = a*5;
int c = 11 + a*3;
String d;
d = b
计算两种方法分别耗时即可
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
float a = sc.nextFloat();
float b = sc.nextFloat();
float c = (a/(b*b))*10;
if(c<185)
System.out.println("Underweight");
else if(c < 240)
System.out.println("Normal");
else
System.out.printf("%.4f\nOverweight",c/10);
}
}
根据提示计算,BMI不会超过100,最高为61左右,故保留6为有效数字就是保留四位小数,浮点数计算会丢失精度,故乘10后比较
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[3];
for (int n = 0; n < 3; n++) {
arr[n] = sc.nextInt();
}
for (int i = 0; i < 2; i++) {
for (int k = 0; k < 2; k++) {
if (arr[k] > arr[k + 1]) {
int a = arr[k];
arr[k] = arr[k + 1];
arr[k + 1] = a;
}
}
if(i == 1)
System.out.printf("%d %d %d",arr[0],arr[1],arr[2]);
}
}
}
简单冒泡排序,其实Math.sort(),直接可以,但是初学者不推荐,还是手写一下比较好喵~
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
int month = sc.nextInt();
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(31);
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(30);
break;
}
if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
System.out.println(29);
}
else
System.out.println(28);
}
}
}
一三五七八十腊,三十一天永不差,只有二月二十八
闰年二月二十九天
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = 0;
int n = 0;
int[] arr = new int[14];
for (int i = 0; i < 14; i++)
arr[i] = sc.nextInt();
for (int k = 0; k < 7; k++) {
if (a < arr[2 * k] + arr[2 * k + 1]) {
a = arr[2 * k] + arr[2 * k + 1];
n = k;
}
}
if (a > 8)
System.out.println(n + 1);
else
System.out.println(0);
}
}
思路:让a始终等于最长的学习时间,n等于最长学习时间的数组序号
在判断a是否大于8,大于就说明这一周有不开心的天,否之则无
import java.math.MathContext;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b1 = sc.nextInt();
int c1 = sc.nextInt();
int b2 = sc.nextInt();
int c2 = sc.nextInt();
int b3 = sc.nextInt();
int c3 = sc.nextInt();
int d1 = (a % b1 == 0 ? a / b1 : a / b1 + 1) * c1;
int d2 = (a % b2 == 0 ? a / b2 : a / b2 + 1) * c2;
int d3 = (a % b3 == 0 ? a / b3 : a / b3 + 1) * c3;
int d4 = Math.min(d1, d2);
int d = Math.min(d4, d3);
System.out.println(d);
}
}
Tips:上次的【入门一】中讲过三目运算符了,不再赘述,这里的意思是,如果不能正好满足需要的数量,就再多买一包,再去比较这三种方案最少的价格
import java.math.MathContext;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int a1 = Math.max(a, Math.max(c, b));
int b1 = Math.min(a, Math.min(b, c));
int c1 = a + b + c - a1 - b1;
if (a1 >= b1 + c1)
System.out.println("Not triangle");
else {
if (a1 * a1 == b1 * b1 + c1 * c1)
System.out.println("Right triangle");
if (a1 * a1 < b1 * b1 + c1 * c1)
System.out.println("Acute triangle");
if (a1 * a1 > b1 * b1 + c1 * c1)
System.out.println("Obtuse triangle");
if (b1 == c1 || a1 == c1)
System.out.println("Isosceles triangle");
if (a1 == b1)
System.out.println("Equilateral triangle");
}
}
}
Tips:
两边之和小于第三边 ==> 不是三角形
根据余弦定理可知,
两边平方和<第三边平方 ==> 钝角
两边平方和>第三边平方 ==> 锐角
两边平方和==第三边平方 ==> 直角
最小边和另一边相等 ==> 等腰
最大边和最小边相等 ==> 等边
import java.math.MathContext;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
double b = 0;
if (a > 150)
if (a > 400)
b = 150 * 0.4463 + 250 * 0.4663 + (a - 400) * 0.5663;
else
b = 150 * 0.4463 + (a - 150) * 0.4663;
else
b = a * 0.4463;
b *= 10;
b = Math.round(b);
System.out.println(b / 10);
}
}
经典题,不再赘述,有问题评论区可以问
import java.math.MathContext;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = 0;
for (; b > 0; b--) {
if (a == 8)
a = 1;
if (a <= 5)
c += 250;
a++;
}
System.out.println(c);
}
}
周八就是周一
import java.math.MathContext;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = Math.min(a, Math.min(b, c));
int e = Math.max(a, Math.max(b, c));
int f = abc(d,e);
if(f==0)
f=1;
System.out.printf("%d/%d",d/f,e/f);
}
public static int abc(int a, int b) {
int c = Math.max(a, b);
b = a + b - c;
a = c;
if (a % b != 0)
return abc(a % b, b);
else
return b;
}
}
这题主要是约分,这里笔者在外面创建了个函数,这个函数是用来计算最大公约数的,内部逻辑是辗转相除法:
用较大数除以较小数,再用出现的余数(第一余数)去除除数,再用出现的余数(第二余数)去除第一余数,如此反复,直到最后余数是0为止。最后的除数就是这两个数的最大公约数。
import java.math.MathContext;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] arr = new int[10];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
arr[i] = sc.nextInt();
}
int a = sc.nextInt();
a+=30;
int n = 0;
for(int i = 0;i<10;i++) {
if(arr[i] <= a)
n++;
}
System.out.println(n);
}
}
import java.math.MathContext;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int a1, b1, c1;
String d = sc.next();
a1 = Math.max(a, Math.max(b, c));
b1 = Math.min(a, Math.min(b, c));
c1 = a + b + c - a1 - b1;
int[] arr = new int[3];
for (int i = 0; i < 3; i++) {
switch (d.charAt(i)) {
case 65:
arr[i] = b1;
break;
case 66:
arr[i] = c1;
break;
case 67:
arr[i] = a1;
break;
}
System.out.printf("%d ",arr[i]);
}
}
}
给大家再翻译一下题意,就是说有三个数,从小到大分别用ABC来表示,下一行额三个字母就表示了要输出的顺序,如CAB就是大小中,ABC就是小中大,大家应该能明白喵~
import java.math.MathContext;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
int a = 0;
int n = 0;
for (int i = 0; i < 11; i++) {
if (str.charAt(i) == '-')
continue;
else {
n++;
a += ((int) str.charAt(i) - '0') * n;
}
}
String c = a%11+"";
if ((int) str.charAt(str.length() - 1) == ((a%11 + '0')>'9'?'X':(a%11+'0')))
System.out.println("Right");
else
System.out.printf("%11.11s-%s", str, c.equals("10")?"X":c);
}
}
在进行运算时,字符型会隐式转换成整形去参与运算。
str1.equals(String str2)---可用来比较两个字符串是否相等(在Object超类中,equals用来比较两个地址是否相等,但在一些类库如String,Integer,Data中这个方法被重写了,改为比较数据值是否相等)
有问题欢迎大家来评论区留言
我们下次见喵~