赛前第一周作业

No:1 区间k大数查询

点击标题查看题目

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
#include<bits/stdc++.h>
using namespace std;

bool cmp(const int a,const int b) {
return a>b;
}
int check(int l,int r,int k,int obj[]) {
int obj2[r-l+1];
int cnt=0;
for(int i=l-1; i<r; i++) {
obj2[cnt]=obj[i];
cnt++;
}
sort(obj2,obj2+cnt+1,cmp);
cout<<obj2[k-1]<<endl;

}
int main() {
int lenth,tot;
cin>>lenth;
int list[lenth];
for(int i=0; i<lenth; i++) {
int a;
cin>>list[i];
}
cin>>tot;
while(tot--) {
int l,r,k;
cin>>l>>r>>k;
check(l,r,k,list);
}
return 0;
}

No:2 素因子去重

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 p=1;
//unsigned int n; 数据范围10^12
long long n;
cin>>n;
for(long long i=2; i<=n; i++) {
if(n%i==0) {
p*=i;
while(n%i==0) {
n/=i;
}
}
}
cout<<p;
return 0;
}

No:3 表达式计算

1
没写出来,暂时思路应该是中缀表达式转后缀表达式

No:4 A. Wrong Subtraction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Codeforces 977A
#include <bits/stdc++.h>
using namespace std;

int check(int x,int y) {
while(y--) {
if((x%10)!=0) {
x--;
//cout<<x<<endl;
continue;
} else if((x%10)==0) {
x/=10;
}
}
return x;
}

int main(int argc, char** argv) {
int n,m;
cin>>n>>m;
cout<<check(n,m);
return 0;
}

No:5 出现次数最多的整数

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
AC Testcase 70%
#include<bits/stdc++.h>
using namespace std;

int main() {
int n;
cin>>n;
if(n<=0) {
return 0;
}
int list[n];
int cnt[n]= {0};
int maxk=0;
for(int i=0; i<n; i++) {
cin>>list[i];
}
for(int i=0; i<n-1; i++) {
if(list[i]==list[i+1]) {
cnt[maxk]+=1;
} else {
maxk=i+1;
}
}
// for(int i=0; i<n; i++) {
// cout<<cnt[i]<<" "<<endl;
// }
int maxn=-1;
for(int i=0; i<n; i++) {
if(cnt[i]>maxn) {
maxn=i;
}
}
//cout<<maxn;
cout<<list[maxn];
return 0;
}

No:6 特殊回文数

优化了暴力求解。210^6 -> 210^3

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 argc, char** argv) {
int n;
cin >> n;
int sum;
for(int i=1; i<=9; i++) {
for(int j=0; j<=9; j++) {
for(int k=0; k<=9; k++) {
if(2*(i+j)+k==n) {
cout<<i*10001+j*1010+k*100<<endl;
}
}
}
}
for(int i=1; i<=9; i++) {
for(int j=0; j<=9; j++) {
for(int k=0; k<=9; k++) {
if(2*(i+j+k)==n) {
cout<<i*100001+j*10010+k*1100<<endl;
}
}
}
}
}

No:7 寻找比目标字母大的最小字母

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
char nextGreatestLetter(vector<char>& letters, char target) {
int left = 0;
int right = letters.size()-1;
if (target >= letters[right]) {
return letters[0];
}
while (left < right) {
int mid = left + (right - left) / 2;
if (target >= letters[mid]) {
left = mid + 1;
}
else {
right = mid;
}
}
return letters[left];
}
};

No:8 大数取模

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//(a+b)%n =(a%n+b%n)%n
//(a-b)%n = (a%n-b%n)%n
int mod(char str[],int num){
int number[MAXN],i,d = 0;
int len = strlen(str);
for(i = 0; i < len; i++) {
number[i]=str[i]-'0';
}
int remainder=0;
for(i = 0; i < len; i++) {
remainder=(remainder * 10 + number[i]) % num;
}
return remainder;
}

No:9 勾股数

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()
{
for(int a=1;a<=1000;a++)
{
for(int b=a;b<=1000;b++)
{
for(int c=b;c<=1000;c++)
{
if(a*a+b*b==c*c&&a+b+c<=1000)
cout<<a<<" "<<b<<" "<<c<<endl;
}
}
}
return 0;
}

No10 Excel地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//26进制
#include<bits/stdc++.h>
using namespace std;

int main() {
long long n;
string address;
cin>>n;
while(n!=0) {
if(n%26) {
address+='A'+n%26-1;
}else {
address+='Z';
}
n--;
n/=26;
}
for(int i=address.length()-1; i>-1; i--) {
cout<<address[i];
}
return 0;
}
Author: Junzhou Liu
Link: https://liujunzhou.top/2019/3/3/week1/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
AliPay
WechatPay