0%

洛谷刷题记录(1)(1,2,3组)

安全培训中途插入了算法…不过算法还是基础,很有用的,需要认真学。记录一下做题记录,这次是1-3组。

ps.简单题目就只给出代码不解释了,复杂的会做一些解释。

pss.题目使用的语言不同,主要是c,c++和python

0x01 顺序结构

首先是顺序结构。顺序结构作为程序的最基本结构,题目难度不大,就当复习基础了。

A+B Problem

1
2
3
4
5
6
7
8
#include <stdio.h>
int main(){
long int a,b;
scanf("%ld %ld", &a, &b);
long int res = a + b;
printf("%ld", res);
return 0;
}

超级玛丽游戏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
                ********
************
####....#.
#..###.....##....
###.......###### ### ###
........... #...# #...#
##*####### #.#.# #.#.#
####*******###### #.#.# #.#.#
...#***.****.*###.... #...# #...#
....**********##..... ### ###
....**** *****....
#### ####
###### ######
##############################################################
#...#......#.##...#......#.##...#......#.##------------------#
###########################################------------------#
#..#....#....##..#....#....##..#....#....#####################
########################################## #----------#
#.....#......##.....#......##.....#......# #----------#
########################################## #----------#
#.#..#....#..##.#..#....#..##.#..#....#..# #----------#
########################################## ############

注:php代码在<?php 头之外的会被php解释器直接原模原样输出。

【深基2.例5】苹果采购

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
int main(){
long int a,b;
long long c;
scanf("%ld %ld",&a, &b);
c = a * b;
printf("%lld",c);
return 0;

}

【深基2.例6】字母转换

1
2
3
4
5
6
7
#include <stdio.h>
int main(){
char c;
scanf("%c",&c);
printf("%c", c-32);
return 0;
}

【深基2.例7】数字反转

1
print(str(input())[::-1])

注:[::-1]是python里的反转字符串最简单的方法。

【深基2.例8】再分肥宅水

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
int main(){
float a;
int b;
float c;
scanf("%f %d", &a, &b);
c = a / b;
printf("%.3f\n%d",c,2*b);
return 0;
}

小鱼的游泳时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
int main(){
int a,b,c,d;
scanf("%d %d %d %d",&a, &b, &c, &d);
int resh, resm;
resh = c - a;
resm = d - b;
if (resm < 0){
resh--;
resm += 60;
}
printf("%d %d",resh, resm);
return 0;
}

【深基1-2】小学数学 N 合一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;
int main(){
int T;
cin >> T;
if (T == 1) {
// 粘贴问题 1 的主函数代码,除了 return 0
cout << "I love Luogu!";
} else if (T == 2) {
// 粘贴问题 2 的主函数代码,除了 return 0
cout << 2 + 4 << " " << 10 - 2 - 4;
} else if (T == 3) {
// 请自行完成问题 3 的代码
cout << 3 << endl << 12 << endl << 2 << endl;
} else if (T == 4) {
cout << 166.667 << endl;
// 请自行完成问题 4 的代码
} else if (T == 5) {
cout << 15 << endl;
// 请自行完成问题 5 的代码
} else if (T == 6) {
cout << 10.816654 << endl;
// 请自行完成问题 6 的代码
} else if (T == 7) {
cout << 110 << endl << 90 << endl << 0 << endl;
// 请自行完成问题 7 的代码
} else if (T == 8) {
cout << 31.41593 << endl;
cout << 78.539825 << endl;
cout << 523.598833 << endl;
// 请自行完成问题 8 的代码
} else if (T == 9) {
cout << 22 << endl;
// 请自行完成问题 9 的代码
} else if (T == 10) {
cout << 9 << endl;
// 请自行完成问题 10 的代码
} else if (T == 11) {
cout << 33.333333 << endl;
// 请自行完成问题 11 的代码
} else if (T == 12) {
cout << 13 << endl;
cout << "R" << endl;
// 请自行完成问题 12 的代码
} else if (T == 13) {
cout << 16 << endl;
// 请自行完成问题 13 的代码
} else if (T == 14) {
cout << 50 << endl;
// 请自行完成问题 14 的代码
}
return 0;
}

【深基2.习2】三角形面积

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <math.h>
int main(){
float a,b,c;
scanf("%f %f %f",&a, &b, &c);
float p = (a+b+c)/2;
printf("%.1f", sqrt(p*(p-a)*(p-b)*(p-c)));
return 0;
}

小玉买文具

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
int main(){
int a,b;
scanf("%d %d",&a,&b);
float tmp = a + 0.1 * b;
float res = tmp/1.9;
printf("%d", (int)res);
return 0;

}

【深基2.习6】Apples Prologue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
int main(){
int m,t,s;
scanf("%d %d %d",&m,&t,&s);
if (t == 0){
printf("0");
return 0;
}
if (s % t == 0){
if (m < s/t) printf("0");
else printf("%d", m-s/t);
} else {
if (m <= s/t) printf("0");
else printf("%d", m-s/t-1);
}
return 0;
}

对角线

1
2
n = input()
print(n*(n-1)*(n*n-5*n+6)/24)

观察之后可以发现,每两条对角线可以确定一个交点,同时联系起四个顶点构成了一个四边形。因此寻求交点个数可以等价为——找到所有顶点中任意四个顶点的组合情况,即:
$$
C_n^4 = \frac{n!}{4!(n-4)!} = \frac{n(n-1)(n-2)(n-3)}{4!}
$$

0x02 选择结构

选择结构,即程序中存在一些需要进行判断的部分,通过判断结果的不同执行不同的程序。

【深基3.例2】数的性质

1
2
3
4
5
6
7
8
#include <stdio.h>
int main(){
int x;
scanf("%d", &x);
_Bool a = !(x & 1), b = (x > 4 && x <= 12);
printf("%d %d %d %d", a & b, a | b, (a && !b || b && !a), !a && !b);
return 0;
}

通过布尔值和位运算进行判断比较方便些。

【深基3.例3】闰年判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int main(){
int year;
cin >> year;
if (year % 100 != 0){
if (year % 4 == 0) cout << "1" << endl;
else cout << "0" << endl;
} else {
if (year % 400 == 0) cout << "1" << endl;
else cout << "0" << endl;
}
return 0;
}

【深基3.例4】Apples

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main(){
int num;
cin >> num;
if (num <= 1) cout << "Today, I ate " << num << " apple." << endl;
else cout << "Today, I ate " << num << " apples." << endl;
return 0;
}

【深基3.例5】洛谷团队系统

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int main(){
int a;
cin >> a;
if (a <= 5) cout << "Local" << endl;
else cout << "Luogu" << endl;
return 0;
}

【深基3.例7】肥胖问题

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main(){
int weight;
float height;
cin >> weight >> height;
float res = weight / height / height;
if (res < 18.5) cout << "Underweight" << endl;
else if (res >= 18.5 && res < 24 ) cout << "Normal" << endl;
else cout << res << endl << "Overweight" << endl;
return 0;
}

【深基3.例8】三位数排序

1
2
3
4
5
6
7
8
9
#include<bits/stdc++.h>
using namespace std;
int main(){
int s[3];
cin>>s[0]>>s[1]>>s[2];
sort(s,s+3);
cout<<s[0]<<' '<<s[1]<<' '<<s[2];
return 0;
}

【深基3.例9】月份天数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main(){
int y,m;
cin >> y >> m;
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) cout << "31" << endl;
else if (m == 4 || m == 6 || m == 9 || m == 11) cout << "30" << endl;
else {
if (y % 100 != 0){
if (y % 4 == 0) cout << "29" << endl;
else cout << "28" << endl;
} else {
if (y % 400 == 0) cout << "29" << endl;
else cout << "28" << endl;
}
}
return 0;

}

不高兴的津津

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <bits/stdc++.h>
using namespace std;
int main(){
int a[7][2];
int res[7];
int m = 0;
for(int i = 0; i < 7; i++){
cin >> a[i][0] >> a[i][1];
res[i] = a[i][0] + a[i][1];
}
for(int i = 0; i < 7; i++){
if (res[i] > res[m]) m = i;
}
cout << m + 1 << endl;
}

买铅笔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
int ceil(int num, int p){
if (num % p == 0) return num / p;
else return (int)(num / p) + 1;
}

int main(){
int num;
cin >> num;
int p[3][2];
int res[3];
for(int i = 0; i < 3; i++){
cin >> p[i][0] >> p[i][1];
res[i] = ceil(num, p[i][0]) * p[i][1];
}

int output = res[0];
for(int i = 0; i < 3; i++){
if (res[i] < output) output = res[i];
}
cout << output << endl;
return 0;


}

ISBN号码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
stri = raw_input()
res = 0
m = 1
for i in range(0, 12):
if stri[i] != "-":
res = res + int(stri[i]) * m
m = m + 1
res = res % 11

if res == 10 and stri[12] == "X":
print("Right")
elif str(res) == stri[12]:
print("Right")
else:
newstr = list(stri)
if res == 10:
newstr[12] = "X"
else:
newstr[12] = str(res)
print(''.join(newstr))

小玉家的电费

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
int main(){
int num;
float res;
scanf("%d", &num);
if (num <= 150) res = num * 0.4463;
else if (num > 150 && num <= 400) res += 150 * 0.4463 + (num - 150) * 0.4663;
else res += 150 * 0.4463 + 250 * 0.4663 + (num - 400) * 0.5663;
printf("%.1f", res);

return 0;

}

小鱼的航程(改进版)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main(int argc, char** argv)
{
int x, n;
cin >> x >> n;
int d = n / 7 * 5;
int r = n % 7;
if (r > 0)
{
if (r + x == 7 || x == 7)
r -= 1;
else if (r + x >= 8)
r -= 2;
}
cout << (d + r) * 250 << endl;
return 0;
}

三角函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;
int gcd(int a, int b){
int r;
while(1){
r = a % b;
if (r == 0) break;
a = b;
b = r;
}
return b;
}
int main(){
int a[3];
cin >> a[0] >> a[1] >> a[2];
int max = a[0],min = a[0];
for(int i = 0; i < 3; i++){
if (a[i] > max) max = a[i];
if (a[i] < min) min = a[i];
}
int g = gcd(min, max);
min /= g;
max /= g;
cout << min << "/" << max << endl;
return 0;
}

注:求最大公约数的主要操作方法是:

1
2
3
4
5
6
7
8
9
10
int gcd(int a, int b){
int r;
while(1){
r = a % b;
if (r == 0) break;
a = b;
b = r;
}
return b;
}

陶陶摘苹果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int main(){
int a[10];
int h;
int res = 0;
for(int i = 0; i < 10; i++){
cin >> a[i];
}
cin >> h;
for(int i = 0; i < 10; i++){
if (h + 30 >= a[i]) res++;
}
cout << res << endl;
return 0;
}

【深基3.习8】三角形分类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;
int main(){
long long int a[3];
cin >> a[0] >> a[1] >> a[2];
sort(a, a + 3);
if (a[0] + a[1] < a[2]) {
cout << "Not triangle" << endl;
return 0;
}

if (a[0] * a[0] + a[1] * a[1] > a[2] * a[2]) cout << "Acute triangle" << endl;
if (a[0] * a[0] + a[1] * a[1] == a[2] * a[2]) cout << "Right triangle" << endl;
if (a[0] * a[0] + a[1] * a[1] < a[2] * a[2]) cout << "Obtuse triangle" << endl;
if (a[0] == a[1] || a[1] == a[2]) cout << "Isosceles triangle" << endl;
if (a[0] == a[1] && a[1] == a[2]) cout << "Equilateral triangle" << endl;
return 0;
}

[COCI2006-2007#2] ABC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;
int main(){
int a[3], b[3];
cin >> a[0] >> a[1] >> a[2];
sort(a, a + 3);
string ord;
cin >> ord;
for(int i = 0; i < 3; i++){
if (ord[i] == 'C') b[i] = 2;
if (ord[i] == 'B') b[i] = 1;
if (ord[i] == 'A') b[i] = 0;
}
for(int i = 0; i < 2; i++){
cout << a[b[i]] << " ";
}
cout << a[b[2]] << endl;
return 0;
}

注:一一对应的思想。

0x03 循环结构

循环结构顾名思义,主要就是讲程序中存在大量重复性操作的语句进行封装,使用循环语句完成操作,优化代码结构,提高代码可读性。

【深基4.例2】找最小值

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>
using namespace std;
int main(){
int num, *a;
cin >> num;
a = (int*)malloc(sizeof(int) * num);
for(int i = 0; i < num; i++){
cin >> a[i];
}
sort(a, a + num);
cout << a[0] << endl;
return 0;
}

【深基4.例3】分类平均

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <bits/stdc++.h>
using namespace std;
int main(){
int a,b,sum = 0,num = 0;
cin >> a >> b;
for(int i = 1; i <= a; i++){
sum += i;
if (i % b == 0) num++;
}
float res1, res2;
res1 = (float)b / 2 * num + (float)b / 2;
res2 = (sum - res1 * num) / (a - num);
printf("%.1f %.1f", res1, res2);
return 0;
}

【深基4.例4】一尺之棰

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>
using namespace std;
int main(){
long long int a;
int res = 0;
cin >> a;
while (a != 1) {
a /= 2;
res++;
}
cout << res+1 << endl;
return 0;
}

【深基4.例6】数字直角三角形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
int cnt = 0;
cin >> n;
int num = n * (n + 1) / 2;
for(int i = 1; i <= num; i++){
printf("%02d", i);
cnt++;
if (cnt == n){
cout << endl;
cnt = 0;
n--;
}
}
return 0;
}

阶乘之和

1
2
3
4
5
6
7
import math

n = input()
res = 0
for i in range (1, n+1):
res += math.factorial(i)
print(res)

注:注意使用python标准库中的阶乘函数math.factorial()

计数问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,x,m=1,ans=0;
scanf("%d%d",&n,&x);
while(m<=n){
int a=n/(m*10),b=n/m%10,c=n%m; //a,b,c为n的三部分,求哪一位x的个数,b就为那一位数,a为b左边的数,c为b右边的数,如求1~728中十位7的个数,则a=7,b=2,c=8
if(x){
if(b>x) ans+=(a+1)*m; //如果b>x,说明有(a+1)*m个x(如求1~728中个位7的个数,则为(72+1)*1=73)
if(b==x) ans+=a*m+c+1; //如果b=x,说明有a*m+c+1个x(如求1~728中百位7的个数,则为0*100+28+1=29)//如果b<x,说明有a*m个x(如求1~728中十位7的个数,则为7*10个)
if(b<x) ans+=a*m;
}
else{ //x=0的情况和x!=0的情况有所不同
if(b) ans+=a*m;
else ans+=(a-1)*m+c+1;
}
m*=10;
}
printf("%d\n",ans);
return 0;
}

注:这个题有些意思,值得思考。

我们假设n=728,x=7

可以按照这样的思路计算结果:

个位7:73个 7,17,…,727

十位7:70个 7079,170179,…,670~679

百位7:29个 700~728

答案是172。

从而我们可以考虑通过遍历数字的每一位来进行结果的计算,时间复杂度降到了logn。具体思路见注释。

级数求和

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>
using namespace std;
int main(){
int k, i = 1;
cin >> k;
double tmp = 0;
while(tmp <= k){
tmp += (double)1/i;
i++;
}
cout << i-1 << endl;
return 0;
}

金币

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <bits/stdc++.h>
using namespace std;
int main(){
int k;
cin >> k;
int n = 0;
while (n*(n+1)/2 < k) n++;
n--;
long long int sum1 = n*(n+1)*(2*n+1)/6;
int tmp = k - n*(n+1)/2;
long long int sum2 = tmp * (n+1);
cout << sum1 + sum2 << endl;
return 0;
}

注意n²的求和公式的使用,简化程序。

【深基4.例11】数列求和

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
long long int res = 0;
for(int i = 1;i <= n; i++){
res += i;
}
cout << res << endl;
return 0;
}

不让用求和公式…那就一个个加吧。

【深基4.例13】质数口袋

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <bits/stdc++.h>
using namespace std;
bool check(int n){
for(int i = 2; i <= sqrt(n); i++){
if (n % i == 0) return false;
}
return true;
}
int main(){
int k, res = 0, sum = 0;
cin >> k;
if (k == 1){
cout << "0" << endl;
return 0;
}
for(int i = 2 ;; i++){
if (check(i)) {
sum += i;
if (sum <= k){
cout << i << endl;
res++;
} else break;
}
}
cout << res << endl;
return 0;
}

注意判断质数的方法,只需要遍历到它的1/2次方即可。

[USACO1.5]回文质数 Prime Palindromes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <bits/stdc++.h>
using namespace std;
bool checkPrime(long long int n){
for(int i = 2; i <= sqrt(n); i++){
if (n % i == 0) return false;
}
return true;
}

bool checkPalin(int n){
string s = to_string(n);
int m = 0;
int q = s.size() - 1;
while(m <= q){
if (s[m] == s[q]) {
m++;
q--;
} else return false;
}
return true;
}

int main(){
int a, b;
cin >> a >> b;
if (b <= 10000000){ // 回文质数不存在偶数的情况,除了11.
for(int i = a; i <= b; i++){
if (i % 2 == 0) continue;
if (checkPalin(i)){
if (checkPrime(i))cout << i << endl;
}
}
} else {
for(int i = a; i <= 10000000; i++){
if (checkPalin(i)){
if (checkPrime(i))cout << i << endl;
}
}
}
return 0;
}

基础做法,加上一些小trick,险ac。

小玉在游泳

1
2
3
4
5
6
7
8
9
10
#include <bits/stdc++.h>
using namespace std;
int main(){
float n;
int i = 0;
cin >> n;
while(100*(1-pow(0.98,i)) < n) i++;
cout << i << endl;
return 0;
}

数字反转

1
2
3
4
5
6
7
n1 = input()
n = str(n1)
n = n[::-1]
if n[len(n)-1] == '-':
n = n[:len(n)-1]
print("-",end="")
print(int(n))

月落乌啼算钱

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>
using namespace std;
int main(){
long long int n,a=1,b=1,c=0;
cin >> n;
for(int i = 3; i <= n; i++){
c = a + b;
a = b;
b = c;
}
cout << c << ".00" << endl;
return 0;
}

观察公式可以很容易发现是斐波那契数列,这里不能用递归否则会超内存,使用循环的方式会快很多。关键代码:

1
2
3
4
5
for(int i = 3; i <= n; i++){
c = a + b;
a = b;
b = c;
}

【深基4.习5】求极差

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
int *a = new int[n];
int min, max;
for(int i = 0; i < n; i++){
cin >> a[i];
if (i == 0) {
min = a[0];
max = a[0];
}
if (max < a[i]) max = a[i];
if (min > a[i]) min = a[i];
}
cout << max - min << endl;
return 0;

}

最长连号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, res = 0, tmp;
cin >> n;
int *a = new int[n];
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < n - 1; i++){
if (a[i] + 1 == a[i+1]) res++;
else {
if (tmp < res + 1) tmp = res + 1;
res = 0;
}
}
cout << tmp;
return 0;

}

质因数分解

1
2
3
4
5
6
7
8
9
10
11
12
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
for(int i = 2; i <= sqrt(n); i++){
if (n % i == 0) {
cout << n / i << endl;
return 0;
}
}
}

投机取巧了,考虑到题目要求中说只能分解成两个因数,那么找到了一个另一个除一下就出来了。

【深基4.习8】求三角形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
for(int i = 1; i <= n*n; i++){
printf("%02d", i);
if (i % n == 0) cout << endl;
}
cout << endl;
int flag = 1;
for(int i = 1; i <= n*(n+1)/2;){
for(int j = 1; j <= (n-flag)*2; j++) cout << " ";
for(int m = 0; m < flag; m++) {
printf("%02d", i);
i++;
}
cout << endl;
flag++;
}
return 0;
}

【深基4.习9】打分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
float *a = new float[n];
float min, max, sum = 0;
for(int i = 0; i < n; i++){

cin >> a[i];
if (i == 0){
min = a[0];
max = a[0];
}
if (min > a[i]) min = a[i];
if (max < a[i]) max = a[i];
sum += a[i];
}

float res = (sum-min-max)/(n-2);
printf("%.2f", res);
return 0;


}

[COCI2017-2018#6] Davor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
n /= 364;
if (n <= 103) cout << n - 3 << endl << 1 << endl;
else {
int k = (n - 101) / 3 + 1;
int x = n - 3 * k;
cout << x << endl << k << endl;
}
return 0;
}

津津的储蓄计划

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;
int main(){
int a[12], num = 0, tmp = 0, res = 0;
for(int i = 0; i < 12; i++){
cin >> a[i];
num += 300;
if (num - a[i] < 0) {
cout << "-" << i + 1 << endl;
return 0;
}
tmp = (num - a[i]) / 100;
num -= tmp * 100;
num -= a[i];
res += tmp * 100;
}
cout << num + res * 1.2 << endl;
return 0;
}

细心一点,模拟操作即可。