A(模拟+数学)
题意:在一个数列当中最少添加多少个数可以使它们两两互质,并打印出添加以后的数列
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 #include <set> 8 #include <map> 9 #include <bitset> 10 #include <cmath> 11 #include <queue> 12 #include <stack> 13 using namespace std; 14 const int maxn=2020; 15 const int maxm=1000000000; 16 int a[maxn]; 17 int gcd(int a,int b) 18 { 19 if(b==0) return a; 20 return gcd(b,a%b); 21 } 22 int n; 23 int main() 24 { 25 while(cin>>n) 26 { 27 for(int i=0;i<n;i++) 28 scanf("%d",&a[i]); 29 vector<int>b; 30 int cnt=0; 31 for(int i=0;i<n-1;i++) 32 { 33 if(gcd(a[i],a[i+1])>=2){ 34 cnt++; 35 int k; 36 for(int j=1;j<=maxm;j++){ 37 if(gcd(a[i],j)<2&&gcd(j,a[i+1])<2){ 38 k=j; break; 39 } 40 } 41 b.push_back(a[i]); 42 b.push_back(k); 43 } 44 else{ 45 b.push_back(a[i]); 46 } 47 } 48 b.push_back(a[n-1]); 49 cout<<cnt<<endl; 50 for(int i=0;i<b.size()-1;i++) 51 cout<<b[i]<<" "; 52 cout<<b[b.size()-1]<<endl; 53 } 54 return 0; 55 }
B(队列模拟)
题意:根据公交车上下车的顺序,打印下车的顺序
分析:用队列直接进行模拟即可
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 #include <set> 8 #include <map> 9 #include <bitset> 10 #include <cmath> 11 #include <queue> 12 #include <stack> 13 using namespace std; 14 const int maxn=150; 15 int a[maxn][4]; 16 int n,m; 17 int main() 18 { 19 while(cin>>n>>m) 20 { 21 queue<int>que[4]; 22 int k=1; 23 int cnt=0; 24 for(;;) 25 { 26 if(cnt>=n) break; 27 if(k>m) break; 28 que[0].push(k); 29 k++; 30 if(k>m) break; 31 que[3].push(k); 32 k++; 33 cnt++; 34 } 35 for(;;) 36 { 37 if(k>m) break; 38 que[1].push(k); 39 k++; 40 if(k>m) break; 41 que[2].push(k); 42 k++; 43 } 44 vector<int>b; 45 int t; 46 int f1=0,f2=0,f3=0,f4=0; 47 for(;;) 48 { 49 if(que[1].empty()){ 50 f2=1; 51 }else{ 52 t=que[1].front(); 53 que[1].pop(); 54 b.push_back(t); 55 } 56 if(que[0].empty()){ 57 f1=1; 58 }else{ 59 t=que[0].front(); 60 que[0].pop(); 61 b.push_back(t); 62 } 63 if(que[2].empty()){ 64 f3=1; 65 }else{ 66 t=que[2].front(); 67 que[2].pop(); 68 b.push_back(t); 69 } 70 if(que[3].empty()){ 71 f4=1; 72 }else{ 73 t=que[3].front(); 74 que[3].pop(); 75 b.push_back(t); 76 } 77 if(f1&&f2&&f3&&f4) break; 78 } 79 for(int i=0;i<b.size()-1;i++) 80 cout<<b[i]<<" "; 81 cout<<b[b.size()-1]<<endl; 82 } 83 return 0; 84 }
时间: 2024-10-10 13:17:16