文档库 最新最全的文档下载
当前位置:文档库 › acm自己做过的一些题

acm自己做过的一些题

acm自己做过的一些题
acm自己做过的一些题

ACM那点事

c++语言中怎么样保留float数小数点的后两位

需要有这个!!!#include

//第一种方式,从根本改变输出流

cout<

cout<

//第二种方式,简缩型

cout<

cout<

cout<

//第三种方式,全局整体型

这种方法是保留两个有效数字,而不是小数点后边两位

cout<

cout.precision(2);

cout<

cout<

return 0;

字符与ascii码转换问题

#include

using namespace std;

int main(){

char s='A';

cout<<(int)s<

int x=70;

cout<<(char)x<

}

1006 输入一串字符,判断有多少个重复的字符串

例如:abcd 1

aaaa 4

ababab 3

#include

char s[1000001];

int next[1000001];

int main()

{

int i,j,n,t;

while(gets(s) && s[0]!='.')

{

i=0;

next[0]=-1;

j=-1;

l1:while(s[i]!='\0')

{

if(j==-1 || s[i]==s[j])

{ ++i;

++j;

next[i]=j;

goto l1;

}

j=next[j];

}

n=i;

t=n-next[n];

if(n%t==0)printf("%d\n",n/t);

else printf("1\n");

}

return 0;

}

1009 棋盘问题

... X.. X.O X.O X.O X.O X.O X.O ... ... ... ... .O. .O. OO. OO. ... ... ... ..X ..X X.X X.X XXX

输入:

2

X.O

OO.

XXX

O.X

XX.

OOO

输出:

yes

no

#include

using namespace std;

char s[3][4];

int num(char c)

{

int i,j;

int sum=0;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

if(s[i][j]==c)++sum;

return sum;

}

int line(char c)

{

int i;

for(i=0;i<3;i++)

{

if(s[i][0]==s[i][1] && s[i][0]==s[i][2] && s[i][0]==c)return 1;

if(s[0][i]==s[1][i] && s[0][i]==s[2][i] && s[0][i]==c)return 1;

}

if(s[0][0]==s[1][1] && s[0][0]==s[2][2] && s[0][0]==c)return 1;

if(s[0][2]==s[1][1] && s[2][0]==s[1][1] && s[1][1]==c)return 1;

return 0;

}

int main()

{

int a,b;

int n;

cin>>n;

for(;n>0;n--)

{

getchar();

gets(s[0]);

gets(s[1]);

gets(s[2]);

a=num('X');b=num('O');

if(a

{

cout<<"no"<

continue;

}

if(a-b>1)

{

cout<<"no"<

continue;

}

if(a-b==1)

{

if(line('O'))

{

cout<<"no"<

continue;

}

}

if(a==b)

{

if(line('X'))

{

cout<<"no"<

continue;

}

}

cout<<"yes"<

}

return 1;

}

1011 矩形小木块,落法,求最小表面积

Sample Input

5

9

10

26

27

100

Sample Output

30

34

82

54

130

#include

#include

using namespace std;

int main()

{

int x;

int area;

int n;

while(cin>>n && n!=0)

{

while(n)

{

cin>>x;

int s=2+4*x;

for(int i=1;i<=x;i++)

{

if(x%i==0)

{

for(int j=1;j<=x/i;j++)

{

if(x/i%j==0)

{

area=2*(i*j+i*x/i/j+j*x/i/j);

if(area

s=area;

}

}

}

}

cout<

n--;

}

}

}

1016 一道有关欧拉函数5的问题

Description

It is said that 90% of frosh expect to be above average in their class. You are to provide a reality check.

Input

The first line of standard input contains an integer C, the number of test cases. C data sets follow. Each data set begins with an integer, N, the number of people in the class (1 <= N <= 1000). N integers follow, separated by spaces or newlines, each giving the final grade (an integer between 0 and 100) of a student in the class.

Output

For each case you are to output a line giving the percentage of students whose grade is above average, rounded to 3 decimal places.

Sample Input

5

5 50 50 70 80 100 7 100 95 90 80 70 60 50 3 70 90 80 3 70 90 81 9 100 99 98 97 9

6 95 94 93 91

Sample Output

40.000%

57.143%

33.333%

66.667%

55.556%

#include

#include

using namespace std;

int main(){

double a[1000],sum,num;;

int t,n,i;

float k;

cin>>t;

for(;t>0;t--)

{

cin>>n;

sum=0;num=0;

for(i=0;i

{

cin>>a[i];

sum+=a[i];

}

sum/=n;

for(i=0;i

if(a[i]>sum)

++num;

k=100*num/n;

cout<

}

return 0;

}

1022 一场战争,持续n天。这个人的上下左右四个方向可以与之发生战争。其中R战胜S,s战胜p,p战胜r。战胜每天一总结,只输出最后的。

Sample Input

2

3 3 1 RRR

RSR

RRR

3 4 2 RSPR

SPRS

PRSP

Sample Output

RRR

RRR

RRR

RRRS

RRSP

RSPR

#include

using namespace std;

int main()

{

int l;

cin>>l;

while(l)

{

int x,y,z;

cin>>x>>y>>z;

char a[101][101],b[101][101];

for(int i=0;i

for(int j=0;j

{

cin>>a[i][j];

b[i][j]=a[i][j];

}

}

while(z)

{

for(int i=0;i

{

for(int j=0;j

{

if(a[i][j]=='R')

{

if(a[i-1][j]=='P' || a[i+1][j]=='P' || a[i][j+1]=='P' || a[i][j-1]=='P')

{

b[i][j]='P';

}

}

if(a[i][j]=='S')

{

if(a[i-1][j]=='R' || a[i+1][j]=='R' || a[i][j+1]=='R' || a[i][j-1]=='R')

{

b[i][j]='R';

}

}

if(a[i][j]=='P')

{

if(a[i-1][j]=='S' || a[i+1][j]=='S' || a[i][j+1]=='S' || a[i][j-1]=='S')

{

b[i][j]='S';

}

}

}

}

for(int i=0;i

{

for(int j=0;j

{

a[i][j]=b[i][j];

}

}

z--;

for(int i=0;i

{

for(int j=0;j

{

cout<

}

cout<

}

if(l>1)

cout<

l--;

}

}

1027 猜数字,大了输出头too high,小了输出too low,到最后看看一开始顶数字的那个人有没有玩赖。

Sample Input

10

too high 3

too low 4

too high 2

right on 5

too low 7

too high 6

right on 0

Sample Output

Stan is dishonest

Stan may be honest

#include

#include

#include

using namespace std;

{

int x;

int i;

char bh[30];

string s;

int n[10];

for(i=0;i<10;i++)

{

n[i]=1;

}

while(cin>>x)

{

cin.ignore(100, '\n');

if(x==0) break;

gets(bh);

s=bh;

if(s=="too high")

{

for(i=x-1;i<10;i++)

{

n[i]=0;

}

}

if(s=="too low")

{

for(i=0;i<=x-1;i++)

{

n[i]=0;

}

}

if(s=="right on")

{

for(i=0;i<10;i++)

{

if(n[x-1]==1)

{

for(i=0;i<10;i++)

{

n[i]=1;

}

cout<<"Stan may be honest"<

break;

}

else

{

cout<<"Stan is dishonest"<

for(i=0;i<10;i++)

{

n[i]=1;

}

break;

}

}

}

}

}

1029 锁头转动度数的问题,规则如下:

turn the dial clockwise 2 full turns

stop at the first number of the combination

turn the dial counter-clockwise 1 full turn

continue turning counter-clockwise until the 2nd number is reached

turn the dial clockwise again until the 3rd number is reached

pull the shank and the lock will open.

Sample Input

0 30 0 30 5 35 5 35 0 20 0 20 7 27 7 27 0 10 0 10 9 19 9 19 0 0 0 0

Sample Output

1350

1350

1620

1620

1890

1890

#include

using namespace std;

int main()

{

int a,b,c,d;

int sum;

while(cin>>a>>b>>c>>d && (a || b || c || d))

{

sum=0;

sum=1080+(40+(a-b))%40*9+(40+(c-b))%40*9+(40+(c-d))%40*9;

cout<

}

}

1029another idea

#include

using namespace std;

float lockemu(int s,int t,int d)

{

int m[40];

for(int i=0;i<=39;i++){m[i]=i;}

int cnt=0,i=s;

while(m[i]!=t)

{

cnt++;

i+=d;

if (i==-1) {i=39;}

if (i==40) {i=0;}

}

return (float)(cnt/40.0)*360;

}

int main()

{

int s1,s2,s3,s4;

int d=0;

while(cin>>s1>>s2>>s3>>s4 && s1+s2+s3+s4!=0)

{

cout<<720+lockemu(s1,s2,-1)+360+lockemu(s2,s3,1)+lockemu(s3,s4,-1)<

}

return 0;

}

1029myself

#include

using namespace std;

int main()

{

int a,b,c,d;

int sum1,sum2,sum3;

int sum;

while(cin>>a>>b>>c>>d)

{

sum1=sum2=sum3=0;

sum=0;

if(a!=0 || b!=0 || c!=0 || d!=0)

{

if(a

sum1=(40-(b-a))*9;

else if(a>b)

sum1=(a-b)*9;

else

sum1=360;

if(b>c)

sum2=(40-(b-c))*9;

else if(b

sum2=(c-b)*9;

else

sum2=360;

if(c

sum3=(40-(d-c))*9;

else if(c>d)

sum3=(c-d)*9;

else

sum3=360;

sum=1080+sum1+sum2+sum3;

cout<

}

else

break;

}

return 0;

}

1039 求m

c的问题

n

#include

using namespace std;

int main()

{

long long x,y,sum;

while(cin>>x>>y)

{

if(x==0 && y==0)

break;

int i,j;

sum=1;

if(x>y*2)

{

for( i=(x-y+1),j=1;i<=x,j<=y;i++,j++)

{

sum=sum*i/j;

}

}

if(x<=y*2)

{

for(i=y+1,j=1;i<=x,j<=(x-y);i++,j++)

{

sum=sum*i/j;

}

}

cout<

}

}

1159 递推问题,公式如下:F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2)得到的F(n)能被3整除,输出yes,反之输出no。

Sample Input

1

2

3

4

5

Sample Output

no

no

yes

no

no

no

#include

using namespace std;

int main()

{

int n;

while(cin>>n)

{

if(n<1000000 && n>=0)

{

int a[99999];

a[0]=7;

a[1]=11;

int i;

for(i=2;i<=n;i++)

{

a[i]=a[i-1]+a[i-2];

}

if(i<2)

cout<<"no"<

if(a[i-1]%3==0)

cout<<"yes"<

else

cout<<"no"<

}

else

continue;

}

}

1060 正明任何一个大于4的整数都可以分解成两个素数相加的和

Sample Input

8

20

42

Sample Output

8 = 3 + 5

20 = 3 + 17

42 = 5 + 37

#include

#include

using namespace std;

int main()

{

int n,m,i,j,k,p;

while(scanf("%d",&n)!=EOF)

{

if(n%2!=0)

continue;

p=n/2;

for(i=2;i<=p;i++)

{

k=0;

for(j=2;j<=(int)sqrt(i);j++)

{

if(i%j==0)

break;

}

if(j==(int)sqrt(i)+1)

{

m=n-i;

for(k=2;k<=(int)sqrt(m);k++)

{

if(m%k==0)

break;

}

if(k==(int)sqrt(m)+1)

{

//cout<

printf("%d = %d + %d\n",n,i,m);

break;

}

}

}

if(i==p+1)

printf("Goldbach's conjecture is wrong.");

}

}

1079 判断字符串包含问题

Sample Input

sequence subsequence

person compression

VERDI vivaVittorioEmanueleReDiItalia caseDoesMatter CaseDoesMatter Sample Output

Yes

No

Yes

No

#include

#include

#include

using namespace std;

int main()

{

string s,s1;

while(cin>>s1>>s)

{

int t=0,i=0,j=0;

int length=0,length1=0;

while(s[length])

{

length++;

}

while(s1[length1])

{

length1++;

}

while(s[i])

{

if(s1[j]==s[i])

{

j++;

}

i++;

if(j==length1)

{

t=1;

break;

}

}

if(t==1)

cout<<"Yes"<

else

cout<<"No"<

}

}

1082 求两个点(坐标形式)的终点问题

Sample Input

5 10 2 18 2 22

6 14 18 10 18

3 -

4 6 -2 4 -2 6

3 -8 12

4 8 6 12

Sample Output

5 14.000000 2.000000 20.000000 4.000000 18.000000 12.000000 12.000000 18.000000 10.000000 10.000000

3 -3.000000 5.000000 -2.000000 5.000000 -3.000000 6.000000

3 -2.000000 10.000000 5.000000 10.000000 -1.000000 12.000000

#include

#include

using namespace std;

int main()

{

int n;

while(cin>>n && n!=0)

{

int i=0,s;

double a[99999],b[99999];

double c[99999],d[99999];

s=n;

while(s)

{

cin>>a[i]>>b[i];

i++;

s--;

}

for(int j=0;j

{

//cout<<"^^^^^^^^^^^";

if(j

{

c[j]=(a[j]+a[j+1])/2.0;

d[j]=(b[j]+b[j+1])/2.0;

}

if(j==n-1)

{

c[j]=(a[j]+a[0])/2.0;

d[j]=(b[j]+b[0])/2.0;

}

}

cout<

for(int j=0;j

{

cout<

cout<<" ";

cout<

if(j!=n-1)

cout<<" ";

}

cout<

}

}

1198 限定一个最小的矩形,把输入的所有的点包含进去

Sample Input

12 56

23 56

13 10

0 0

12 34

0 0

0 0

Sample Output

12 10 23 56

12 34 12 34

#include

#include

using namespace std;

int main()

{

int x,y;

int x1,y1,x2,y2;

int i=1,j;

while(i)

{

x1=99999;

y1=99999;

y2=0;

x2=0;

j=0;

while(cin>>x>>y && (x || y))

{

j++;

if(x1>x)

{x1=x;}

if(x2

{x2=x;}

if(y1>y)

{y1=y;}

if(y2

{y2=y;}

}

if(j==0)

break;

cout<

}

}

1199 一个自定义的加法规则比如:24+1 -> 42+1 -> 43 -> 34

Sample Input

3

24 1

4358 754

305 794

Sample Output

34

1998

1

#include

#include

#include

相关文档