A題:
找規律的題,如果n%4==0,那麼就能被3整除。
#include <iostream>
#include <stdio.h>
using namespace std;
long long a[1000005];
int main()
{
int t,i,j;
cin >> t;
for(i=0;i<t;i++)
{
long long n;
cin >> n;
if(n%4==0)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
B題:
單純的輸出
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int t,n,i,j,k;
cin >> t;
for(i=0;i<t;i++)
{
cin >> n;
for(j=1;j<=n;j++)
{
for(k=0;k<n-j;k++)
cout << " ";
for(k=0;k<2*j-1;k++)
cout << "*";
cout << endl;
}
}
return 0;
}
C題:
本題輸入字符串要用能讀空格的gets或者getline,並且是將單詞進行翻轉而不是字母,所以,解決方法是將讀取字符,若遇到空格,則將前面的字符存入到一個數組中,以此類推
代碼如下:
#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
char c[1000];
char s[1000][1000];
int t,i,j;
cin >> t;
getchar();
for(i=0;i<t;i++)
{
int k=0,l=0;
gets(c);
for(j=0;c[j]!='\0';j++)
{
if(c[j]!=' ')
{
s[k][l]=c[j];
l++;
}
else
{
s[k][l]='\0';
k++;
l=0;
}
}
s[k][l]='\0';
for(j=k;j>0;j--)
cout << s[j] << " ";
cout << s[0] << endl;
}
return 0;
}
D題:
這道題屬於dfs,首先先求所有木棒的和,在判斷不能組成正n邊形的情況爲:和不能被4整除、最長的木棒比和/n大。處這兩種情況外,剩下的情況放在dfs中處理。
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int maxn = 24;
int a[ maxn ];
int vis[ maxn ];
int n ,flag;
void dfs( int pos,int len,int LEN,int sum ) //
{
if( flag==1 )
return ;
if( sum==3 )
{
flag=1;
return ;
}
for( int i=pos; i<=n; i++ )
{
if( vis[ i ]==0 )
{
if( len+a[i]<LEN )
{
vis[ i ]=1;
dfs( i,len+a[i],LEN,sum );
vis[ i ]=0;
}
else if( len+a[i]==LEN )
{
vis[ i ]=1;
dfs( 1,0,LEN,sum+1 );
vis[ i ]=0;
}
}
}
return ;
}
int main()
{
int t,N;
scanf("%d",&t);
while( t-- )
{
int sum=0;
cin >> N;
scanf("%d",&n);
for( int i=1; i<=n; i++ )
{
scanf("%d",&a[ i ]);
sum+=a[ i ];
}
if( sum%N!=0 )
{
printf("No\n");
continue;
}
sort( a+1,a+1+n );
if( a[n]>( sum/N ) )
{
printf("No\n");
continue;
}
memset( vis,0,sizeof( vis ));
flag=-1;
dfs( 1,0,sum/N,0 );
if( flag!=-1 )
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
return 0;
}
E題:
本題屬於找零錢問題,首先找錢肯定先從面值最大的錢裏面找,那麼有兩種情況,(1)M/100大於100元的張數,那麼100元的回全部找出,那剩下M-100*張數,再以此類推50,20,10,5,1元的情況;(2)小於張數,那麼只需付足夠的錢即可。最後判斷M是否爲0.
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int mian[6]={100,50,20,10,5,1};
int num[6]={0};
int main()
{
int t,i,M,s;
cin >> t;
while(t--)
{
cin >> M;
for(i=0;i<6;i++)
{
cin >> num[i];
}
for(i=0;i<6;i++)
{
s=M/mian[i];
if(s>num[i])
{
M-=(num[i]*mian[i]);
}
else
M%=mian[i];
}
if(M==0)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
F題:
簡單的字符串轉化問題
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
char c[26];
int main()
{
c[0]='a';
for(int m=1;m<26;m++)
{
c[m]=c[m-1]+1;
}
int t,i,j;
int a[35];
string s;
cin >> t;
for(i=0;i<t;i++)
{
cin >> s;
int l=s.length();
for(j=0;j<l-1;j++)
{
a[j]=(s[j]-'0')*(s[j+1]-'0');
a[j]%=26;
cout << c[a[j]];
}
cout << endl;
}
return 0;
}
G題:
這道題是博弈,難點是尋找必敗態,本題中必敗態分爲兩種:(1)兩堆的時候,只要棋子相同則爲必敗;(2)三堆的時候,1 1 1爲必敗,則由此可以推出1 2 3爲必敗,1 4 5爲必敗,2 4 6爲必敗,3 4 7爲必敗,所以只有先在2堆拿一個棋子必勝,
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int k,n;
while(scanf("%d%d",&k,&n)!=EOF)
{
if(k==0&&n==0)
break;
if(k==2)
{
if(n==1)
cout << "Dw is the winner!!!" << endl;
else
cout << "Daddy is the winner!!!" << endl;
}
else
cout << "Daddy is the winner!!!" << endl;
}
return 0;
}
H題:
簡單的數學計算題,公式爲:n*(n-1)/2-1.
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
cout<<n*(n-1)-1<<endl;
}
return 0;
}
I題
結構體排序
#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
using namespace std;
struct m
{
double y,s,w,li,wen,sum;
string name,ke;
};
int cmp(m N,m M)
{
return N.sum>M.sum;
}
int main()
{
m N[1005];
int n,i;
cin >> n;
for(i=0;i<n;i++)
{
cin >> N[i].name >> N[i].ke >> N[i].y >> N[i].s >> N[i].w >> N[i].li >> N[i].wen;
if(N[i].ke=="Science\0")
N[i].sum=N[i].y+N[i].s+N[i].w+N[i].li*0.7+N[i].wen*0.3;
else
N[i].sum=N[i].y+N[i].s+N[i].w+N[i].li*0.3+N[i].wen*0.7;
}
sort(N,N+n,cmp);
for(i=0;i<n;i++)
{
cout << N[i].name << " : " << N[i].sum << endl;
}
return 0;
}
J題:
本題是求相交區間的長度,將兩個圖形分別投影到x軸和y軸求出相交區間的長度爲相交長方形的長和寬。
#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
using namespace std;
int max(int a,int b,int c,int d)
{
int t=0;
if(a>=t)
t=a;
if(b>=t)
t=b;
if(c>=t)
t=c;
if(d>=t)
t=d;
return t;
}
int min(int a,int b,int c,int d)
{
int t=10000;
if(a<=t)
t=a;
if(b<=t)
t=b;
if(c<=t)
t=c;
if(d<=t)
t=d;
return t;
}
int main()
{
int x1,y1,x2,y2,a1,a2,xx,yy;
while(scanf("%d%d%d",&x1,&y1,&a1)!=EOF)
{
cin >> x2 >> y2 >> a2;
int s1=0,s2=0,s3=0,w=0;
int l1=max(x1,x2,x1+a1,x2+a2);
int l2=min(x1,x2,x1+a1,x2+a2);
int w1=max(y1,y2,y1-a1,y2-a2);
int w2=min(y1,y2,y1-a1,y2-a2);
if(l1-l2>=a1+a2)
{xx=0;}
else
{xx=a1+a2-(l1-l2);}
if(w1-w2>=a1+a2)
{yy=0;}
else
{yy=a1+a2-(w1-w2);}
s2=xx*yy;
s1=a1*a1-s2;
s3=a2*a2-s2;
w=30*s1+50*s2+40*s3;
cout << w << endl;
}
return 0;
}
K
水題
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
printf(" A CCCCCCCC MM MM\n");
printf(" A A C M M M M\n");
printf(" A A C M M M M\n");
printf(" AAAAAAA C M M M M\n");
printf(" A A C M M M\n");
printf("A A CCCCCCCC M M\n");
return 0;
}
L題:
本題可以從捆成混合花束的數量進行討論,混合花束的數量可能爲0,1,2;分別計算出三種情況下的值,然後取最大值就可以了,,,
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int sum1,sum2,sum3;
int max(int a,int b,int c)
{
int t=0;
if(t<=a)
t=a;
if(t<=b)
t=b;
if(t<=c)
t=c;
return t;
}
int min(int a,int b,int c)
{
int t=10000000;
if(t>=a)
t=a;
if(t>=b)
t=b;
if(t>=c)
t=c;
return t;
}
int main()
{
int t,i;
int R,B,G;
cin >> t;
while(t--)
{
sum1=0,sum2=0,sum3=0;
cin >> R >> G >> B;
int k=min(R,G,B);
int m=R%3;
int n=G%3;
int p=B%3;
if(m<=k)
sum1=m+(R-m)/3+(G-m)/3+(B-m)/3;
if(n<=k)
sum2=n+(R-n)/3+(G-n)/3+(B-n)/3;
if(p<=k)
sum3=p+(R-p)/3+(G-p)/3+(B-p)/3;
int sum=max(sum1,sum2,sum3);
cout << sum << endl;
}
return 0;
}
M
等差數列c*c=d*b;則第四個數d=c*c/b;
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int main()
{
int t,a,b,c;
cin >> t;
while(t--)
{
cin >> a >> b >> c;
cout << c*c/b << endl;
}
return 0;
}