还有这么多莫名其妙的代码,终究还是写代码的时候懒得写文件名的缘故
[2.2.cpp]
#include<bits/stdc++.h>
using namespace std;
int main() {
// 1. 数字直接相减就不需要-’0‘;2. 看清楚只有一组测试用例还是多组,否则输入输出写的麻烦。2. 注意正向和反向的加10,以及为了防止溢出在取余的操作。
int n;
cin >> n;
string s, t;
cin >> s >> t;
int res = 0;
for (int i = 0; i < n; i++) {
res += min((s[i] - t[i] + 10) % 10, (t[i] - s[i] + 10) % 10);
}
cout << res << endl;
return 0;
}
[2.3.cpp]
#include<iostream>
using namespace std;
#define ok 0
#define error -1
class SeqList{
private:
int *list;
int maxSize;
int size;
public:
SeqList(){
maxSize=1000;
size=0;
list =new int[maxSize];
}
~SeqList(){
delete []list;
}
int list_size(){
return size;
}
int isEmpty(){
return size==0?1:0;
}
int list_front(){
return list[0];
}
void list_pop(){
for(int j=0;j<size;j++){
list[j]=list[j+1];
}
size--;
}
int list_push(int item){
list[size++]=item;
return ok;
}
void list_display(){
cout<<size<<" ";
for(int i=0;i<size;i++)
{
cout<<list[i]<<" ";
}cout<<endl;
}
};
int main(){
int l1,l2,item;
cin>>l1;
SeqList a;
for(int i=0;i<l1;i++){
cin>>item;
a.list_push(item);
}
SeqList b;
cin>>l2;
for(int i=0;i<l2;i++){
cin>>item;
b.list_push(item);
}
SeqList c;
while(c.list_size()!=l1+l2){
if(a.isEmpty()){
c.list_push(b.list_front());
b.list_pop();
}
else if(b.isEmpty()){
c.list_push(a.list_front());
a.list_pop();
}
else if(a.list_front()<=b.list_front()){
c.list_push(a.list_front());
a.list_pop();
}
else{
c.list_push(b.list_front());
b.list_pop();
}
}
c.list_display();
}
[2.4.cpp]
#include<iostream>
using namespace std;
#define ok 0
#define error -1
class SeqList{
private:
int *list;
int maxSize;
int size;
public:
SeqList(){
maxSize=1000;
size=0;
list =new int[maxSize];
}
~SeqList(){
delete []list;
}
int list_push(int item){
list[size++]=item;
return ok;
}
int mov(int op,int opn){
int temp[opn+1];
for(int i=0;i<opn;i++){
temp[i]=list[i];
}
for(int i=0;i<size-opn;i++){
list[i]=list[i+opn];
}
for(int i=size-opn,j=0;i<size;i++){
list[i]=temp[j++];
}
this->list_display();
}
void list_display(){
for(int i=0;i<size;i++)
{
cout<<list[i]<<" ";
}cout<<endl;
}
};
int main(){
int n,item,op,opn;
cin>>n;
SeqList s;
for(int i=0;i<n;i++){
cin>>item;
s.list_push(item);
}
s.list_display();
for(int i=0;i<2;i++)
{
cin>>op>>opn;
opn=opn%n;
if(op==0){
s.mov(op,opn);
}
else{
s.mov(op,n-opn);
}
}
}
[3.4.cpp]
#include<iostream>
using namespace std;
int Get_next(string a)
{
int len=a.size();
int i=0,k=-1;
int *next=new int[len+1];
next[0]=-1;
while(i<len)
{
if(k==-1||a[i]==a[k])
{
i++,k++;
next[i]=k;
}
else k=next[k];
}
if(next[len]*2>=len)
{
if(len%(len-next[len])==0)return 0;
else return (len-next[len])-len%(len-next[len]);
}
else return len-next[len]*2;
}
int main()
{
string a;
int n;
cin>>n;
while(n--)
{
cin>>a;
cout<<Get_next(a)<<endl;
}
return 0;
}
[A.cpp]
#include<iostream>
#include<queue>
using namespace std;
int main(){
queue<char> A;
queue<int> B;
int n,a,cnta=0,cntb=0,cntc=0,ca=0,cb=0,cc=0;
char c;
cin>>n;
for(int i=0;i<n;i++){
cin>>c;
A.push(c);
}
for(int i=0;i<n;i++){
cin>>a;
B.push(a);
}
while(n--){
switch(A.front()){
case 'A':{
ca++;
cnta+=B.front();
B.pop();
break;
}
case 'B':{
cb++;
cntb+=B.front();
B.pop();
break;
}
case 'C':{
cc++;
cntc+=B.front();
B.pop();
break;
}
}
A.pop();
}
cout<<cnta/ca<<endl<<cntb/cb<<endl<<cntc/cc;
}
[C.cpp]
#include<iostream>
#include<queue>
using namespace std;
int main()
{
int t,i,n;
int Group[10000];
int data;
cin>>t;
for(i=0;i<t;i++)
{
cin>>n;
while(n--)
{
cin>>data;
Group[data]=i;
}
}
queue<int> my_queue[100];//存储分段数据的queue
string ch;
int q;
while(1)
{
cin>>ch;
if(ch=="ENQUEUE")
{
cin>>q;
for(i=0;i<10;i++)//最多十组
if(my_queue[i].empty()||Group[q]==Group[my_queue[i].front()])//两个条件->如果自己组那行空着 或者 第i行有自己组成员-> my_queue具有先到先得原则
{
my_queue[i].push(q);
break;
}
}
else if(ch=="DEQUEUE")
{
i=0;
if(my_queue[i].empty())
i++;
cout<<my_queue[i].front()<<" ";
my_queue[i].pop();
}
else if(ch=="STOP")
break;
}
cout<<endl;
return 0;
}
[D.cpp]
#include <bits/stdc++.h>
using namespace std;
class customer{
public:
int arrive_time;
int work_time;
customer(int arriveTime, int workTime) : arrive_time(arriveTime), work_time(workTime) {}
};
class window{
public:
double nexttime;
double withtime;
double lwtime;
window() {
nexttime=0;
withtime=0;
lwtime=0;
}
void jisuan(customer customertemp)
{
if(customertemp.arrive_time>=nexttime)
{
nexttime=customertemp.arrive_time+customertemp.work_time;
}
else
{
double customerwithtime=nexttime-customertemp.arrive_time;
nexttime+=customertemp.work_time;
withtime+=customerwithtime;
if(customerwithtime>lwtime)
lwtime=customerwithtime;
}
}
};
int lookforwhichnear(int arrivetime,window* windowlist,int windownum)
{
int nexttimenear=windowlist[0].nexttime;
int choose=0;
for (int i = 1; i <windownum ; ++i) {
if(windowlist[i].nexttime<nexttimenear) {
nexttimenear=windowlist[i].nexttime;
choose = i;
}
}
return choose;
}
int main()
{
int customernum;
cin>>customernum;
queue<customer> customerlist;
for (int i = 0; i < customernum; ++i) {
int arrivetime,worktime;
cin>>arrivetime>>worktime;
customer customer1(arrivetime,worktime);
customerlist.push(customer1);
}
int windownum;
cin>>windownum;
window windowlist[windownum+1];
customer customertemp=customerlist.front();
windowlist[0].nexttime=customertemp.arrive_time+customertemp.work_time;
customerlist.pop();
while(!customerlist.empty()){
customertemp=customerlist.front();
int chosewhich=lookforwhichnear(customertemp.arrive_time,windowlist,windownum);
windowlist[chosewhich].jisuan(customertemp);
customerlist.pop();
}
double withsum=0;
double lwtime=0;
double lnexttime=0;
for (int i = 0; i <windownum ; ++i) {
withsum+=windowlist[i].withtime;
if(windowlist[i].lwtime>lwtime)
lwtime=windowlist[i].lwtime;
if(windowlist[i].nexttime>lnexttime)
lnexttime=windowlist[i].nexttime;
}
withsum/=customernum;
cout<<fixed<<setprecision(1)<<withsum<<" ";
cout<<(int)lwtime<<" ";
cout<<(int)lnexttime<<endl;
}
[F.cpp]
#include<iostream>
using namespace std;
int main()
{
string mode,str;
int num,t,k,i,u,f,j;
while(cin>>mode) {
cin >> t;
while(t--)
{
cin>>str;
string op[100000];
k=mode.length();
f=str.length();
num=0;
int num1;
j=0;
int e=0;
for(i=0;i<k;i++)
{
if(i+f<=k)
op[e++]=mode.substr(i,f);
}
num=0;
for(i=0;i<e;i++)
{
if(op[i]==str)
num++;
}
cout<<str<<":"<<num<<endl;
}
}
}
[KMP.cpp]
#include<bits/stdc++.h>
using namespace std;
int nextt[1000000];
void GetNext(string a)
{
int i=0,k=-1;
int len=a.length();
nextt[0]=-1;
while(i<len)
{
if(k==-1||a[i]==a[k])
{
i++,k++;
nextt[i]=k;
}
else k=nextt[k];
}
for(int i=0; i<len; i++)cout<<nextt[i]<<" ";
cout<<endl;
}
int KMP(string a,string b)
{
int i=0,j=0;
int len=a.length(),len1=b.length();
GetNext(b);
while(i<len&&j<len1)
{
if(j==-1||a[i]==b[j])i++,j++;
else j=nextt[j];
}
if(j==len1)return i-j+1;
else return 0;
}
int main()
{
string a,b;
int n;
cin>>n;
while(n--)
{
cin>>a>>b;
cout<<KMP(a,b)<<endl;
}
return 0;
}
[Longest.cpp]
#include <iostream>
#include <string>
using namespace std;
int* GetNext (string str)
{
int j, k;
int len = (int)str.size();
int* next = new int[(int)str.size() + 1];
j = 0; k = -1;
next[j] = k;
while (j < len)
{
if ( k == -1 || str[k] == str[j] )
{
++j; ++k; next[j] = k;
}
else
k = next[k];
}
return next;
}
string matched_Prefix_Postfix(string str)
{
int *next = GetNext(str);
int ans = next[(int)str.size()];
delete[]next;
if (ans <= 0) return "empty";
return str.substr(0, ans);
}
int main()
{
int t;
string s;
cin >> t;
while (t--)
{
cin >> s;
cout << matched_Prefix_Postfix(s) << endl;
}
return 0;
}
[NoRepeat.cpp]
#include<iostream>
using namespace std;
int Get_next(string a)
{
int i=0,k=-1;
int len=a.length(),maxn=0;
int *nextt=new int[a.length()+1];
nextt[0]=-1;
while(i<len)
{
if(k==-1||a[i]==a[k])
{
i++,k++;
nextt[i]=k;
if(k*2<=i)maxn=max(maxn,k);
if(i/(i-k)>=2)maxn=max(maxn,(i-k)*(i/(i-k)/2));
}
else k=nextt[k];
}
return maxn;
}
int get_maxnum(string a)
{
int len=a.length(),maxn=0;
for(int i=0;i<len;i++)
{
maxn=max(maxn,Get_next(a.substr(i,len)));
}
return maxn;
}
int main()
{
string a;
int n;
cin>>n;
while(n--)
{
cin>>a;
int num=get_maxnum(a);
if(!num)cout<<-1<<endl;
else cout<<num<<endl;
}
return 0;
}
[二分查找.cpp]
#include<iostream>
using namespace std;
const int N=1e6+10;
int q[N];
int n,m;
int bisearch(int l,int r){
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
scanf("%d",&q[i]);
}
while(m--){
int x;
scanf("%d",&x);
int l=0,r=n-1;
while(l<r){
int mid=(l+r)>>1;
if(q[mid]>=x) r=mid;
else l=mid+1;
}
if(q[l]!=x){
cout<<"-1 -1"<<endl;
}
else {
cout<<l<<" ";
while(l<r){
int mid=(l+r+1)>>1;
if(q[mid]<=x) l=mid;
else r=mid-1;
}
cout <<l<<endl;
}
}
return 0;
}
[归并.cpp]
#include<iostream>
using namespace std;
const int N= 1e6+10;
int n,result=0;
int q[N],tmp[N];
void merge_sort(int q[],int l,int r){
if(l>=r) return;
int mid=l+r>>1;
merge_sort(q,l,mid);merge_sort(q,mid+1,r);
int k=0,i=l,j=mid+1;
while(i<=mid&&j<=r){
if(q[i]<=q[j])
tmp[k++]=q[i++];
else {
tmp[k++]=q[j++];
result+=(mid-l+1);
}
}
while(i<=mid)tmp[k++]=q[i++];
while(j<=r) tmp[k++]=q[j++];
for(int i=l,j=0;i<=r;i++,j++){
q[i]=tmp[j];
cout<<"push "<<tmp[j]<<endl;
}
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&q[i]);
}
merge_sort(q,0,n-1);
// for(int i=0;i<n;i++){
// printf("%d ",q[i]);
// }
printf("%d",result);
}
[快排.cpp]
#include<iostream>
using namespace std;
/*30
128 294 133 295 175 8 232 248 241 164 11
60 238 133 291 116 6 67 98 67 196 260 181 160 83 160 90 153 233 216*/
const int N = 1e6+10;
int n;
int q[N];
void quick_sort(int q[], int l, int r)
{
if(l >= r) return;
int i = l - 1, j = r + 1, x = q[l + r + 1 >> 1];//注意是向上取整,因为向下取整可能使得x取到q[l]
while(i < j)
{
do i++; while(q[i] < x);
do j--; while(q[j] > x);
if(i < j) swap(q[i], q[j]);
}
quick_sort(q, l, i - 1), quick_sort(q, i, r);//不用q[l..i],q[i+1..r]划分的道理和分析4中j的情况一样
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&q[i]);
}
quick_sort(q,0,n-1);
for(int i=0;i<n;i++){
printf("%d",q[i]);
}
}