DS3-1 字符串的模式匹配:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int main(){
string a,b;
getline(cin,a);
cin>>b;
for(int i=0;i<a.size();i++){
if(a[i]==b[0]){
int j,flag=0;
for(j=1;j<b.size();j++){
if(a[i+j]!=b[j]) flag=1;
}
if(!flag)
cout<<i<<" ",i+=b.size()-1;
}
}
return 0;
}
DS4-1 稀疏矩阵的转置
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
struct mps{
int x,y;
int date;
int c;
}mp[1000];
bool cmp(mps a,mps b){
if(a.x!=b.x) return a.x<b.x;
return a.c<b.c;
}
int main(){
int n,m,k,cnt=0;
cin>>n>>m>>k;
for(int i=0;i<k;i++){
int temp;
cin>>mp[i].x>>mp[i].y>>mp[i].date;
temp=mp[i].x;
mp[i].x=mp[i].y;
mp[i].y=temp;
cnt=mp[i].c;
cnt++;
}
sort(mp,mp+k,cmp);
cout<<m<<" "<<n<<" "<<k<<endl;
for(int i=0;i<k;i++){
cout<<mp[i].x<<" "<<mp[i].y<<" "<<mp[i].date<<endl;
}
return 0;
}
超级素数:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
bool issu(int a){
if(a==2) return true;
else if((a%2)==0) return false;
else if(a==1) return false;
for(int i=3;i<a;i++){
if(a%i==0) return false;
}
return true;
}
bool check(int x){
if(issu(x)){
while(x){
if(!issu(x)) return false;
x/=10;
}
return true;
}
return false;
}
int main(){
int n,flag=0;
cin>>n;
for(int i=2;i<=n;i++){
if(check(i)) flag++;
}
cout<<flag<<endl;
return 0;
}
多少个分数加法:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int a[1000],b[1000];
int gongbei(int x,int y){
return x*y/(__gcd(x,y));
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i]>>b[i];
}
int ansm=1,anss=0;
for(int i=0;i<n;i++){
ansm=gongbei(ansm,b[i]);
}
for(int i=0;i<n;i++){
int cnt;
cnt=ansm/b[i];
anss+=a[i]*cnt;
}
int yueshu;
yueshu=__gcd(anss,ansm);
if(yueshu==1){
cout<<anss<<" "<<ansm;
}
else{
cout<<anss/yueshu<<" "<<ansm/yueshu;
}
return 0;
}
今年过去多少天:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int month[13]={0,1,2,3,4,5,6,7,8,9,10,11,12};
int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main(){
int sum=0;
int y,m,d;
cin>>y>>m>>d;
int leap = y%400==0 ||( y%100 && y%4==0);
for(int i=1;i<m;i++){
if(i==2) sum+=28+leap;
else sum+=day[i];
}
sum+=d;
cout<<sum;
return 0;
}
数组极大值下标:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int a[10000];
int main(){
int n,maxv=0;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
maxv=max(maxv,a[i]);
}
for(int i=0;i<n;i++){
if(a[i]==maxv){
cout<<i;
break;
}
}
return 0;
}
有序插入:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int t[10000];
int main(){
int n,i;
cin>>n;
for(i=0;i<n;i++){
cin>>t[i];
}
cin>>t[i];
sort(t,t+(n+1));
for(int i=0;i<n+1;i++)
cout<<t[i]<<" ";
return 0;
}
字符串左移:
#include <iostream>
using namespace std;
const int N = 116;
int a[N];
int main(){
int n,k;
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
cin>>k;
int v=(k+n)%n;
for(int i=n-v;i<n;i++) cout<<a[i]<<" ";
cout<<a[0];
for(int i=1;i<(n-v);i++) cout<<" "<<a[i];
return 0;
}