ACM 第十三天
训练赛题目
A题代码:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 int main() 6 { 7 double n,m; 8 freopen("input.txt","r",stdin); 9 freopen("output.txt","w",stdout); 10 while(cin>>n>>m){ 11 cout<<max(ceil(n*2.0/m),2.0)<<endl; 12 } 13 }
D题代码:
方法一 数学方法
1 #include <bits/stdc++.h> 2 using namespace std; 3 int pow2(int a,int b) 4 { 5 int ans =1; 6 for(int i=1;i<=b;i++) 7 ans=(ans*2)%(1000000007); 8 return ans; 9 } 10 int main() 11 { 12 freopen("input.txt","r",stdin); 13 freopen("output.txt","w",stdout); 14 string s; 15 cin>>s; 16 int ans=0; 17 for(int i=0;i<s.size();i++) 18 { 19 if((s[i]=='N'||s[i]=='S') &&(s[i+1]=='W'||s[i+1]=='E')) 20 ans=(ans+1)%1000000007; 21 22 } 23 long long a1=(long long)pow2(2,ans)%(1000000007); 24 cout<<a1<<endl; 25 26 return 0; 27 }
方法二 递推思想
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define mod 1000000007 4 int main() 5 { 6 freopen("input.txt","r",stdin); 7 freopen("output.txt","w",stdout); 8 char s[10010]; 9 cin>>s; 10 int a[10010]; 11 int s1=strlen(s); 12 a[1]=a[0]=1; 13 for(int i=1;i<=s1;i++) 14 { 15 if((s[i-1]=='N'||s[i-1]=='S') &&(s[i]=='W'||s[i]=='E')) 16 a[i+1]=(a[i]+a[i-1])%mod; 17 else 18 a[i+1]=a[i]; 19 } 20 cout<<a[s1]<<endl; 21 return 0; 22 }
G题:一个数是 三个素数的乘积
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int main() 5 { 6 freopen("input.txt","r",stdin); 7 freopen("output.txt","w",stdout); 8 int n; 9 bool flag; 10 while(cin>>n) 11 { 12 int cnt=0; 13 for(int i=2; i*i<=n; i++) 14 { 15 flag=1; 16 while(n%i==0) 17 { 18 if(flag) 19 { 20 cnt++; 21 flag=0; 22 } 23 n/=i; 24 } 25 } 26 if(n!=1) cnt++; 27 if(cnt==3) cout<<"YES"<<endl; 28 else cout<<"NO"<<endl; 29 } 30 return 0; 31 }
雪儿言