西工大C语言POJ习题答案

合集下载

西工大C语言POJ习题答案讲解

西工大C语言POJ习题答案讲解
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a<b)
a=b;
if(a<c)
a=c;
printf("%d\n",a);
return 0;
}
5.
#include<stdio.h>
int main()
{
int i=0,j=0,k=1;
char a[6];
e=29;
else
e=28;
switch (b)
{
case 1:n=c;break;
case 2:n=g+c;break;
case 3:n=g+e+c;break;
case 4:n=g+e+g+c;break;
case 5:n=g+e+g+f+c;break;
case 6:n=g+e+g+f+g+c;break;
b=(a-2)*1.5+7;
else b=7;
}
printf("%lf\n",b);
return 0;
}
8.
#include <stdio.h>
int main()
{
int a,b,c,e,f=30,g=31,n;
scanf("%d-%d-%d",&a,&b,&c);
if((a%400==0)||(a%100!=0&&a%4==0))
return 0;

西工大新版poj部分题答案

西工大新版poj部分题答案

1.#include <stdio.h>int main(){int a[10]={0},i,j,num,count; for(i=2;i<1000;i++){ count=0;num=i;for(j=1;j<i/2+1;j++){if(i%j==0){num-=j; a[count++]=j;}}if(num==0){printf("%d=%d",i,a[0]);for(j=1;j<count;j++)printf("+%d",a[j]);printf("\n");}}return 0;}2..#include <stdio.h>#include <math.h>int main(){double x1,a,eqs=1,x2; scanf("%lf",&a);x1=a/2;while(fabs(eqs)>=0.00001){ x2=x1;x1=1.0/2*(x1+a/x1);eqs=x2-x1;}printf("%.5lf\n",x1); return 0;}3.#include <stdio.h>double fun(double x){return (2*x*x*x-4*x*x+3*x-6); }int main(){double a,b,x;scanf("%lf%lf",&a,&b);x=(a+b)/2.0;while(fun(x)!=0){if(fun(x)<0)a=x;else b=x;x=(a+b)/2;}printf("%.2lf\n",x);return 0;}4.#include <stdio.h>int main(){char A,B,C;for(A='X';A<='Z';A++)for(B='X';B<='Z';B++)for(C='X';C<='Z';C++)if(A!='X'&&C=='Y'&&A!=B&&B!=C&&C!=A)printf("A=%c\nB=%c\nC=%c\n",A,B,C);return 0;}5.#include <stdio.h>void swap(int *m,int *n){int t;if(*m>*n){t=*n;*n=*m;*m=t;}}int main(){int a,b,i,j;scanf("%d%d",&a,&b);swap(&a,&b);for(i=a+1;i<b;i++)for(j=2;j*j<=i;j++)if(i%j==0)break;if(j*j>i)printf("%d ",i);}printf("\n");return 0;}6.#include <stdio.h>int main(){int i;double sum=0,a=2,b=1,t; for(i=0;i<20;i++){sum+=a/b;t=a;a=a+b;b=t;}printf("%lf\n",sum); return 0;}7.#include <stdio.h>#include <math.h>int main(){int a[5]={0},c[5],i;double b[5]={8*12*0.0084,5*12*0.0075,3*12*0.0069,24*0.0066,12*0.0063},max=0,sum; for(a[0]=0;a[0]<3;a[0]++)for(a[1]=0;a[1]<=(20-8*a[0])/5;a[1]++)for(a[2]=0;a[2]<=(20-8*a[0]-5*a[1])/3;a[2]++)for(a[3]=0;a[3]<=(20-8*a[0]-5*a[1]-3*a[2])/2;a[3]++){a[4]=20-8*a[0]-5*a[1]-3*a[2]-2*a[3];sum=1;for(i=0;i<5;i++)sum*=pow((1+b[i]),a[i]);if(sum>max){max=sum;for(i=0;i<5;i++)c[i]=a[i];}}for(i=0;i<5;i++)printf("%d ",c[i]);printf("\n%.2f",max*2000);return 0;}8.#include <stdio.h> #include <math.h> int main(){int i=0;double m;scanf("%lf",&m);while(fabs(m)>=1) {m=m/10;i++;}printf("%d\n",i); return 0;}9.#include <stdio.h>#include <math.h>int main(){int s=1;double pi=0,n=1,t=1;while(fabs(t)>1e-6)pi=pi+t,n=n+2,s=-s,t=s/n;pi=pi*4;printf("%lf\n",pi);return 0;}10.#include<stdio.h>int main(){int n,j;float s,t1,t2;scanf("%d",&n);s=0; t1=1.0; t2=2.0;for(j=1;j<=n;j++){s=s+t1*t2/((t2-t1)*(2*t2+t1));t2=t2*2;t1=-t1;}printf("%.6f\n",s);return 0;}11.#include <stdio.h>int main(){int cnt=0,sum=0,p=1,i,j;for(i=800;i>500;i--){for(j=2;j*j<=i;j++)if(i%j==0)break;if(j*j>i){cnt++;sum+=p*i;p=-p;}}printf("%d %d\n",cnt,sum);return 0;}12.任何一个自然数的n的立方都等于n个连续奇数之和#include <stdio.h>int main(){int n,i,j,s,k=1,a[20];scanf("%d",&n);while(1){s=0;for(i=1,j=k;i<=n;i++,j+=2){s+=j;a[i]=j;}k+=2;if(s==n*n*n)break;}printf("%d*%d*%d=%d=%d",n,n,n,s,a[1]); for(i=2;i<=n;i++)printf("+%d",a[i]);printf("\n");return 0;}13.#include <stdio.h>int main(){int x,a,b=1,i; //b保存最后三位数scanf("%d%d",&x,&a);for(i=0;i<a;i++)b=b*x%1000; //取积的后三位printf("%d\n",b);return 0;}14.#include <stdio.h>int main(){int a[20]={1,1},i=2,s,s1; s=a[0]+a[1];while(1){a[i]=a[i-2]+2*a[i-1];s1=s;s+=a[i];if(s>100&&s1<100)printf("%d ",i);if(s>1000&&s1<1000)printf("%d ",i);if(s>10000&&s1<10000) {printf("%d ",i);break;}i++;}return 0;}15.#include <stdio.h>int main(){int n=4,m,k,count,j; while(1){count=0;m=n;for(k=0;k<5;k++){j=m/4*5+1;m=j;if(j%4==0) count++;elsebreak;}if(count==4){printf("%d %d\n",j,n);break;}n+=4;}return 0;}16.#include <stdio.h>#define MAX(x,y) ((x)>=(y)?(x):(y)) int main(){int n,a[20],max1,max,i,j,min; scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&a[i]);max=a[0];for(i=0;i<n;i++){max1=min=a[i];for(j=i+1;j<n;j++){min*=a[j];if(max1*a[j]>max||min>a[j])max1=MAX(max1*a[j],min); }if(max1>max)max=max1;}if(max<=0)printf("-1\n");elseprintf("%d\n",max);return 0;}17.#include <stdio.h>int main(){int n,a,b,c;scanf("%d%d%d",&a,&b,&c);for(n=c+7;;n+=7)if(n%3==a&&n%5==b&&n>=10)break;if(n>100)printf("-1");elseprintf("%d\n",n);return 0;}18.#include <stdio.h>#include <math.h>int main(){int x,y,a,b,l,t=1;scanf("%d%d%d%d%d",&x,&y,&a,&b,&l); if(a==b)printf("impossible\n");else{while(1){if(fabs(a*t-b*t)+fabs(x-y)==l){printf("%d\n",t);break;}t++;}}}19.#include <stdio.h>int main(){int a,b,c,d,e,f,g,h,i,j,n;scanf("%d",&n);for(a=0;a<10;a++)for(b=0;b<10;b++)if(b!=a)for(c=0;c<10;c++)if(c!=a&&c!=b)for(d=0;d<10;d++)if(d!=a&&d!=b&&d!=c)for(e=0;e<10;e++)if(e!=a&&e!=b&&e!=c&&e!=d)for(f=0;f<10;f++)if(f!=a&&f!=b&&f!=c&&f!=d&&f!=e)for(g=0;g<10;g++)if(g!=a&&g!=b&&g!=c&&g!=d&&g!=e&&g!=f)for(h=0;h<10;h++)if(h!=a&&h!=b&&h!=c&&h!=d&&h!=e&&h!=f&&h!=g)for(i=0;i<10;i++)if(i!=a&&i!=b&&i!=c&&i!=d&&i!=e&&i!=f&&i!=g&&i!=h)for(j=0;j<10;j++)if(j!=a&&j!=b&&j!=c&&j!=d&&j!=e&&j!=f&&j!=g&&j!=h&&j!=i)if((a*10000+b*1000+c*100+d*10+e)%(f*10000+g*1000+h*100+i*10+j)==0&&(a*10000+b*10 00+c*100+d*10+e)/(f*10000+g*1000+h*100+i*10+j)==n)printf("%d%d%d%d%d/%d%d%d%d%d=%d\n",a,b,c,d,e,f,g,h,i,j,n);return 0;}20.#include <stdio.h>int main(){int a,b,count=1,i,flag1=0,flag2=0; double sum=0,c;scanf("%d%d",&a,&b);while(1){c=0;for(i=1;i<=count;i++)c+=1.0/i;sum+=1/c;if(sum>a&&flag1!=1){printf("%d ",count);flag1=1;}if(sum>b&&flag2==0) break; else if(sum>b){printf("%d",count-1);break;}flag2=1;count++;}printf("\n");return 0;}21.#include <stdio.h>int main(){int n,count=0,i=500,s,x;scanf("%d",&n);while(count<n){s=0;for(j=i*100-99;j<i*100;j+=2) {x=0;for(k=3;k*k<=j;k+=2)if(j%k==0){x=1;break;}s+=x;}if(s==50)count++;i++;}printf("%d %d\n",j-101,j-2); return 0;}22.#include <stdio.h>{int n,i,k;scanf("%d",&n); for(i=1;i<=n;i++) {if(i%7==0)printf("%d ",i); else{k=i;while(k){if(k%10==7){printf("%d ",i);break;}k/=10;}}}}23.#include <stdio.h> int main(){int a=1,b=0,t,i,c; scanf("%d",&t);for(i=0;i<t;i++) {a=b;b=3*c+2*b;}printf("%d %d",a,b);return 0;}24.#include <stdio.h>int main(){int a,b,c,d,e,f,g,h,i;for(a=1;a<4;a++)for(b=1;b<10;b++)if(b!=a){for(c=1;c<10;c++){if(c!=a&&c!=b){f=c*2%10;if(f!=a&&f!=b&&f!=c){e=(b*2+c*2/10)%10;if(e!=a&&e!=b&&e!=c&&e!=f){d=2*a+(b*2+c*2/10)/10;if(d!=a&&d!=b&&d!=c&&d!=f&&d!=e) {i=3*c%10;if(i!=a&&i!=b&&i!=c&&i!=f&&i!=e&&i!=d){h=(b*3+c*3/10)%10;if(h!=a&&h!=b&&h!=c&&h!=f&&h!=e&&h!=d&&h!=i&&h!=0) {g=3*a+(b*3+c*3/10)/10;if(g<10&&g!=a&&g!=b&&g!=c&&g!=f&&g!=e&&g!=d&&g!=i&&g!=h){printf("%d%d%d %d%d%d %d%d%d",a,b,c,d,e,f,g,h,i);printf("\n");}}}}}}}}}return 0;}25.#include <stdio.h>int main(){int n,m,i;double sum=0;scanf("%d%d",&n,&m);for(i=n;i<=m;i++)sum+=(1.0/i)*(1.0/i); //注意,i*i溢出printf("%.5lf\n",sum); return 0;}26.#include <stdio.h>int main(){int n,count=0,i,k; scanf("%d",&n);for(i=1;i<=n;i++) {k=i;while(k){if(k%10==1)count++;k/=10;}}printf("%d\n",count); return 0;}27.#include <stdio.h>int main(){int k,x,y;scanf("%d",&k);for(y=k+1;y<=2*k;y++){if(k*y%(y-k)==0){x=k*y/(y-k);printf("1/%d=1/%d+1/%d\n",k,x,y); }}return 0;}28.分数化小数#include <stdio.h>int main(){int a,b,c,i;scanf("%d%d%d",&a,&b,&c);printf("%d.",a/b);a=a%b;for(i=0;i<c-1;i++){a*=10;printf("%d",a/b);a%=b;a=a*10/b;if(a>=5)printf("%d\n",a+1);elseprintf("%d\n",a); return 0;}29.#include <stdio.h>#include <math.h>int main(){int m,n,i,flag,g=0,s=0,k; scanf("%d%d",&m,&n); i=(int)pow(10,n-1);n=i*10;for(;i<n;i++){k=i;flag=0;while(k){if(k%10==m){flag=1;break;}k/=10;}if(flag==1)if(i%m!=0){g++;s+=i;}}}printf("%d %d\n",g,s);return 0;}30.下面为数据加密的代码:#include <stdio.h>int fun(int n){return (n+5)%10;}int main(){int a,b[4],i=0;scanf("%d",&a);while(a){b[i]=fun(a%10);a/=10;i++;}a=b[0]*1000+b[1]*100+b[2]*10+b[3]; printf("%d\n",a);return 0;}31.#include <stdio.h>#include <math.h>int move(int value,int n){if(n==0) return value;else if(n<0){n=(int)fabs(n);value=(value<<n)|(value>>(32-n));}elsevalue=(value<<(32-n))|(value>>n); return value;}int main(){int a,n;scanf("%d%d",&a,&n);a=move(a,n);printf("%d",a);return 0;}32.#include <stdio.h>void fun(int n){if(n!=0){printf("%d",n%10);n/=10;fun(n);}}int main(){long a;scanf("%ld",&a);fun(a);printf("\n");return 0;}33.梯形法求积分#include <stdio.h>double f(double x){return 1.0/(1.0+x*x);}double jifen(double a,double b) {double h,s=0;;int i;h=(b-a)/5000000;for(i=1;i<5000000;i++){s+=(f(a)+f(a+h))*0.5*h; a+=h;}return s;}int main(){double a,b,s;scanf("%lf%lf",&a,&b);s=jifen(a,b);printf("%lf\n",s);return 0;}34.#include <stdio.h>int getbit(int n,int k){int count=0;while(1){count++;if(count==k)break;n/=2;}return n%2;}int main(){int n,k;scanf("%d%d",&n,&k);k=getbit(n,k);printf("%d\n",k);return 0;}35.#include <stdio.h>int fun(int n,int a[]){int k=0,b[6]={100,50,10,5,2,1},i,j; for(j=0;j<n;j++)for(i=0;i<6;i++){if(a[j]>=b[i]){k+=a[j]/b[i];a[j]%=b[i];}if(a[j]==0)break;}return k;}int main(){int n,a[100],sum=0,i;scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&a[i]);sum=fun(n,a);printf("%d\n",sum);return 0;}36.#include <stdio.h>inline int xchg(unsigned char n){char left=n<<4; //左移取高四位,低四位补0 char right=n>>4;return left+right;}int main(){int a;scanf("%d",&a);a=xchg((unsigned char)a);printf("%d\n",a);return 0;}37.#include <stdio.h>#include <stdlib.h>struct shu{double data;struct shu *next;};struct shu *creat(int n,double* sum){int i;struct shu* head=NULL;struct shu* p1=NULL,*p2=NULL;for(i=0;i<n;i++){p1=(struct shu *)malloc(sizeof(struct shu)); scanf("%lf",&p1->data);*sum+=p1->data;if(i==0) head=p1;else p2->next=p1;p2=p1;}p2->next=NULL;return (head);}int main(){int n;double sum=0,fc=0,aver=0;struct shu* head=NULL;scanf("%d",&n);head=creat(n,&sum);aver=sum/n;while(head!=NULL){fc+=(head->data-aver)*(head->data-aver); head=head->next;}printf("%lf\n",fc);return 0;}38.#include <stdio.h>int fun(int a){int b=0,i;for(i=1;i<=a/2;i++){if(a%i==0)b+=i;}return b;}int main(){int a,b;scanf("%d%d",&a,&b);if(fun(a)==b&&fun(b)==a)printf("YES\n");elseprintf("NO\n"); return 0;}39.#include <stdio.h> #include <math.h> int fceil(double a){ int b;if((int)a>=a) return (int)a; elsereturn (int)a+1; }int main(){double a;int b;scanf("%lf",&a); b=fceil(a);printf("%d\n",b); return 0;}40.#include <stdio.h>int main(){int a,b,n;scanf("%d",&n);while(n--){scanf("%d%d",&a,&b);a%=100,b%=100;printf("%d\n",(a+b)%100);}return 0;}41.#include <stdio.h>int main(){int a1,a2,a3,b1,b2,b3,a,b,c;scanf("%d%d%d%d%d%d",&a1,&a2,&a3,&b1,&b2,&b3);c=(a3+b3)%60;b=(a2+b2+(a3+b3)/60)%60;a=a1+b1+((a3+b3)/60+a2+b2)/60; printf("%d %d %d\n",a,b,c); return 0;}42.#include <stdio.h>int ack(int m,int n){if(m==0) return n+1;else if(n==0) return ack(m-1,1); elsereturn ack(m-1,ack(m,n-1));}int main(){int m,n;scanf("%d%d",&m,&n);m=ack(m,n);printf("%d\n",m);return 0;}43.#include <stdio.h>#include <string.h>int n;int stringcompare(char S1[],char S2[]) {int i;if(strcmp(S1,S2)==0) n=0;elsefor(i=0;;i++){if(S1[i]!=S2[i]){n=S1[i]-S2[i];break;}}return(n);}int main(){char S1[81],S2[81];gets(S1);gets(S2);printf("%d\n",stringcompare(S1,S2));return 0;}44.#include <stdio.h>#include <string.h>#include <math.h>int gcd(int a,int b){if(a==0){return b;}else if(b==0){return a;}else{int i=a>b?b:a;while(a%i!=0||b%i!=0){i--;}return i;}}int main(){char A[1000];int a=0,b=0,i,m;gets(A);m=strlen(A);for(i=0;i<m;i++){if(A[i]=='S'){printf("WA\n");break;}if(A[i]=='U')a++;if(A[i]=='D')b++;}if(a+b!=m) return 0;else{if(fabs(((double)a)/m-0.5)>0.003) printf("Fail\n");elseprintf("%d/%d\n",a/gcd(a,m),m/gcd(a,m));return 0;}}45.#include <stdio.h>#include <string.h>int main(){char A[3][80];int n,m,i=0,j=0,k=0,a=0,b=0;for(n=0;n<3;n++){gets(A[n]);for(m=0;m<80&&A[n][m]!='\0';m++){if(A[n][m]=='\n') break;else if(A[n][m]>='A'&&A[n][m]<='Z') i++;else if(A[n][m]>='a'&&A[n][m]<='z') j++;else if(A[n][m]>='0'&&A[n][m]<='9') k++;else if(A[n][m]==' ') a++;else b++;}}printf("%d %d %d %d %d\n",i,j,k,a,b);return 0;}46.#include <stdio.h>#include <string.h>int main(){int i=0,j=0,k=0;char s[1000]={0},t[100][100]={0};gets(s);while(s[i]!=0){if(s[i]!=' '&&s[i]!=','&&s[i]!='.') t[k][j]=s[i],j++;else{k++;j=0;}i++;}for(i=k-1;i>=0;i--)printf("%s ",t[i]);printf("\n");return 0;}47.#include <stdio.h>#include <string.h>void Left(char str[],int n,char dest[]){int i;for(i=0;i<n;i++)dest[i]=str[i];}void Right(char str[],int n,char dest[]){int i,m,j=0;m=strlen(str);for(i=m-n;i<=m;i++){dest[j]=str[i];j++;}}void Mid(char str[],int loc,int n,char dest[]) {int i,j=0;for(i=loc;i<loc+n;i++){dest[j]=str[i];j++;}}int main(){int n,i,loc;char str[81],dest[81];gets(str);scanf("%d %d",&n,&loc);Left(str,n,dest);for(i=0;i<n;i++)printf("%c",dest[i]);printf("\n");Right(str,n,dest);puts(dest);Mid(str,loc,n,dest);puts(dest);return 0;}48.#include <stdio.h>#include <string.h>int main(){int i,j,n;char A[81],t;gets(A);n=strlen(A);if(n%2==0){for(j=0;j<n/2-1;j++)for(i=0;i<n/2-1-j;i++)if(A[i]<A[i+1])t=A[i],A[i]=A[i+1],A[i+1]=t;for(j=0;j<n/2-1;j++)for(i=n/2;i<n-1-j;i++)if(A[i]>A[i+1])t=A[i],A[i]=A[i+1],A[i+1]=t;for(i=n/2;i<n;i++)printf("%c",A[i]);for(i=0;i<n/2;i++)printf("%c",A[i]);printf("\n");}else{for(j=0;j<(n-1)/2-1;j++)for(i=0;i<(n-1)/2-1-j;i++)if(A[i]<A[i+1])t=A[i],A[i]=A[i+1],A[i+1]=t;for(j=0;j<(n-1)/2-1;j++)for(i=(n+1)/2;i<n-1-j;i++)if(A[i]>A[i+1])t=A[i],A[i]=A[i+1],A[i+1]=t;for(i=(n+1)/2;i<n;i++)printf("%c",A[i]);printf("%c",A[(n+1)/2-1]);for(i=0;i<(n+1)/2-1;i++)printf("%c",A[i]);printf("\n");}return 0;}49.#include <stdio.h>int main(){char A[10000],B[10000];int i,j=0,t;gets(A);while(A[j]!='='){B[j]=A[j];j++;}t=j-1;for(i=0;i<=t;i++){A[i]=B[j-1];j--;}for(i=0;i<=t;i++)printf("%c",A[i]);printf("\n");return 0;}50.#include <stdio.h>int main (){int test_case = 0;char note[1000];while (gets( note ) != NULL){test_case ++;if (note[0] == 'A' && note[1] == ' '){printf( "UNIQUE\n");break;}else if (note[0] == 'A' && note[1] == '#'){note[0] = 'B';note[1] = 'b';printf("%s\n",note);break;}else if (note[0] == 'B' && note[1] == 'b') {note[0] = 'A';note[1] = '#';printf("%s\n",note);break;}else if (note[0] == 'B' && note[1] == ' ') {printf( "UNIQUE\n");break;} else if (note[0] == 'C' && note[1] == ' ') {printf( "UNIQUE\n");break;} else if (note[0] == 'C' && note[1] == '#') {note[0] = 'D';note[1] = 'b';printf("%s\n",note);break;}else if (note[0] == 'D' && note[1] == 'b') {note[0] = 'C';note[1] = '#';printf("%s\n",note);break;}else if (note[0] == 'D' && note[1] == ' ') {printf("UNIQUE\n");break;} else if (note[0] == 'D' && note[1] == '#') {note[0] = 'E';note[1] = 'b';printf("%s\n",note);break;}else if (note[0] == 'E' && note[1] =='b') {note[0] = 'D';note[1] = '#';printf("%s\n",note);break;}else if (note[0] == 'E' && note[1] == ' ') {printf("UNIQUE\n");break;} else if (note[0] == 'F' && note[1] == ' ') {printf("UNIQUE\n");break;} else if (note[0] == 'F' && note[1] == '#'){note[0] = 'G';note[1] = 'b';printf("%s\n",note);break;}else if (note[0] == 'G' && note[1] == 'b'){note[0] = 'F';note[1] = '#';printf("%s\n",note);break;}else if (note[0] == 'G' && note[1] == ' '){printf("UNIQUE\n");break;}else if (note[0] == 'G' && note[1] == '#'){note[0] = 'A';note[1] = 'b';printf("%s\n",note);break;}else if (note[0] == 'A' && note[1] == 'b'){note[0] = 'G';note[1] = '#';printf("%s\n",note);break;}else {printf("false");break;}}return 0;}51.#include <stdio.h>#include <string.h>int main(){char A[100],B[100];int i,m,n,j=0;gets(A);n=strlen(A);scanf("%d",&m);for(i=m;i<n;i++){B[j]=A[i];j++;}for(i=0;i<j;i++)printf("%c",B[i]);printf("\n");return 0;}52.#include <stdio.h>int main(){char C[100],*p=C;int A[50],*q=A,num=0,i;gets(C);for(;*p!='\0';p++)if(*p>='0'&&*p<='9'){*q=(*p)-'0';p++;while(*p>='0'&&*p<='9')*q=(*q)*10+(*(p++)-'0');num+=1;q+=1;}printf("%d\n",num);for(i=0,q=A;i<num;i++,q++)printf("%d ",*q);printf("\n");return 0;}53.#include<stdio.h>int minsum(int a,int b){int i,h=1,k;k=(a>b)?b:a;for(i=2;i<=k;i++){if(a%i==0&&b%i==0) h=i;}return h;}int main(){int a,b,c,d,min,y,z;char e,f,g;scanf("%d%c%d%c%d%c%d",&a,&e,&b,&f,&c,&g,&d);if(f=='+'){y=a*d+b*c;z=b*d;if(y%z==0) printf("%d\n",y/z);//注意相加后可能结果为整数,这时不能以分数形式输出else{min=minsum(y,z);printf("%d/%d\n",y/min,z/min);}}else{y=a*d-b*c;z=b*d;if(y==0) printf("%d\n",y);else{if(y<0) y=-y;//注意相减后结果可能是0,此时应该直接输出if(y%z==0) printf("%d\n",y/z);//注意相减后可能结果为整数,这时不能以分数形式输出else{min=minsum(y,z);printf("-");printf("%d/%d\n",y/min,z/min);}}}return 0;}54.#include <stdio.h>int main(){int n,a[10][10],i,j;scanf("%d",&n);for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d", &a[i][j]); for(i=0;i<n;i++){for(j=0;j<n-i;j++)printf("%d ",a[i][j]);printf("\n");}return 0;}55.。

C语言程序设计_西安理工大学中国大学mooc课后章节答案期末考试题库2023年

C语言程序设计_西安理工大学中国大学mooc课后章节答案期末考试题库2023年

C语言程序设计_西安理工大学中国大学mooc课后章节答案期末考试题库2023年1.for(i=j=0;i<10&&j<8;i++,j+=3)控制的循环体执行的次数是()参考答案:32.对下面程序段,描述正确的是()for(t=1;t<=100;t++){scanf("%d",&x);if(x<0)continue;printf("%d",x);}参考答案:最多允许输出100个非负整数3.表示y在(-∞,3]和[23,+∞)范围内为真的表达式为()参考答案:(y<=3)||(y>=23)4.若有定义:char a;int b;float c;double d;则表达式a*b+d-c值的类型为参考答案:double5.设有以下程序,若要使程序的输出值为2,则应该从键盘给n输入的值是()voidmain(){ints=0,a=1,n;scanf("%d",&n);do{s=s+1;a=a-2;}while(a!=n);printf("%d",s);}参考答案:-36.以下不正确的说法是()参考答案:在主函数内定义的变量可在其余函数中通用7.设int a=1,b=2;以下选项中不正确的语句是参考答案:a*3=38.下列程序的运行结果是()#include void fun(); int main() { int a=10; fun();printf("%5d",a); return 0; } void fun() { int a=20; printf("%5d",a);}参考答案:20 109.在下面的叙述中,不正确的是()。

参考答案:枚举常量的值可以是整数或字符串10.已知字符'0'的ASCII码为十六进制的30H,下面程序的输出是()。

西工大C语言大作业习题答案

西工大C语言大作业习题答案

童鞋们,快来呀!答案!答案!no1.绘制余弦曲线在屏幕上用“*”显示0~360度的余弦函数cos(x)曲线*问题分析与算法设计如果在程序中使用数组,这个问题十分简单。

但若规定不能使用数组,问题就变得不容易了。

关键在于余弦曲线在0~360度的区间内,一行中要显示两个点,而对一般的显示器来说,只能按行输出,即:输出第一行信息后,只能向下一行输出,不能再返回到上一行。

为了获得本文要求的图形就必须在一行中一次输出两个“*”。

为了同时得到余弦函数cos(x)图形在一行上的两个点,考虑利用cos(x)的左右对称性。

将屏幕的行方向定义为x,列方向定义为y,则0~180度的图形与180~360度的图形是左右对称的,若定义图形的总宽度为62列,计算出x行0~180度时y点的坐标m,那么在同一行与之对称的180~360度的y点的坐标就应为62-m。

程序中利用反余弦函数acos计算坐标(x,y)的对应关系。

使用这种方法编出的程序短小精炼,体现了一定的技巧。

*程序说明与注释#include<stdio.h>#include<math.h>void main(){double y;int x,m;for(y=1;y>=-1;y-=0.1){m=acos(y)*10;for(x=1;x<m;x++) printf(" ");printf("*");for(;x<62-m;x++)printf(" ");printf("*\\n");}}no2.绘制余弦曲线和直线在屏幕上显示0~360度的cos(x)曲线与直线f(x)=45*(y-1)+31的迭加图形。

其中cos(x)图形用“*”表示,f(x)用“+”表示,在两个图形相交的点上则用f(x)图形的符号。

*问题分析与算法设计本题可以在上题的基础上进行修改。

图形迭加的关键是要在分别计算出同一行中两个图形的列方向点坐标后,正确判断相互的位置关系。

POJ经典题答案

POJ经典题答案

2231# include <stdio.h># include <stdlib.h>int cmp(const void *a, const void *b){return *(__int64 *)a - *(__int64 *)b;}int main(){__int64 i, n;__int64 data[10001];__int64 sum, total, tmp;scanf("%I64d",&n);sum = total = tmp = 0;for(i = 0; i < n; i++){scanf("%I64d",&data[i]);sum += data[i];}qsort(data,n,sizeof(data[0]),cmp);for(i = 0; i < n; i++){tmp += data[i];total += sum-tmp-(n-i-1)*data[i]+i*data[i]-(tmp-data[i]); }printf("%I64d\n",total);return 1;}1922#include<iostream>using namespace std;struct{double v,t;} rider[10010];int n;void input(){int i;double temp,mins=100000000;for(i=0;i<n;i++){cin >> rider[i].v >> rider[i].t;temp=4.5/(rider[i].v/3600)+rider[i].t;if(rider[i].t>=0)if(temp<mins)mins=temp;}if(int(mins*10)==(int(mins)*10))mins=int(mins);else mins=int(mins)+1;cout < mins < endl;}int main(){while(1){scanf("%d",&n);if(n==0)break;input();}return 0;}////////////////////////////////////////#include<iostream>using namespace std;struct{double v,t;} rider[10010];int n;void input(){int i;double temp,mins=100000000;for(i=0;i<n;i++){cin >> rider[i].v >> rider[i].t;temp=4.5/(rider[i].v/3600)+rider[i].t;if(rider[i].t>=0)if(temp<mins)mins=temp;}if(int(mins*10)==(int(mins)*10))mins=int(mins);else mins=int(mins)+1;cout << mins <<endl;}int main(){while(1){scanf("%d",&n);if(n==0)break;input();}return 0;}1014#include <iostream>using namespace std;int ok ;int d[6] ;void find ( int now , int x ){if ( x 0 )return ;int s , i ,t = now / (x+1) ;s = t > d[x] ? d[x] : t ;if ( now - s * (x+1) != 0 )for ( i = s ; i >= 0 ; i -- ) {find ( now - i * (x+1) , x-1 ) ;if ( ok )return ;}else {ok = 1 ;return ;}return ;}int main (){int i ,sum ,t = 0 ;while ( 1 ){for ( i = 0,sum = 0 ; i 6 ;i ++ ) {cin>>d[i];d[i] %= 60 ;sum += d[i] * ( i+1 ) ; }if ( sum == 0 )break ;ok = 0 ;if ( sum % 2 ) {cout<<"Collection #"<<++t<<":"<<endl;cout<<"Can't be divided."<<endl;cout<<endl; }else {find ( sum/2 , 5 ) ;if ( ok ) {cout<<"Collection #"<<++t<<":"<<endl;cout<<"Can be divided."<<endl;cout<<endl;}else {cout<<"Collection #"<<++t<<":"<<endl;cout<<"Can't be divided."<<endl;cout<<endl;}}}return 0 ;}1005//1005#include<iostream>#include<cmath>using namespace std;int main(){int n,count,num=1;float x=0.0,y=0.0,temp=0.0,pi=3.1415926;cin>>n;while(n--){cin>>x>>y;temp=pi*(x*x+y*y)/2;count=(int)(temp/50+1);cout<<"Property "<<num<<": This property will begin eroding in year "<<count<<"."<<endl;num++;}cout<<"END OF OUTPUT."<<endl;return 0;}1006#include"iostream"using namespace std;int main(){int p,e,i,d;int count=1;int result;while(true){cin>>p>>e>>i>>d;if(p==-1&&e==-1&&i==-1&&d==-1) break;else result = (21252 + 5544 * p + 14421 * e + 1288 * i - d) % 21252;if(!result) result=21252;cout<<"Case "<<count<<": the next triple peak occurs in "<<result<<" days."<<endl;count++;}return 0;}/*Problem: Poj 1019 思路:归纳分析出m位数所有排出来的长度,有下面的a数组---0索引不用。

西北工业大学2020春机考《C语言程序设计》答案 -

西北工业大学2020春机考《C语言程序设计》答案 -

西北工业大学2020春机考《C语言程序设计》作业1单选题1.下面程序的输出结果是()。

main() { int a[10]={1,2,3,4,5,6,7,8,9,10,*p=a;A.3B.4C.1D.2答案:VX:34637870获取参考答案2.以下描述错误的是()。

A.break 语句不能用于循环语句和 switch 语句外的任何其他语句B.在 switch 语句中使用 break 语句或 continue 语句的作用相同C.在循环语句中使用 continue 语句是为了结束本次循环,而不是终止整个循环D.在循环语句中使用 break 语句是为了使流程跳出循环体,提前结束循环答案:VX:34637870获取参考答案3.下面程序的输出结果是()。

main() { int x=10; x+=(x=8); printf("%d\n",x); }A.10B.8C.18D.16答案:VX:34637870获取参考答案4.定义 int i=1; 则执行语句 while(i++<5); 后,i 的值为()。

A.3B.4C.5D.6答案:VX:34637870获取参考答案5.若有语句 scanf("%d%d",&a,&b);要使变量 a,b 分别得到 10 和 20,正确的输入形式为()。

A.10 20B.10,20C.1020D.10:20答案:VX:34637870获取参考答案6.有以下定义 #include char a[10],*b=a; 不能给 a 数组输入字符串的语句是()。

A.gets(a)B.gets(a[0]);C.gets(&a[0]);D.gets(b)答案:VX:34637870获取参考答案7.当 c 的值不为 0 时,在下列选项中能够将 c 的值赋给变量 a、b 的是()。

A.c=b=a;B.(a=c)||(b=c);C.(a=c)&&(b=c);答案:VX:34637870获取参考答案8.以下描述中正确的是()。

西工大C语言POJ习题答案

西工大C语言POJ习题答案

一.1.第一季10题全(注:第五题问题已经解决,确认AC!)之勘阻及广创作#include <stdio.h>int main(){int a,b,sum;scanf("%d%d",&a,&b);sum=a+b;printf("%d\n",sum);return 0;}2.#include <stdio.h>#define PI 3.1415926int main(){double r,h,l,s,sq,vq,vz;scanf("%lf%lf",&r,&h);l=2*PI*r;s=PI*r*r;sq=4*PI*r*r;vq=4*PI*r*r*r/3;vz=s*h;printf("%.2lf\n%.2lf\n%.2lf\n%.2lf\n%.2lf\n",l,s,sq,v q,vz); return 0;}3.#include <stdio.h>int main(){int a,b,c;double d,e;scanf("%d%d%d",&a,&b,&c);d=a+b+c;e=d/3;printf("%lf\n%lf\n",d,e);}4.#include <stdio.h>int main(){int a,b,c;scanf("%d%d%d",&a,&b,&c);if(a<b)a=b;if(a<c)a=c;printf("%d\n",a);}5.#include<stdio.h>int main(){int i=0,j=0,k=1;char a[6];while((a[i]=getchar())!='\n'){ i++;}for(;i>0;i--){if(a[j]==a[i-1]){j++;continue;}else {k=0;break;}}if(k==1)printf("yes\n");elseprintf("no\n"); }6.#include<stdio.h>int main(){double a,c;scanf("%lf",&a);switch((int)a/10) {case 0:c=a*0.1;break;case 1:c=(a-10)*0.075+10*0.1;break;case 2:case 3:c=(a-20)*0.05+10*0.075+10*0.1;break;case 4: case 5:c=(a-40)*0.03+20*0.05+10*0.075+10*0.1;break;case 6:case 7:case 8:case 9:c=(a-60)*0.015+20*0.03+20*0.05+10*0.075+10*0.1;break;default:c=(a-100)*0.01+40*0.015+20*0.03+20*0.05+10*0.075+10*0.1;}printf("%lf\n",c);return 0;}7.#include<stdio.h>int main(){double a,b,c;scanf("%lf",&a);c=(int)a;if(a>c)a=c+1;if(a>15)b=(a-15)*2.1+7+13*1.5;else {if(a>2)b=(a-2)*1.5+7;else b=7; } printf("%lf\n",b);return 0;}8.#include <stdio.h>int main(){int a,b,c,e,f=30,g=31,n;scanf("%d-%d-%d",&a,&b,&c);if((a%400==0)||(a%100!=0&&a%4==0))e=29;elsee=28;switch (b){case 1:n=c;break;case 2:n=g+c;break;case 3:n=g+e+c;break; case 4:n=g+e+g+c;break;case 5:n=g+e+g+f+c;break;case 6:n=g+e+g+f+g+c;break;case 7:n=g+e+g+f+g+f+c;break;case 8:n=g+e+g+f+g+f+g+c;break;case 9:n=g+e+g+f+g+f+g+g+c;break;case 10:n=g+e+g+f+g+f+g+g+f+c;break;case 11:n=g+e+g+f+g+f+g+g+f+g+c;break;default: n=g+e+g+f+g+f+g+g+f+g+f+c;}printf("%d\n",n);return 0;}9.#include <stdio.h>int main(){int x;scanf("%d",&x);if(x>=90&&x<=100)printf("A\n");else if (x>=80)printf("B\n");else if (x>=70)printf("C\n");else if (x>=60)printf("D\n");elseprintf("E\n");return 0;}10.#include<stdio.h>int main(){double x,y,s;scanf("%lf,%lf",&x,&y);s=(x+2)*(x+2)+(y-2)*(y-2);if(s>1){s=(x+2)*(x+2)+(y+2)*(y+2);if(s>1){ s=(x-2)*(x-2)+(y+2)*(y+2);if(s>1){s=(x-2)*(x-2)+(y-2)*(y-2);if(s>1){printf("0\n");return 1;}}}}printf("10\n");return 0;}二。

西工大noj标准答案版.doc

西工大noj标准答案版.doc

西北工业大学 POJ答案绝对是史上最全版(不止100哦⋯⋯按首字母排序)1.“ 1“的传奇2.A+B3.A+BⅡ4.AB5.ACKERMAN6.Arithmetic Progressions7.Bee8.Checksum algorithm9.Coin Test10.Dexter need help11.Double12.Easy problem13.Favorite number14.Graveyard15.Hailstone16.HanoiⅡ17.Houseboat18.Music Composer19.Redistribute wealth20.Road trip21.Scoring22.Specialized Numbers23.Sticks24.Sum of Consecutive25.Symmetric Sort26.The Clock27.The Ratio of gainers to losers28.VOL 大学乒乓球比赛29.毕业设计论文打印30.边沿与内芯的差31.不会吧,又是 A+B32.不屈的小蜗33.操场训练34.插入链表节点35.插入排序36.插入字符37.成绩表计算38.成绩转换39.出租车费40.除法41.创建与遍历职工链表42.大数乘法43.大数除法44.大数加法45.单词频次46.迭代求根47.多项式的猜想48.二分查找49.二分求根50.发工资的日子51.方差52.分离单词53.分数拆分54.分数化小数55.分数加减法56.复数57.高低交换58.公园喷水器59.韩信点兵60.行程编码压缩算法61.合并字符串62.猴子分桃63.火车站64.获取指定二进制位65.积分计算66.级数和67.计算 A+B68.计算 PI69.计算π70.计算成绩71.计算完全数72.检测位图长宽73.检查图像文件格式74.奖金发放75.阶乘合计76.解不等式77.精确幂乘78.恐怖水母79.快速排序80.粒子裂变81.链表动态增长或缩短82.链表节点删除83.两个整数之间所有的素数84.路痴85.冒泡排序86.你会存钱吗87.逆序整数88.排列89.排列分析90.平均值函数91.奇特的分数数列92.求建筑高度93.区间内素数94.三点顺序95.山迪的麻烦96.删除字符97.是该年的第几天98.是该年的第几天99.数据加密100.搜索字符101.所有素数102.探索合数世纪103.特殊要求的字符串104.特殊整数105.完全数106.王的对抗107.危险的组合108.文件比较109.文章统计110.五猴分桃111.小型数据库112.幸运儿113.幸运数字” 7“114.选择排序115.寻找规律116.循环移位117.延伸的卡片118.羊羊聚会119.一维数组”赋值“120.一维数组”加法“121.勇闯天涯122.右上角123.右下角124.圆及圆球等的相关计算125.圆及圆球等相关计算126.程序员添加行号127.找出数字128.找幸运数129.找最大数130.整数位数131.重组字符串132.子序列的和133.子字符串替换134.自然数立方的乐趣135.字符串比较136.字符串复制137.字符串加密编码138.字符串逆序139.字符串排序140.字符串替换141.字符串左中右142.组合数143.最次方数144.最大乘积145.最大整数146.最小整数147.最长回文子串148.左上角149.左下角1.“ 1“的传奇#include <>#include <>#include <>int main(){int n,i,j,k=0,x=1,y,z,m,p,q,a,s=0;scanf("%d",&n);m=n;for(i=1;i<12;i++){m=m/10;k++;if(m==0)break;}q=n;k=k-1;for(a=1;a<=k;a++){x=x*10;}y=q%x;z=q/x;p=q-y;if(z>=2)s=s+x+z*k*(x/10); elses=s+z*k*(x/10); for(j=p;j<=n;j++) {m=j;for(i=1;i<12;i++){x=m%10;if(x==1)s++;m=m/10;if(m==0)break;}}printf("%d",s);return 0;}2.A+B#include <>int doubi(int n,int m) {n=n+m;n=n%100;return n;}int main(){int t,i,a[100],n,m; scanf("%d",&t);for (i=0;i<=(t-1);i++){scanf("%d%d",&n,&m);a[i]=doubi(n,m);}for (i=0;i<=(t-1);i++)printf("%d\n",a[i]); return 0;}3.A+BⅡ#include <>int main(){int A,B,sum;scanf("%d%d",&A,&B);sum=A+B;printf("%d\n",sum);return 0;}4.AB#include <>#include <>#include <>int main(){char s[100],q[100];double a,b,c;int n=0,i;scanf("%lf%lf",&a,&b);c=a*b;sprintf(s,"%.0lf",c);for(i=0;i<strlen(s);i++){n=n+s[i]-48;}while(n>=10){sprintf(q,"%d",n);n=0;for(i=0;i<strlen(q);i++)n=n+q[i]-48;}printf("%d",n);return 0;}5.ACKERMAN#include <>#include <>int ack(int x,int y){int n;if (x==0) {n=y+1;return n;}else if (y==0) n=ack(x-1,1);else n=ack(x-1,ack(x,y-1));return n;}int main(){int m,b;scanf("%d%d",&m,&b);m=ack(m,b);printf("%d",m);return 0;}6.Arithmetic Progressions #include <>#include <>int g(int n){int i;if(n==1) return 0;if(n==2) return 1;if(n==3) return 1;for(i=2;i<=sqrt(n);i++) if(n%i==0) return 0;return 1;}int f(int a,int b,int c){int i=0,s=a-b;if(c==1&&g(a)==1) return a;if(b==0&&g(a)!=1) return -1;while(1){s=s+b;if(g(s)) i++;if(i>=c) break;}return s;int main(){int a,b,c,d[100],i=0,n;while(1){scanf("%d%d%d",&a,&b,&c);if(a==0&&b==0&&c==0) break;d[i]=f(a,b,c);i++;}n=i;for(i=0;i<n;i++)printf("%d\n",d[i]);return 0;}7.Bee#include <>#include <>int main()int A[100],i=0,j,k,female=0,male=1,x;for(;;i++){scanf("%d",&A[i]);if(A[i]==-1)break;}for(j=0;j<i;j++){female=0,male=1;for(k=1;k<A[j];k++){x=female;female=male;male=x+male+1;}printf("%d %d\n",male,female+male+1);}return 0;}8.Checksum algorithm #include <>#include <>#include <>int main(){int i,n,t,j;char s[100][100];for(i=0;;i++){gets(s[i]);if(s[i][0]=='#') break;}n=i;for(i=0;i<n;i++){t=0;for(j=0;j<strlen(s[i]);j++)if(s[i][j]==32) t=t;else t=t+(j+1)*(s[i][j]-64);printf("%d\n",t);}return 0;}9.Coin Test#include <>#include <>int main(){char A[100000];int n,i=0,a=0,b=0,j;double x;while(1){scanf("%c",&A[i]);if(A[i]=='\n')break;i++;}for(j=0;j<i;j++){if(A[j]=='S'){printf("WA");goto OH;}if(A[j]=='U')a++;if(A[j]=='D')b++;}x=a*(a+b)*;if>||<printf("Fail");elseprintf("%d/%d",a,a+b);OH:return 0;}10.Dexter need help #include <>int fun(int a){if(a==1) return 1;elsereturn fun(a/2)+1; }int main(){int a,b[100],i=0,j; while(1){scanf("%d",&a);if(a==0)break;b[i]=fun(a);i++;}for(j=0;j<i;j++){ printf("%d\n",b[j]); }return 0;}11.Double#include <>#include <>int main(){int a[100],b[100],i,j,n,t=0;for(i=0;;i++){scanf("%d",&a[i]);if(a[i]==0) break;}n=i;for(i=0;i<n;i++)b[i]=2*a[i];for(i=0;i<n;i++)for(j=0;j<n;j++)if(a[i]==b[j]) t++;printf("%d",t);return 0;}12.Easy problem#include <>#include <>int main(){int N,i,n,j=0;scanf("%d",&N);for(i=2;i<N+1;i++){if((N+1)%i==0)j++;}printf("%d",j/2);return 0;}13.Favorite number #include <>#include <>#define MAXNUM 100000int prime_number = 0;int prime_list[MAXNUM]; bool is_prime[MAXNUM]; int ans[MAXNUM + 2];int dp[MAXNUM + 2];void set_prime() {int i, j;memset(is_prime, 0, sizeof(is_prime));for (i = 2; i < MAXNUM; i++) {if (is_prime[i] == 0) {prime_list[prime_number++] = i;if (i >= MAXNUM / i) continue;for (j = i * i; j < MAXNUM; j+=i) {is_prime[j] = 1;}}}}int main() {int i, j, k,o=0,d[100];memset(dp, -1, sizeof(dp));set_prime();ans[0] = 0;dp[1] = 0;for (i = 1; i <= MAXNUM; i++) {ans[i] = ans[i - 1] + dp[i];if (dp[i + 1] == -1 || dp[i + 1] > dp[i] + 1) {dp[i + 1] = dp[i] + 1;}for (j = 0; j < prime_number; j++) {if (i > MAXNUM / prime_list[j]) break;k = i * prime_list[j];if (dp[k] == -1 || dp[k] > dp[i] + 1) {dp[k] = dp[i] + 1;}}}while (scanf("%d%d", &i, &j) == 2 && (i || j)){ d[o]=ans[j] - ans[i - 1];o++;}for(i=0;i<o;i++)printf("%d\n",d[i]);}14.Graveyard#include <>#include <>#include <>int main(){int a[100],b[100],n,i,j;double s,p,l,t;for(i=0;;i++){scanf("%d%d",&a[i],&b[i]);if(a[i]==0&&b[i]==0) break;}n=i;for(i=0;i<n;i++){p=10000;if(b[i]%a[i]==0){printf("\n");continue;};t=10000/((double)a[i]);for(j=1;j<a[i]+b[i];j++){l=10000/((double)(a[i]+b[i]));l=t-j*l;l=fabs(l);if(l<p) p=l;}s=(a[i]-1)*p;printf("%.4lf\n",s);}return 0;}15.Hailstone#include <>#include <>#include <>int f(int n){int s=1;while(1){if(n==1) return s;else if(n%2==0) n=n/2,s++;else n=3*n+1,s++;}}int main(){int n,m,i,j=0,t;scanf("%d%d",&m,&n);printf("%d %d",m,n);if(m>n) t=m,m=n,n=t;for(i=m;i<=n;i++)if(f(i)>j) j=f(i);printf(" %d",j);return 0;}16.HanoiⅡ#include <>#include <>#define M 70int start[M], targe[M];long long f(int *p, int k, int fina) {if(k==0) return 0;if(p[k]==fina) return f(p,k-1,fina);return f(p,k-1,6-fina-p[k])+(1LL<<(k-1));}int main (){long long ans;int n;while(scanf("%d",&n),n){int i;for(i=1;i<=n;i++) scanf("%d",&start[i]);for(i=1;i<=n;i++) scanf("%d",&targe[i]);int c=n;for(;c>=1&&start[c]==targe[c];c--);if(c==0){printf("0\n"); continue;}int other=6-start[c]-targe[c]; ans=f(start,c-1,other)+f(targe,c-1,other)+1;printf("%lld\n",ans);}return 0;}17.Houseboat#include <>#include <>#include <>#define piint f(float x,float y){int i;for(i=0;;i++)if(50*i>sqrt(x*x+y*y)*sqrt(x*x+y*y)*pi/2) break;return i;}int main(){int n,i,a[100];float x,y;scanf("%d",&n);for(i=0;i<n;i++){scanf("%f%f",&x,&y);a[i]=f(x,y);}for(i=0;i<n;i++)printf("%d %d\n",i+1,a[i]);return 0;}18.Music Composer19.Redistribute wealth#include <>#include <>#include <>int main(){inta[1000],b[1000],n,i,j,s,sum,t,m,mid,c[100],k=0;while(1){scanf("%d",&n);if(n==0) break;{s=0;for(i=1;i<=n;i++){scanf("%d",&a[i]);s=s+a[i];}m=s/n;b[1]=a[1]-m;b[0]=0;for(i=2;i<n;++i)b[i]=b[i-1]+a[i]-m;for(i=0;i<n;i++)for(j=0;j<n-1-i;j++)if(b[j]>b[j+1])t=b[j],b[j]=b[j+1],b[j+1]=t;mid=b[n/2];sum=0;for(i=0;i<=n-1;++i) sum=sum+fabs(mid-b[i]);c[k]=sum;k++;}}for(i=0;i<k;i++) printf("%d\n",c[i]);return 0;}20.Road trip#include <>#include <>#include <>int f(int n){int a[100],b[100],i,s;for(i=0;i<n;i++)scanf("%d%d",&a[i],&b[i]);s=a[0]*b[0];for(i=1;i<n;i++)s=s+a[i]*(b[i]-b[i-1]);return s;}int main(){int n,c[100],i=0;while(1){scanf("%d",&n);if(n==-1) break;c[i]=f(n);i++;}n=i;for(i=0;i<n;i++)printf("%d\n",c[i]);return 0;}21.Scoring#include <>#include <>#include <>int main(){int i,j,sum,min,c,count,n,a,b; char s1[50],s2[50];scanf("%d",&n);for(i=0;i<n;i++){count=sum=0;scanf("%s",s2);for(j=0;j<4;j++){scanf("%d%d",&a,&b);if(b!=0){sum+=(a-1)*20+b;count++;}}if(i==0){c=count,min=sum;strcpy(s1,s2);}else if(count>c||(count==c&&sum<min)) {min=sum;c=count;strcpy(s1,s2);}}printf("%s %d %d\n",s1,c,min); return 0;}22.Specialized Numbers#include <>#include <>int main(){int i,n,sum10,sum12,sum16;for(i=2992;i<3000;i++){n=i;sum10=0;while(n){sum10+=n%10;n/=10;}n=i;sum12=0;while(n){sum12+=n%12;n/=12;}n=i;sum16=0;while(n){sum16+=n%16;n/=16;}if(sum10==sum12&&sum12==sum16) printf("%d\n",i);}return 0;}23.Sticks#include <>#include <>#include <>int len[64], n, minlen, get;bool b[64];int cmp(const void *a, const void *b) {return *(int *)a < *(int *)b 1 : -1;}bool dfs(int nowlen, int nowget, int cnt) {if(cnt >= n) return false;if(get == nowget) return true;int i;bool f = false;if(nowlen == 0) f = true;for(i = cnt; i < n; i++){if(!b[i]){if(len[i] + nowlen == minlen){b[i] = true;if(dfs(0, nowget+1, nowget))return true;b[i] = false;return false;}else if(len[i] + nowlen < minlen){b[i] = true;if(dfs(nowlen+len[i], nowget, i+1))return true;b[i] = false;if(f) return false;while(i+ 1 < n && len[i] == len[i+1]) i++;}}}return false;}int main(){int i, tollen;while(scanf("%d", &n), n){tollen = 0;int j = 0, p;for(i = 0; i < n; i++){scanf("%d", &p);if(p <= 50){len[j] = p;tollen += len[j];j++;}}n = j;if(n == 0){printf("0\n");continue;}qsort(len, n, sizeof(int), cmp); for(minlen = len[0]; ; minlen++) {if(tollen % minlen) continue;memset(b, 0, sizeof(b));get = tollen / minlen;if(dfs(0, 0, 0)){printf("%d\n", minlen);break;}}}return 0;}24.Sum of Consecutive#include <>#include <>#include <>int len[64],n,minlen,get;int b[64];int cmp(const void *a,const void *b) {return *(int *)a<*(int *)b1:-1;}int dfs(int nowlen,int nowget,int cnt){if(cnt>=n) return 0;if(get==nowget) return 1;int i,f=0;if(nowlen==0) f=1;for(i=cnt;i<n;i++){if(len[i]+nowlen==minlen){b[i]=1;if(dfs(0,nowget+1,nowget)) return 1;b[i]=0;return 0;}else if(len[i]+nowlen<minlen){b[i]=1;if(dfs(nowlen+len[i],nowget,i+1)) return 1;b[i]=0;if(f) return 0;while(i+1<n&&len[i]==len[i+1]) i++;}}return 0;}int main(){int i,tollen,q=0,c[100];while(scanf("%d",&n),n){tollen=0;int j=0,p;for(i=0;i<n;i++){scanf("%d",&p);if(p<=50){len[j]=p;tollen+=len[j];j++;}}n=j;if(n==0){printf("0\n");continue;}qsort(len,n,sizeof(int),cmp);for(minlen=len[0];;minlen++){if(tollen%minlen) continue;memset(b,0,sizeof(b));get=tollen/minlen;if(dfs(0,0,0)){c[q]=minlen;q++;break;}}}for(i=0;i<q;i++)printf("%d\n",c[i]);return 0;}25.Symmetric Sort#include <>#include <>#include <>int main(){double A[100];int i=0,j=0,k=0,l=0,sum=0;while(1){scanf("%lf",&A[i]);if(A[i]==0)break;i++;}for(j=0;j<i;j++){if(A[j]==2)printf("1\n");else{int B[10000],m=1,number=0;double n;B[0]=2;for(k=3;k<=A[j];k+=2){n=(double)k;for(l=2;l<=sqrt(n);l++){if(k%l==0)goto ai;}B[m]=k;m++;ai:;}for(k=0;k<m;k++){sum=0;for(l=k;l<m;l++){sum+=B[l];if(sum==A[j]){number++;break;}}}printf("%d\n",number);}}return 0;}26.The Clock#include <>#include <>#include <>int main(){char s[100][100],a[100];int i,j,n;scanf("%d",&n);for(i=0;i<n;i++) scanf("%s",s[i]);for(i=0;i<n-1;i++)for(j=0;j<n-1-i;j++)if(strlen(s[i])>strlen(s[i+1]))strcpy(a,s[i]),strcpy(s[i],s[i+1]),strcpy(s[i+1],a) ;if(n%2==0){for(i=0;i<n-1;i=i+2) printf("%s ",s[i]);printf("%s ",s[n-1]);for(i=i-3;i>0;i=i-2) printf("%s ",s[i]);}else{for(i=0;i<n-1;i=i+2) printf("%s ",s[i]);printf("%s ",s[n-1]);for(i=i-1;i>0;i=i-2) printf("%s ",s[i]);}return 0;}27.The Ratio of gainers to losers #include<>int main(){char s[5];int i,sum=0;gets(s);for(i=0;s[i]!='\0';i++){switch(s[i]){case'I': sum+=1;break; case'V': sum=5-sum;break; case'X':sum=10-sum;break; }}printf("%d\n",sum);return 0;}28.VOL大学乒乓球比赛#include <>#include <>int main(){printf("A=Z\nB=X\nC=Y\n");return 0;}29.毕业设计论文打印#include <>#include <>int main(){int a[100],j=1,i,n,m;scanf("%d%d",&n,&m);for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=0;i<n;i++)if(a[i]>a[m]) j++;printf("%d",j++);return 0;}30.边沿与内芯的差#include <>#include <>int main(){int A[100][100],i,j,m,n,s=0,t=0;scanf("%d%d",&n,&m);for(i=1;i<=n;i++){for(j=1;j<=m;j++){scanf("%d",&A[i][j]);}。

西工大POJ100题(全新)

西工大POJ100题(全新)

西工大POJ100题(全新)1计算A+B2圆及圆球等的相关计算3计算成绩4找最大数5找幸运数6奖金发放7出租车费8是该年的第几天9成绩转换10求建筑高度11区间内素数12计算π13两个整数之间所有的素数14最次方数15自然数立方的乐趣16五猴分桃17完全数18二分求根19你会存钱吗?20级数和21幸运数字722粒子裂变23特殊整数24最大乘积25解不等式26危险的组合27子序列的和29探索合数世纪30韩信点兵31亲和数32高低交换33循环移位35组合数36积分计算37数据加密38获取指定二进制位39__N40不会吧,又是A+B 41平均值函数42插入排序43一维数组赋值44右下角45右上角46山迪的麻烦47冒泡排序48恐怖水母49左上角50一维数组加法51字符串排序52字符串左中右53文章统计54找出数字55字符串比较56THE CLOCK57合并字符串58特殊要求的字符串59字符串逆序60COIN TEST61小型数据库62单词频次65检测位图长宽67子字符串替换68复数69字符串加密编码70重组字符串71大数除法72创建与遍历职工链表73大数加法74大数减法75链表节点删除76链表动态增长或缩小77大数乘法78精确乘幂80火车站81操场训练82__NE84阶乘合计85王的对抗86三点顺序87公园喷水器88勇闯天涯89不屈的小蜗90THE RATIO OF __ TO LOSERS91__RD92__TIC __SIONS93__94__AT97BEE98__M __HM99Hanoi 2100Specialized number1计算A+B#includestdio.hint main(){int a,b,sum;scanf(“%d %d",a,sum=a+b;printf("%d",sum); }2圆及圆球等的相关计算#includestdio.hint main(){float r,h,l,s,sq,vq,vz,pi=3.__-__3; scanf("%f %f",r,l=2*pi*r;s=pi*r*r;sq=4*pi*r*r;vq=4.0/3*pi*r*r*r;vz=pi*r*r*h;printf("%.2f\n%.2f\n%.2f\n%.2f\n%.2f\n",l,s,sq,vq,vz); return 0; }3计算成绩#include stdio.hint main(){float a,b,c,sum,ave;scanf("%f %f %f",a,b,sum=a+b+c;ave=sum/3.0;printf("%.6f\n%.6f",sum,ave);return 0;}4找最大数#includestdio.hint main(){int a,b,c,t;scanf("%d %d %d",a,b, if (ab) t=a,a=b;if(ac) t=a,a=c;printf("%d",a); return 0;}5找幸运数#include stdio.hint main(){int i,j,a;scanf("%d",i=0;j=a;while(j){i=10*i+(j%10);j=j/10;}if(i==a)printf("yes\n");else printf("no\n");return 0;}6奖金发放#includestdio.hint main (){float l;scanf("%f",if (l=10)l=l*0.1;else if(l=20)l=10*0.1+(l-10)*0.075;else if(l=40)l=10*0.1+10*0.075+(l-20)*0.05; elseif(l=60)l=10*0.1+10*0.075+20*0.05+(l-40)*0.03; elseif(l=100)l=10*0.1+10*0.075+20*0.05+20*0.03+(l-60)*0.015; elsel=10*0.1+10*0.075+20*0.05+20*0.03+40*0.015+(l-100)*0.01; printf("%.6f",l);return 0;}7出租车费#includestdio.h#includemath.hint main(){float a,n,t;scanf("%f",if (a=2) n=7;else if (a=15) {t=a-floor(a);if(t==0)n=7+(floor(a)-2)*1.5;else n=7+(floor(a)-1)*1.5;}else {t=a-floor(a);if(t==0) n=7+13*1.5+(floor(a)-15)*2.1; else n=7+13*1.5+(floor(a)-14)*2.1;} printf("%.6f",n);return 0;}8是该年的第几天#includestdio.hint main(){int y,m,d,n,a;scanf("%d-%d-%d",y,m,if((y%4==0y%100!=0)||(y%400==0)) a=29; else a=28; switch(m){case 1:n=d;break;case 2:n=31+d;break;case 3:n=31+a+d;break;case 4:n=62+a+d;break;case 5:n=92+a+d;break;case 6:n=123+a+d;break;case 7:n=153+a+d;break;case 8:n=184+a+d;break;case 9:n=215+a+d;break;case 10:n=245+a+d;break;case 11:n=276+a+d;break;case 12:n=306+a+d;break;}printf("%d",n);return 0;}9成绩转换#includestdio.hint main()int n;scanf("%d",if(n60)printf("E\n");else if(n70)printf("D\n");else if(n80)printf("C\n");else if(n90)printf("B\n");else printf("A\n");return 0;}10求建筑高度#includestdio.hint main(){float x,y,a,b,c,d;scanf("%f,%f",x,a=(x-2)*(x-2)+(y-2)*(y-2);b=(x-2)*(x-2)+(y+2)*(y+2);c=(x+2)*(x+2)+(y-2)*(y-2);d=(x+2)*(x+2)+(y+2)*(y+2);if(a=1||b=1||c=1||d=1)printf("10"); else printf("0"); return 0;11区间内素数#includestdio.h#includemath.hint main(){int i,n=500,cnt=0,sum=0;while(n=800){for (i=2;i=n-1;i++)if (n%i==0)break;if (i==n)cnt=cnt+1,sum=sum+pow((-1),cnt)*n; n=n+1; }printf("%d %d",cnt,sum);return 0;}12计算π#includestdio.h#includemath.hint main(){int s=1;double pi=0,n=1,t=1;while (fabs(t)1e-6)pi=pi+t,n=n+2,s=-s,t=s/n; pi=pi*4;printf("%.6f",pi);return 0;}13两个整数之间所有的素数#includestdio.hint main(){int a,b,t,i;scanf("%d%d",a,if(ab)t=a,a=b,b=t;while (a=b){for (i=2;i=a-1;i++)if (a%i==0)break;if(i==a)printf("%d ",a);a++;}return 0;}14最次方数#includestdio.hint main(){int x,a,m=1,n=1;scanf("%d %d",x,while (m=a){n=n__;n=n%1000;m=m+1;}printf("%d",n);return 0;}15自然数立方的乐趣#includestdio.hint main(){int n,a,i,t;scanf("%d",a=n*(n-1)+1;t=n*n*n;printf("%d*%d*%d=%d=",n,n,n,t);for(i=1;ii++){printf("%d+",a);a=a+2;}printf("%d",a);return 0;}16五猴分桃#includestdio.hint divides(int n,int m){if (n/5==0||n%5!=1)return 0;if (m==1)return 1;return divides(n-n/5-1,m-1);}int main(){int n,a;for (n=1;;n++)if(divides(n,5)){printf("%d ",n);break;}a=(((((n-1)*4/5-1)*4/5-1)*4/5-1)*4/5-1)*4/5; printf("%d",a);return 0;}17完全数#includestdio.hint main(){int i,n,sum=0;for(n=2;n1000;n++){sum=0;for (i=1;i=n/2;i++){if(n%i==0)sum=sum+i;}if(sum==n){printf("%d=",n); for(i=1;ii++)if(n%i==0)printf("%d+",i);i=n/2;printf("%d\n",i);}}return 0;}18二分求根#includemath.h#includestdio.hdouble fun(double x) {return 2__-4__+3__-6;} double root(double a,double b,double e){double x1,x2,y1,x,y;x1=a;x2=b;do {x=(x1 + x2)/2;y=fun(x);y1=fun(x1);if((y0y10)||(y0y10))x1=x;elsex2=x;/*end if*/}while(fabs(y)return x;}int main(void){double m,n;scanf("%f %f",m,double x=root(m,n,1e-8);printf("%.2f\n",x);return 0;}19你会存钱吗?#includestdio.h#includemath.hint main(){int i8,i5,i3,i2,i1,n8,n5,n3,n2,n1;float max=0,term;for(i8=0;i8i8++) /*穷举所有可能的存款方式*/。

西工大noj问题详解解析汇报(完整版)

西工大noj问题详解解析汇报(完整版)

西北工业大学POJ答案绝对是史上最全版(不止100题哦……按首字母排序)1.“1“的传奇2.A+B3.A+BⅡ4.AB5.ACKERMAN6.Arithmetic Progressions7.Bee8.Checksum algorithm9.Coin Test10.Dexter need help11.Double12.Easy problem13.Favorite number14.Graveyard15.Hailstone16.Hanoi Ⅱ17.Houseboat18.Music Composer19.Redistribute wealth20.Road trip21.Scoring22.Specialized Numbers23.Sticks24.Sum of Consecutive25.Symmetric Sort26.The Clock27.The Ratio of gainers to losers28.VOL大学乒乓球比赛29.毕业设计论文打印30.边沿与内芯的差31.不会吧,又是A+B32.不屈的小蜗33.操场训练34.插入链表节点35.插入排序36.插入字符37.成绩表计算38.成绩转换39.出租车费40.除法41.创建与遍历职工链表42.大数乘法43.大数除法44.大数加法45.单词频次46.迭代求根47.多项式的猜想48.二分查找49.二分求根50.发工资的日子51.方差52.分离单词53.分数拆分54.分数化小数55.分数加减法56.复数57.高低交换58.公园喷水器59.韩信点兵60.行程编码压缩算法61.合并字符串62.猴子分桃63.火车站64.获取指定二进制位65.积分计算66.级数和67.计算A+B68.计算PI69.计算π70.计算成绩71.计算完全数72.检测位图长宽73.检查图像文件格式74.奖金发放75.阶乘合计76.解不等式77.精确幂乘78.恐怖水母79.快速排序80.粒子裂变81.链表动态增长或缩短82.链表节点删除83.两个整数之间所有的素数84.路痴85.冒泡排序86.你会存钱吗87.逆序整数88.排列89.排列分析90.平均值函数91.奇特的分数数列92.求建筑高度93.区间内素数94.三点顺序95.山迪的麻烦96.删除字符97.是该年的第几天98.是该年的第几天?99.数据加密100.搜索字符101.所有素数102.探索合数世纪103.特殊要求的字符串104.特殊整数105.完全数106.王的对抗107.危险的组合108.文件比较109.文章统计110.五猴分桃111.小型数据库112.幸运儿113.幸运数字”7“114.选择排序115.寻找规律116.循环移位117.延伸的卡片118.羊羊聚会119.一维数组”赋值“120.一维数组”加法“121.勇闯天涯122.右上角123.右下角124.圆及圆球等的相关计算125.圆及圆球等相关计算126.程序员添加行号127.找出数字128.找幸运数129.找最大数130.整数位数131.重组字符串132.子序列的和133.子字符串替换134.自然数立方的乐趣135.字符串比较136.字符串复制137.字符串加密编码138.字符串逆序139.字符串排序140.字符串替换141.字符串左中右142.组合数143.最次方数144.最大乘积145.最大整数146.最小整数147.最长回文子串148.左上角149.左下角1.“1“的传奇#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){int n,i,j,k=0,x=1,y,z,m,p,q,a,s=0;scanf("%d",&n);m=n;for(i=1;i<12;i++){m=m/10;k++;if(m==0)break;}q=n;k=k-1;for(a=1;a<=k;a++){x=x*10;}y=q%x;z=q/x;p=q-y;if(z>=2)s=s+x+z*k*(x/10); elses=s+z*k*(x/10); for(j=p;j<=n;j++) {m=j;for(i=1;i<12;i++){x=m%10;if(x==1)s++;m=m/10;if(m==0)break;}}printf("%d",s);return 0;}2.A+B#include <stdio.h>int doubi(int n,int m){n=n+m;n=n%100;return n;}int main(){int t,i,a[100],n,m;scanf("%d",&t);for (i=0;i<=(t-1);i++){scanf("%d%d",&n,&m);a[i]=doubi(n,m);}for (i=0;i<=(t-1);i++)printf("%d\n",a[i]);return 0;}3.A+BⅡ#include <stdio.h>int main(){int A,B,sum;scanf("%d%d",&A,&B);sum=A+B;printf("%d\n",sum);return 0;}4.AB#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){char s[100],q[100];double a,b,c;int n=0,i;scanf("%lf%lf",&a,&b);c=a*b;sprintf(s,"%.0lf",c);for(i=0;i<strlen(s);i++){n=n+s[i]-48;}while(n>=10){sprintf(q,"%d",n);n=0;for(i=0;i<strlen(q);i++)n=n+q[i]-48;}printf("%d",n);return 0;}5.ACKERMAN#include <stdio.h>#include <stdlib.h>int ack(int x,int y){int n;if (x==0) {n=y+1;return n;}else if (y==0) n=ack(x-1,1);else n=ack(x-1,ack(x,y-1));return n;}int main(){int m,b;scanf("%d%d",&m,&b);m=ack(m,b);printf("%d",m);return 0;}6.Arithmetic Progressions#include <stdio.h>#include <math.h>int g(int n){int i;if(n==1) return 0;if(n==2) return 1;if(n==3) return 1;for(i=2;i<=sqrt(n);i++) if(n%i==0) return 0;return 1;}int f(int a,int b,int c){int i=0,s=a-b;if(c==1&&g(a)==1) return a;if(b==0&&g(a)!=1) return -1;while(1){s=s+b;if(g(s)) i++;if(i>=c) break;}return s;int main(){int a,b,c,d[100],i=0,n;while(1){scanf("%d%d%d",&a,&b,&c);if(a==0&&b==0&&c==0) break;d[i]=f(a,b,c);i++;}n=i;for(i=0;i<n;i++)printf("%d\n",d[i]);return 0;}7.Bee#include <stdio.h>#include <stdlib.h>int main()int A[100],i=0,j,k,female=0,male=1,x;for(;;i++){scanf("%d",&A[i]);if(A[i]==-1)break;}for(j=0;j<i;j++){female=0,male=1;for(k=1;k<A[j];k++){x=female;female=male;male=x+male+1;}printf("%d %d\n",male,female+male+1);}return 0;}8.Checksum algorithm#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){int i,n,t,j;char s[100][100];for(i=0;;i++){gets(s[i]);if(s[i][0]=='#') break;}n=i;for(i=0;i<n;i++){t=0;for(j=0;j<strlen(s[i]);j++)if(s[i][j]==32) t=t;else t=t+(j+1)*(s[i][j]-64);printf("%d\n",t);}return 0;}9.Coin Test#include <stdio.h>#include <stdlib.h>int main(){char A[100000];int n,i=0,a=0,b=0,j;double x;while(1){scanf("%c",&A[i]);if(A[i]=='\n')break;i++;}for(j=0;j<i;j++){if(A[j]=='S'){printf("WA");goto OH;}if(A[j]=='U')a++;if(A[j]=='D')b++;}x=a*1.0/(a+b)*1.0;if(x-0.5>0.003||x-0.5<-0.003) printf("Fail");elseprintf("%d/%d",a,a+b);OH:return 0;}10.Dexter need help#include <stdio.h>int fun(int a){if(a==1) return 1;elsereturn fun(a/2)+1;}int main(){int a,b[100],i=0,j; while(1){scanf("%d",&a);if(a==0)break;b[i]=fun(a);i++;}for(j=0;j<i;j++){printf("%d\n",b[j]); }return 0;}11.Double#include <stdio.h>#include <stdlib.h>int main(){int a[100],b[100],i,j,n,t=0;for(i=0;;i++){scanf("%d",&a[i]);if(a[i]==0) break;}n=i;for(i=0;i<n;i++)b[i]=2*a[i];for(i=0;i<n;i++)for(j=0;j<n;j++)if(a[i]==b[j]) t++;printf("%d",t);return 0;}12.Easy problem#include <stdio.h>#include <math.h>int main(){int N,i,n,j=0;scanf("%d",&N);for(i=2;i<N+1;i++){if((N+1)%i==0)j++;}printf("%d",j/2);return 0;}13.Favorite number #include <stdio.h>#include <string.h>#define MAXNUM 100000int prime_number = 0;int prime_list[MAXNUM]; bool is_prime[MAXNUM]; int ans[MAXNUM + 2];int dp[MAXNUM + 2];void set_prime() {int i, j;memset(is_prime, 0, sizeof(is_prime));for (i = 2; i < MAXNUM; i++) {if (is_prime[i] == 0) {prime_list[prime_number++] = i;if (i >= MAXNUM / i) continue;for (j = i * i; j < MAXNUM; j+=i) {is_prime[j] = 1;}}}}int main() {int i, j, k,o=0,d[100];memset(dp, -1, sizeof(dp));set_prime();ans[0] = 0;dp[1] = 0;for (i = 1; i <= MAXNUM; i++) {ans[i] = ans[i - 1] + dp[i];if (dp[i + 1] == -1 || dp[i + 1] > dp[i] + 1) {dp[i + 1] = dp[i] + 1;}for (j = 0; j < prime_number; j++) {if (i > MAXNUM / prime_list[j]) break;k = i * prime_list[j];if (dp[k] == -1 || dp[k] > dp[i] + 1) {dp[k] = dp[i] + 1;}}}while (scanf("%d%d", &i, &j) == 2 && (i || j)) { d[o]=ans[j] - ans[i - 1];o++;}for(i=0;i<o;i++)printf("%d\n",d[i]);}14.Graveyard#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){int a[100],b[100],n,i,j;double s,p,l,t;for(i=0;;i++){scanf("%d%d",&a[i],&b[i]);if(a[i]==0&&b[i]==0) break;}n=i;for(i=0;i<n;i++){p=10000;if(b[i]%a[i]==0){printf("0.0000\n");continue;};t=10000/((double)a[i]);for(j=1;j<a[i]+b[i];j++){l=10000/((double)(a[i]+b[i]));l=t-j*l;l=fabs(l);if(l<p) p=l;}s=(a[i]-1)*p;printf("%.4lf\n",s);}return 0;}15.Hailstone#include <stdio.h>#include <stdlib.h>#include <string.h>int f(int n){int s=1;while(1){if(n==1) return s;else if(n%2==0) n=n/2,s++;else n=3*n+1,s++;}}int main()int n,m,i,j=0,t;scanf("%d%d",&m,&n);printf("%d %d",m,n);if(m>n) t=m,m=n,n=t;for(i=m;i<=n;i++)if(f(i)>j) j=f(i);printf(" %d",j);return 0;}16.Hanoi Ⅱ#include <stdio.h>#include <stdlib.h>#define M 70int start[M], targe[M];long long f(int *p, int k, int fina){if(k==0) return 0;if(p[k]==fina) return f(p,k-1,fina); return f(p,k-1,6-fina-p[k])+(1LL<<(k-1));int main (){long long ans;int n;while(scanf("%d",&n),n){int i;for(i=1;i<=n;i++) scanf("%d",&start[i]);for(i=1;i<=n;i++) scanf("%d",&targe[i]);int c=n;for(;c>=1&&start[c]==targe[c];c--);if(c==0){printf("0\n"); continue;}int other=6-start[c]-targe[c];ans=f(start,c-1,other)+f(targe,c-1,other)+1;printf("%lld\n",ans);}return 0;}17.Houseboat#include <stdio.h>#include <stdlib.h>#include <math.h>#define pi 3.1415926int f(float x,float y){int i;for(i=0;;i++)if(50*i>sqrt(x*x+y*y)*sqrt(x*x+y*y)*pi/2) break;return i;}int main(){int n,i,a[100];float x,y;scanf("%d",&n);for(i=0;i<n;i++){scanf("%f%f",&x,&y);a[i]=f(x,y);}for(i=0;i<n;i++)printf("%d %d\n",i+1,a[i]);return 0;}18.Music Composer19.Redistribute wealth#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){int a[1000],b[1000],n,i,j,s,sum,t,m,mid,c[100],k=0;while(1){scanf("%d",&n);if(n==0) break;{s=0;for(i=1;i<=n;i++){scanf("%d",&a[i]);s=s+a[i];}m=s/n;b[1]=a[1]-m;b[0]=0;for(i=2;i<n;++i)b[i]=b[i-1]+a[i]-m;for(i=0;i<n;i++)for(j=0;j<n-1-i;j++)if(b[j]>b[j+1]) t=b[j],b[j]=b[j+1],b[j+1]=t;mid=b[n/2];sum=0;for(i=0;i<=n-1;++i) sum=sum+fabs(mid-b[i]);c[k]=sum;k++;}}for(i=0;i<k;i++) printf("%d\n",c[i]);return 0;}20.Road trip#include <stdio.h>#include <stdlib.h>#include <math.h>int f(int n){int a[100],b[100],i,s;for(i=0;i<n;i++)scanf("%d%d",&a[i],&b[i]);s=a[0]*b[0];for(i=1;i<n;i++)s=s+a[i]*(b[i]-b[i-1]);return s;}int main(){int n,c[100],i=0;while(1){scanf("%d",&n);if(n==-1) break;c[i]=f(n);i++;}n=i;for(i=0;i<n;i++)printf("%d\n",c[i]);return 0;}21.Scoring#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){int i,j,sum,min,c,count,n,a,b;char s1[50],s2[50];scanf("%d",&n);for(i=0;i<n;i++){count=sum=0;scanf("%s",s2);for(j=0;j<4;j++){scanf("%d%d",&a,&b);if(b!=0){sum+=(a-1)*20+b;count++;}}if(i==0){c=count,min=sum;strcpy(s1,s2);}else if(count>c||(count==c&&sum<min)){min=sum;c=count;strcpy(s1,s2);}}printf("%s %d %d\n",s1,c,min);return 0;}22.Specialized Numbers#include <stdio.h>#include <stdlib.h>int main(){int i,n,sum10,sum12,sum16;for(i=2992;i<3000;i++){n=i;sum10=0;while(n){sum10+=n%10;n/=10;}n=i;sum12=0;while(n){sum12+=n%12;n/=12;}n=i;sum16=0;while(n){sum16+=n%16;n/=16;}if(sum10==sum12&&sum12==sum16) printf("%d\n",i);}return 0;}23.Sticks#include <stdio.h>#include <string.h>#include <stdlib.h>int len[64], n, minlen, get;bool b[64];int cmp(const void *a, const void *b){return *(int *)a < *(int *)b ? 1 : -1;}bool dfs(int nowlen, int nowget, int cnt){if(cnt >= n) return false;if(get == nowget) return true;int i;bool f = false;if(nowlen == 0) f = true;for(i = cnt; i < n; i++){if(!b[i]){if(len[i] + nowlen == minlen){b[i] = true;if(dfs(0, nowget+1, nowget))return true;b[i] = false;return false;}else if(len[i] + nowlen < minlen){b[i] = true;if(dfs(nowlen+len[i], nowget, i+1))return true;b[i] = false;if(f) return false;while(i + 1 < n && len[i] == len[i+1]) i++;}}}return false;}int main(){int i, tollen;while(scanf("%d", &n), n){tollen = 0;int j = 0, p;for(i = 0; i < n; i++){scanf("%d", &p);if(p <= 50)len[j] = p;tollen += len[j];j++;}}n = j;if(n == 0){printf("0\n");continue;}qsort(len, n, sizeof(int), cmp);for(minlen = len[0]; ; minlen++) {if(tollen % minlen) continue;memset(b, 0, sizeof(b));get = tollen / minlen;if(dfs(0, 0, 0)){printf("%d\n", minlen);break;}}return 0;}24.Sum of Consecutive#include <stdio.h>#include <stdlib.h>#include <string.h>int len[64],n,minlen,get;int b[64];int cmp(const void *a,const void *b) {return *(int *)a<*(int *)b?1:-1; }int dfs(int nowlen,int nowget,int cnt) {if(cnt>=n) return 0;if(get==nowget) return 1;int i,f=0;if(nowlen==0) f=1;for(i=cnt;i<n;i++){if(len[i]+nowlen==minlen){b[i]=1;if(dfs(0,nowget+1,nowget)) return 1;b[i]=0;return 0;}else if(len[i]+nowlen<minlen){b[i]=1;if(dfs(nowlen+len[i],nowget,i+1)) return 1;b[i]=0;if(f) return 0;while(i+1<n&&len[i]==len[i+1]) i++;}}return 0;}int main(){int i,tollen,q=0,c[100];while(scanf("%d",&n),n){tollen=0;int j=0,p;for(i=0;i<n;i++){scanf("%d",&p);if(p<=50){len[j]=p;tollen+=len[j];j++;}}n=j;if(n==0){printf("0\n"); continue;}qsort(len,n,sizeof(int),cmp); for(minlen=len[0];;minlen++){ if(tollen%minlen) continue;memset(b,0,sizeof(b));get=tollen/minlen;if(dfs(0,0,0)){c[q]=minlen;q++;break;}}}for(i=0;i<q;i++)printf("%d\n",c[i]);return 0;}25.Symmetric Sort#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){double A[100];int i=0,j=0,k=0,l=0,sum=0;while(1){scanf("%lf",&A[i]);if(A[i]==0)break;i++;}for(j=0;j<i;j++){if(A[j]==2)printf("1\n");else{int B[10000],m=1,number=0;double n;B[0]=2;for(k=3;k<=A[j];k+=2){n=(double)k;for(l=2;l<=sqrt(n);l++){if(k%l==0)goto ai;}B[m]=k;m++;ai:;}for(k=0;k<m;k++){sum=0;for(l=k;l<m;l++){sum+=B[l];if(sum==A[j]){number++;break;}}}printf("%d\n",number);}}return 0;}26.The Clock#include <stdio.h>#include <stdlib.h>#include <string.h>int main()char s[100][100],a[100];int i,j,n;scanf("%d",&n);for(i=0;i<n;i++) scanf("%s",s[i]);for(i=0;i<n-1;i++)for(j=0;j<n-1-i;j++)if(strlen(s[i])>strlen(s[i+1]))strcpy(a,s[i]),strcpy(s[i],s[i+1]),strcpy(s[i+1],a);if(n%2==0){for(i=0;i<n-1;i=i+2) printf("%s ",s[i]);printf("%s ",s[n-1]);for(i=i-3;i>0;i=i-2) printf("%s ",s[i]);}else{for(i=0;i<n-1;i=i+2) printf("%s ",s[i]);printf("%s ",s[n-1]);for(i=i-1;i>0;i=i-2) printf("%s ",s[i]);}return 0;}27.The Ratio of gainers to losers #include<stdio.h>int main(){char s[5];int i,sum=0;gets(s);for(i=0;s[i]!='\0';i++){switch(s[i]){case'I': sum+=1;break;case'V': sum=5-sum;break; case'X':sum=10-sum;break; }}printf("%d\n",sum);return 0;}28.VOL大学乒乓球比赛#include <stdio.h>#include <stdlib.h>int main(){printf("A=Z\nB=X\nC=Y\n");return 0;}29.毕业设计论文打印#include <stdio.h>#include <stdlib.h>int main(){int a[100],j=1,i,n,m;scanf("%d%d",&n,&m);for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=0;i<n;i++)if(a[i]>a[m]) j++;printf("%d",j++);return 0;}30.边沿与内芯的差#include <stdio.h>#include <stdlib.h>int main(){int A[100][100],i,j,m,n,s=0,t=0;scanf("%d%d",&n,&m);for(i=1;i<=n;i++){for(j=1;j<=m;j++){scanf("%d",&A[i][j]);}}for(i=2;i<m;i++)s=s+A[1][i];for(i=2;i<m;i++)s=s+A[n][i];for(i=1;i<=n;i++)s=s+A[i][1];for(i=1;i<=n;i++)。

西工大C语言POJ习题复习资料

西工大C语言POJ习题复习资料

一.1.第一季10题全(注:第五题问题已经解决,确认AC!)#include <stdio.h>int main(){int a,b,sum;scanf("%d%d",&a,&b);sum=a+b;printf("%d\n",sum);return 0;}2.#include <stdio.h>#define PI 3.1415926int main(){double r,h,l,s,sq,vq,vz;scanf("%lf%lf",&r,&h);l=2*PI*r;s=PI*r*r;sq=4*PI*r*r;vq=4*PI*r*r*r/3;vz=s*h;printf("%.2lf\n%.2lf\n%.2lf\n%.2lf\n%.2lf\n", l,s,sq,vq,vz); return 0;}3.#include <stdio.h>int main(){int a,b,c;double d,e;scanf("%d%d%d",&a,&b,&c); d=a+b+c;e=d/3;printf("%lf\n%lf\n",d,e);return 0;}4.#include <stdio.h>int main(){int a,b,c;scanf("%d%d%d",&a,&b,&c); if(a<b)a=b;if(a<c)a=c;printf("%d\n",a); return 0;}5.#include<stdio.h>int main(){int i=0,j=0,k=1;char a[6];while((a[i]=getchar())!='\n') { i++;}for(;i>0;i--){if(a[j]==a[i-1]){j++;continue;}else {k=0;break;}}if(k==1)printf("yes\n");elseprintf("no\n");}6.#include<stdio.h>int main(){double a,c;scanf("%lf",&a); switch((int)a/10){case0:c=a*0.1;break;case1:c=(a-10)*0.075+10*0.1;break;case2:case3:c=(a-20)*0.05+10*0.075+10*0.1;break;case4:case5:c=(a-40)*0.03+20*0.05+10*0.075+10*0.1;break;case6:case7:case8:case9:c=(a-60)*0.015+20*0.03+20*0.05+10*0.075+10*0. 1;break;default:c=(a-100)*0.01+40*0.015+20*0.03+20*0.05+10*0. 075+10*0.1;}printf("%lf\n",c);return0;}7.#include<stdio.h>int main(){double a,b,c;scanf("%lf",&a);c=(int)a;if(a>c)a=c+1;if(a>15)b=(a-15)*2.1+7+13*1.5;else{if(a>2)b=(a-2)*1.5+7;else b=7;} printf("%lf\n",b); return0;}8.#include <stdio.h>int main(){int a,b,c,e,f=30,g=31,n;scanf("%d-%d-%d",&a,&b,&c);if((a%400==0)||(a%100!=0&&a%4==0))e=29;elsee=28;switch (b){case 1:n=c;break;case 2:n=g+c;break;case 3:n=g+e+c;break; case 4:n=g+e+g+c;break;case 5:n=g+e+g+f+c;break;case 6:n=g+e+g+f+g+c;break;case 7:n=g+e+g+f+g+f+c;break;case 8:n=g+e+g+f+g+f+g+c;break;case 9:n=g+e+g+f+g+f+g+g+c;break;case 10:n=g+e+g+f+g+f+g+g+f+c;break; case 11:n=g+e+g+f+g+f+g+g+f+g+c;break; default: n=g+e+g+f+g+f+g+g+f+g+f+c;}printf("%d\n",n);return 0;}9.#include <stdio.h> int main(){int x;scanf("%d",&x);if(x>=90&&x<=100) printf("A\n");else if (x>=80)printf("B\n");else if (x>=70)printf("C\n");else if (x>=60)printf("D\n");elseprintf("E\n");return 0;}10.#include<stdio.h>int main(){double x,y,s;scanf("%lf,%lf",&x,&y);s=(x+2)*(x+2)+(y-2)*(y-2);if(s>1){s=(x+2)*(x+2)+(y+2)*(y+2);if(s>1){s=(x-2)*(x-2)+(y+2)*(y+2);if(s>1){s=(x-2)*(x-2)+(y-2)*(y-2);if(s>1){printf("0\n");return1;} }}}printf("10\n");return0;}二。

C-POJ习题

C-POJ习题
计算机学院学生会生活部 6
C 语言 POJ 习题答案
◆T0502 #include<stdio.h> int main() { int a,b; float c; scanf("%d%d",&a,&b); c=a+b/100.00; if(c<=1.20) printf("%d 月%d 日是摩羯座\n",a,b); else if(c<=2.20) printf("%d 月%d 日为宝瓶座\n",a,b); else if(c<=3.20) printf("%d 月%d 日为双鱼座\n",a,b); else if(c<=4.20) printf("%d 月%d 日为白羊座\n",a,b); else if(c<=5.20) printf("%d 月%d 日为金牛座\n",a,b); else if(c<=6.20) printf("%d 月%d 日为双子座\n",a,b); else if(c<=7.22) printf("%d 月%d 日为巨蟹座\n",a,b); else if(c<=8.22) printf("%d 月%d 日为狮子座\n",a,b); else if(c<=9.22) printf("%d 月%d 日为处女座\n",a,b); else if(c<=10.22) printf("%d 月%d 日为天秤座\n",a,b); else if(c<=11.22) printf("%d 月%d 日为天蝎座\n",a,b); else if(c<=12.22) printf("%d 月%d 日为人马座\n",a,b); else printf("%d 月%d 日为摩羯座\n",a,b); return 0; } ◆T0505 #include<stdio.h> #include<math.h> int main() { int h,m,a,b,c; scanf("%d:%d",&h,&m); a=30*h+m/2; b=6*m; c=abs(b-a); if (c>180) c=360-c; printf("c=%d\n",c); return 0; } ◆SXD02 #include<stdio.h> int main() { int i; for(i=100;i<=200;i++) {

西工大POJ100题(全新)(DOC)

西工大POJ100题(全新)(DOC)

1·计算A+B2·圆及圆球等的相关计算3·计算成绩4·找最大数5·找幸运数6·奖金发放7·出租车费8·是该年的第几天9·成绩转换10·求建筑高度11·区间内素数12·计算π13·两个整数之间所有的素数14·最次方数15·自然数立方的乐趣16·五猴分桃17·完全数18·二分求根19·你会存钱吗?20·级数和21·幸运数字722·粒子裂变23·特殊整数24·最大乘积25·解不等式26·危险的组合27·子序列的和29·探索合数世纪30·韩信点兵31·亲和数32·高低交换33·循环移位35·组合数36·积分计算37·数据加密38·获取指定二进制位39·ACKERMAN40·不会吧,又是A+B 41·平均值函数42·插入排序43·一维数组赋值44·右下角45·右上角46·山迪的麻烦47·冒泡排序48·恐怖水母49·左上角50·一维数组加法51·字符串排序52·字符串左中右53·文章统计54·找出数字55·字符串比较56·THE CLOCK 57·合并字符串58·特殊要求的字符串59·字符串逆序60·COIN TEST 61·小型数据库62·单词频次65·检测位图长宽67·子字符串替换68·复数69·字符串加密编码70·重组字符串71·大数除法72·创建与遍历职工链表73·大数加法74·大数减法75·链表节点删除76·链表动态增长或缩小77·大数乘法78·精确乘幂80·火车站81·操场训练82·HAILSTONE84·阶乘合计85·王的对抗86·三点顺序87·公园喷水器88·勇闯天涯89·不屈的小蜗90·THE RATIO OF GAINERS TO LOSERS 91·GRAVEYARD92·ARITHMETIC PROGRESSIONS 93·SCORING94·HOUSEBOAT97·BEE98·CHECKSUM ALGORITHM99·Hanoi 2100·Specialized number1·计算A+B#include<stdio.h>int main(){int a,b,sum;scanf("%d %d",&a,&b);sum=a+b;printf("%d",sum);}2·圆及圆球等的相关计算#include<stdio.h>int main(){float r,h,l,s,sq,vq,vz,pi=3.141592653;scanf("%f %f",&r,&h);l=2*pi*r;s=pi*r*r;sq=4*pi*r*r;vq=4.0/3*pi*r*r*r;vz=pi*r*r*h;printf("%.2f\n%.2f\n%.2f\n%.2f\n%.2f\n",l,s,sq,v q,vz);return 0;}3·计算成绩#include <stdio.h>int main(){float a,b,c,sum,ave;scanf("%f %f %f",&a,&b,&c);sum=a+b+c;ave=sum/3.0;printf("%.6f\n%.6f",sum,ave);return 0;}4·找最大数#include<stdio.h>int main(){int a,b,c,t;scanf("%d %d %d",&a,&b,&c);if (a<b) t=a,a=b;if(a<c) t=a,a=c;printf("%d",a);return 0;}5·找幸运数#include <stdio.h>int main(){int i,j,a;scanf("%d",&a);i=0;j=a;while(j){i=10*i+(j%10);j=j/10;}if(i==a)printf("yes\n");else printf("no\n");return 0;}6·奖金发放#include<stdio.h>int main (){float l;scanf("%f",&l);if (l<=10)l=l*0.1;else if(l<=20)l=10*0.1+(l-10)*0.075;else if(l<=40)l=10*0.1+10*0.075+(l-20)*0.05;elseif(l<=60)l=10*0.1+10*0.075+20*0.05+(l-40)*0.03;elseif(l<=100)l=10*0.1+10*0.075+20*0.05+20*0.03+(l-60)* 0.015;elsel=10*0.1+10*0.075+20*0.05+20*0.03+40*0.015+(l-100)* 0.01;printf("%.6f",l);return 0;}7·出租车费#include<stdio.h>#include<math.h>int main(){float a,n,t;scanf("%f",&a);if (a<=2) n=7;else if (a<=15) {t=a-floor(a);if(t==0)n=7+(floor(a)-2)*1.5;else n=7+(floor(a)-1)*1.5;}else {t=a-floor(a);if(t==0) n=7+13*1.5+(floor(a)-15)*2.1;else n=7+13*1.5+(floor(a)-14)*2.1;} printf("%.6f",n);return 0;}8·是该年的第几天#include<stdio.h>int main(){int y,m,d,n,a;scanf("%d-%d-%d",&y,&m,&d);if((y%4==0&&y%100!=0)||(y%400==0)) a=29;else a=28;switch(m){case 1:n=d;break;case 2:n=31+d;break;case 3:n=31+a+d;break;case 4:n=62+a+d;break;case 5:n=92+a+d;break;case 6:n=123+a+d;break;case 7:n=153+a+d;break;case 8:n=184+a+d;break;case 9:n=215+a+d;break;case 10:n=245+a+d;break;case 11:n=276+a+d;break;case 12:n=306+a+d;break;}printf("%d",n);return 0;}9·成绩转换#include<stdio.h>int main(){int n;scanf("%d",&n);if(n<60)printf("E\n");else if(n<70)printf("D\n");else if(n<80)printf("C\n");else if(n<90)printf("B\n");else printf("A\n");return 0;}10·求建筑高度#include<stdio.h>int main(){float x,y,a,b,c,d;scanf("%f,%f",&x,&y);a=(x-2)*(x-2)+(y-2)*(y-2);b=(x-2)*(x-2)+(y+2)*(y+2);c=(x+2)*(x+2)+(y-2)*(y-2);d=(x+2)*(x+2)+(y+2)*(y+2);if(a<=1||b<=1||c<=1||d<=1)printf("10");else printf("0");return 0;}11·区间内素数#include<stdio.h>#include<math.h>int main(){int i,n=500,cnt=0,sum=0;while(n<=800){for (i=2;i<=n-1;i++)if (n%i==0)break;if (i==n)cnt=cnt+1,sum=sum+pow((-1),cnt)*n; n=n+1;}printf("%d %d",cnt,sum);return 0;}12·计算π#include<stdio.h>#include<math.h>int main(){int s=1;double pi=0,n=1,t=1;while (fabs(t)>1e-6)pi=pi+t,n=n+2,s=-s,t=s/n;pi=pi*4;printf("%.6f",pi);return 0;}13·两个整数之间所有的素数#include<stdio.h>int main(){int a,b,t,i;scanf("%d%d",&a,&b);if(a>b)t=a,a=b,b=t;while (a<=b){for (i=2;i<=a-1;i++)if (a%i==0)break;if(i==a)printf("%d ",a);a++;}return 0;}14·最次方数#include<stdio.h>int main(){int x,a,m=1,n=1;scanf("%d %d",&x,&a);while (m<=a){n=n*x;n=n%1000;m=m+1;}printf("%d",n);return 0;}15·自然数立方的乐趣#include<stdio.h>int main(){int n,a,i,t;scanf("%d",&n);a=n*(n-1)+1;t=n*n*n;printf("%d*%d*%d=%d=",n,n,n,t);for(i=1;i<n;i++){printf("%d+",a);a=a+2;}printf("%d",a);return 0;}16·五猴分桃#include<stdio.h>int divides(int n,int m){if (n/5==0||n%5!=1)return 0;if (m==1)return 1;return divides(n-n/5-1,m-1);}int main(){int n,a;for (n=1;;n++)if(divides(n,5)){printf("%d ",n);break;}a=(((((n-1)*4/5-1)*4/5-1)*4/5-1)*4/5-1)*4/5;printf("%d",a);return 0;}17·完全数#include<stdio.h>int main(){int i,n,sum=0;for(n=2;n<1000;n++){sum=0;for (i=1;i<=n/2;i++){if(n%i==0)sum=sum+i;}if(sum==n){printf("%d=",n);for(i=1;i<n/2;i++)if(n%i==0)printf("%d+",i);i=n/2;printf("%d\n",i);}}return 0;}18·二分求根#include<math.h>#include<stdio.h>double fun(double x) {return 2*x*x*x-4*x*x+3*x-6;} double root(double a,double b,double e){double x1,x2,y1,x,y;x1=a;x2=b;do {x=(x1 + x2)/2;y=fun(x);y1=fun(x1);if((y<0&&y1<0)||(y>0&&y1>0))x1=x;elsex2=x;/*end if*/}while(fabs(y) > e);return x;}int main(void){double m,n;scanf("%f %f",&m,&n);double x=root(m,n,1e-8);printf("%.2f\n",x);return 0;}19·你会存钱吗?#include<stdio.h>#include<math.h>int main(){int i8,i5,i3,i2,i1,n8,n5,n3,n2,n1;float max=0,term;for(i8=0;i8<3;i8++) /*穷举所有可能的存款方式*/for(i5=0;i5<=(20-8*i8)/5;i5++)for(i3=0;i3<=(20-8*i8-5*i5)/3;i3++)for(i2=0;i2<=(20-8*i8-5*i5-3*i3)/2;i2++){i1=20-8*i8-5*i5-3*i3-2*i2;term=2000.0*pow((double)(1+0.0063*12),(double)i1) *pow((double)(1+2*0.0063*12),(double)i2)*pow((double)(1+3*0.0069*12),(double)i3)*pow((double)(1+5*0.0075*12),(double)i5)*pow((double)(1+8*0.0084*12),(double)i8);/*计算到期时的本利合计*/if(term>max){max=term;n1=i1;n2=i2;n3=i3;n5=i5;n8=i8;}}printf("%d ",n8);printf("%d ",n5);printf("%d ",n3);printf("%d ",n2);printf("%d\n",n1);printf("%.2f",max);/*输出存款方式*/}20·级数和#include<stdio.h>#include<math.h>int main(){ int j,t,i,a,n;double s=0;scanf("%d",&n);j=-1; i=1; t=1;for (a=0;a<n;a++){t=t*2; j*=-1; i*=-1; s=s+j*t/((t+i)*(2*t+j)+0.0);}printf("%.6lf\n",s);return 0;}21·幸运数字7#include<stdio.h>int main(){int n,i;scanf("%d",&n);//n不大于30000if(n<7);else {for (i=7;i<=n;i++)if(i%7==0||i%10==7||i/1000%10==7||i/100%10==7||i/10 %10==7)printf("%d ",i);}return 0;}22·粒子裂变#include<stdio.h>int main(){int n[100],m[100],t,i;n[0]=1;m[0]=0;scanf("%d",&t);for(i=1;i<=t;i++){n[i]=m[i-1];m[i]=3*n[i-1]+2*m[i-1]; }printf("%d %d",n[t],m[t]); return 0;}23·特殊整数#include<stdio.h>#include <stdlib.h>#include<math.h>int main(){int m,n,g=0,s=0,i,j,x,x1,x2;scanf("%d %d",&m,&n);x1=(int)(pow(10.0,n)/10);x2=(int)(pow(10.0,n)-1);for(i=x1;i<=x2;i++){j=i;do{x=j%10;if(x==m){if(i%m!=0)g++,s=s+i;break;}else j=j/10;}while(j!=0);}printf("%d %d\n",g,s);return 0;}24·最大乘积#include<stdio.h>int main(){int A[18],i,j,n,x,y=0;scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&A[i]);for(i=0;i<n;i++){x=1;for(j=i;j<n;j++){x=x*A[j];if(x>y) y=x;}}if(y>0)printf("%d",y);else printf("-1");return 0;}25·解不等式#include<stdio.h>int main(){int a,b;float n,s=0,sum=0;scanf("%d %d",&a,&b);for (n=1;sum<=a;n++){s=s+1/n;sum=sum+1/s;}printf("%.0f ",n=n-1); for (n;sum<=b;n++){s=s+1/n;sum=sum+1/s;}printf("%.0f",n=n-1); return 0;}26·危险的组合#include<stdio.h>#include<math.h>int main(){int n;int f(int);scanf("%d",&n);printf("%d",f(n));return 0;}int f(int n){int a;if(n==1||n==2)a=0;else if(n==3)a=1;else if(n==4)a=3;else a=f(n-1)*2+pow(2,n-4)-f(n-4);return a;}27·子序列的和#include<stdio.h>#include<math.h>int main(){int m,n,i; //n<m<1000000double a,sum=0;scanf("%d %d",&n,&m);for (i=n;i<=m;i++){a=pow(i,2.0);sum=sum+1/a;}printf("%.5lf",sum);return 0;}29·探索合数世纪#include<stdio.h>int main(){int n,i=500,s,x,count=0;long j,k;scanf("%d",&n);while(count<n){s=0;for(j=i*100-99;j<i*100;j+=2){x=0;for(k=3;k*k<=j;k+=2)if(j%k==0){x=1;break;}s+=x;}if(s==50)count++;i++;}printf("%d %d",j-101,j-2);return 0;}30·韩信点兵include<stdio.h>int main(){int a,b,c,n;//a<3,b<5,c<7scanf("%d %d %d",&a,&b,&c);for (n=10;n<=100;n++){if((n-a)%3==0&&(n-b)%5==0&&(n-c)%7==0)break;}if(n>100)printf("-1");else printf("%d ",n);return 0;}31·亲和数#include<stdio.h>int fun(int a){int b=0,i;for(i=1;i<=a/2;i++){if(a%i==0)b+=i;}return b;}int main(){int a,b;scanf("%d %d",&a,&b);if(fun(a)==b&&fun(b)==a)printf("YES");else printf("NO");return 0;}32·高低交换#include<stdio.h>inline int xchg(unsigned char n) {char left=n<<4;char right=n>>4;return left+right;}int main(){int n;scanf("%d",&n);n=xchg(n);printf("%d",n);return 0;33·循环移位#include<stdio.h>int move(int value,int n){if(n==0) return value;else if(n<0){n=-n;value=(value<<n)|(value>>(32-n));}else value=(value>>n)|(value<<(32-n));return value;}int main(){int value,n;scanf("%d %d",&value,&n);value=move(value,n);printf("%d",value);return 0;35·组合数#include<stdio.h>int C(int m,int n){int i,p=1;for (i=1;i<=n;i++)p*=(m+1-i)/(double)i;return p;}int main(){int m,n,t;scanf("%d %d",&m,&n);if(n>m) printf("wrong\n");else {t=C(m,n);printf("%d\n",t);}return 0;}36·积分计算#include<stdio.h>double f(double x){return 1.0/(1.0+x*x);}double jf(double a,double b) {double h,s=0;;int i;h=(b-a)/5000000;for(i=1;i<5000000;i++){s+=(f(a)+f(a+h))*0.5*h;a+=h;}return s;}int main(){double a,b,s;scanf("%lf%lf",&a,&b); s=jf(a,b);printf("%lf",s);return 0;}37·数据加密#include<stdio.h>int fun(int n){return (n+5)%10;}int main(){int a,b[4],i=0;scanf("%d",&a);while(a){b[i]=fun(a%10);a/=10;i++;}a=b[0]*1000+b[1]*100+b[2]*10+b[3]; printf("%d",a);return 0;}38·获取指定二进制位#include <stdio.h>int getbit(int n,int k){int count=0;while(1){count++;if(count==k)break;n/=2;}return n%2;}int main(){int n,k;scanf("%d %d",&n,&k);k=getbit(n,k);printf("%d",k);return 0;}39·ACKERMAN#include<stdio.h>int ack(int m,int n){if(m==0) return n+1;else if(n==0) return ack(m-1,1); else return ack(m-1,ack(m,n-1)); }int main(){int m,n;scanf("%d %d",&m,&n);m=ack(m,n);printf("%d",m);return 0;}40·不会吧,又是A+B#include<stdio.h>int main(){int a1,a2,a3,b1,b2,b3,a,b,c;scanf("%d %d %d %d %d %d",&a1,&a2,&a3,&b1,&b2,&b3); c=(a3+b3)%60;b=(a2+b2+(a3+b3)/60)%60;a=a1+b1+((a3+b3)/60+a2+b2)/60;printf("%d %d %d ",a,b,c);return 0;}41·平均值函数#include<stdio.h>double avg(int A[],int s,int e){int i;double t,sum;for(i=s;i<=e;i++)sum+=A[i];t=sum/(e-s+1);return t;}int main(){int n,s,e,i,A[100];double t;scanf("%d\n",&n);for(i=0;i<n;i++)scanf("%d ",&A[i]);scanf("%d %d",&s,&e);t=avg(A,s,e);printf("%f",t);return 0;}42·插入排序#include<stdio.h>void InsertionSort(int a[],int s,int m) {int t,i,j,k;for(i=0;i<m;i++){for(j=s+1;j<s+m-i;j++){t=a[j];k=j-1;while(t>a[k]){a[k+1]=a[k];k--;if(k==(s-1))break;}a[k+1]=t;}}}int main(){int n,a[100],i,s,m;scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&a[i]);scanf("%d %d",&s,&m);InsertionSort(a,s,m);for(i=0;i<n;i++)printf("%d ",a[i]);printf("\n");return 0;}43·一维数组赋值#include<stdio.h>int main(){int A[100],B[100],i,n;scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&A[i]);for(i=0;i<n;i++){B[i]=A[i];if(i%2!=0)printf("%d ",B[i]);}return 0;}44·右下角#include<stdio.h>int main(){int n,a[10][10],i,j;scanf("%d",&n);for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d",&a[i][j]);for(i=0;i<n;i++){for(j=0;j<n;j++){if(j<=n-i-2)printf(" "); else printf("%d ",a[i][j]);} printf("\n");}return 0;}45·右上角#include<stdio.h>int main(){int n,a[10][10],i,j;scanf("%d",&n);for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d",&a[i][j]);for(i=0;i<n;i++){for(j=0;j<n;j++){if(j>=i)printf("%d ",a[i][j]); else printf(" ");}printf("\n");}return 0;}46·山迪的麻烦#include<stdio.h>int main(){int n,count=0,i,j,a[100],k;scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=1;i<n;i++){k=a[i];for(j=i-1;j>=0&&k<a[j];j--){count++;a[j+1]=a[j];}a[j+1]=k;}printf("%d\n",count);return 0;}47·冒泡排序#include<stdio.h>void BubbleSort(int a[],int s,int m) {int t,i,j;for(i=0;i<m-1;i++){for(j=s;j<s+m-i-1;j++)if(a[j]<a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}}int main(){int n,a[100],i,s,m;scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&a[i]);scanf("%d %d",&s,&m);BubbleSort(a,s,m);for(i=0;i<n;i++)printf("%d ",a[i]);printf("\n");return 0;}48·恐怖水母#include<stdio.h>int main(){int n,m,i,j,a[100],b[100],t,sum=0; scanf("%d%d",&n,&m);for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=0;i<m;i++)scanf("%d",&b[i]);for(i=0;i<n-1;i++){for(j=0;j<n-1-i;j++)if(a[j]>a[j+1]){t=a[j];a[j]=a[j+1];a[j+1]=t;}}for(i=0;i<m-1;i++){for(j=0;j<m-1-i;j++)if(b[j]>b[j+1]){t=b[j];b[j]=b[j+1];b[j+1]=t;}}for(i=0,j=0;i<n&&j<m;j++){if(a[i]<=a[j]){sum+=b[j];i++;}}printf("%d\n",sum);return 0;}49·左上角#include<stdio.h>int main(){int n,a[10][10],i,j;scanf("%d",&n);for(i=0;i<n;i++)for(j=0;j<n;j++)scanf("%d",&a[i][j]); for(i=0;i<n;i++){for(j=0;j<n-i;j++)printf("%d ",a[i][j]);printf("\n");}return 0;}50·一维数组加法#include<stdio.h>int main(){int A[100],B[100],C[100],n,i;scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&A[i]);for(i=0;i<n;i++)scanf("%d",&B[i]);for(i=0;i<n;i++){C[i]=A[i]+B[i];printf("%d ",C[i]);}return 0;}51·字符串排序#include<stdio.h>#include<string.h>int main(){char str[10][10];int i,j;char t[10];for(i=0;i<10;i++){scanf("%s",str[i]);}for(i=0;i<10;i++){for(j=0;j<10-i;j++){if(strcmp(str[j],str[j+1])>0) {strcpy(t,str[j]);strcpy(str[j],str[j+1]);strcpy(str[j+1],t);}}}for(i=0;i<10;i++)printf("%s ",str[i]);return 0;}52·字符串左中右#include<stdio.h>#include<string.h>void Left(char str[],int n,char dest[]) {int i;for(i=0;i<n;i++)dest[i]=str[i];}void Right(char str[],int n,char dest[]) {int i,m,j=0;m=strlen(str);for(i=m-n;i<=m;i++){。

西工大noj答案完整版

西工大noj答案完整版

西北工业大学POJ答案绝对是史上最全版(不止100题哦……按首字母排序)1.“1“的传奇2.A+B3.A+BⅡ4.AB5.ACKERMAN6.Arithmetic Progressions7.Bee8.Checksum algorithm9.Coin Test10.Dexter need help11.Double12.Easy problem13.Favorite number14.Graveyard15.Hailstone16.Hanoi Ⅱ17.Houseboat18.Music Composer19.Redistribute wealth20.Road trip21.Scoring22.Specialized Numbers23.Sticks24.Sum of Consecutive25.Symmetric Sort26.The Clock27.The Ratio of gainers to losers28.VOL大学乒乓球比赛29.毕业设计论文打印30.边沿与内芯的差31.不会吧,又是A+B32.不屈的小蜗33.操场训练34.插入链表节点35.插入排序36.插入字符37.成绩表计算38.成绩转换39.出租车费40.除法41.创建与遍历职工链表42.大数乘法43.大数除法44.大数加法45.单词频次46.迭代求根47.多项式的猜想48.二分查找49.二分求根50.发工资的日子51.方差52.分离单词53.分数拆分54.分数化小数55.分数加减法56.复数57.高低交换58.公园喷水器59.韩信点兵60.行程编码压缩算法61.合并字符串62.猴子分桃63.火车站64.获取指定二进制位65.积分计算66.级数和67.计算A+B68.计算PI69.计算π70.计算成绩71.计算完全数72.检测位图长宽73.检查图像文件格式74.奖金发放75.阶乘合计76.解不等式77.精确幂乘78.恐怖水母79.快速排序80.粒子裂变81.链表动态增长或缩短82.链表节点删除83.两个整数之间所有的素数84.路痴85.冒泡排序86.你会存钱吗87.逆序整数88.排列89.排列分析90.平均值函数91.奇特的分数数列92.求建筑高度93.区间内素数94.三点顺序95.山迪的麻烦96.删除字符97.是该年的第几天98.是该年的第几天?99.数据加密100.搜索字符101.所有素数102.探索合数世纪103.特殊要求的字符串104.特殊整数105.完全数106.王的对抗107.危险的组合108.文件比较109.文章统计110.五猴分桃111.小型数据库112.幸运儿113.幸运数字”7“114.选择排序115.寻找规律116.循环移位117.延伸的卡片118.羊羊聚会119.一维数组”赋值“120.一维数组”加法“121.勇闯天涯122.右上角123.右下角124.圆及圆球等的相关计算125.圆及圆球等相关计算126.程序员添加行号127.找出数字128.找幸运数129.找最大数130.整数位数131.重组字符串132.子序列的和133.子字符串替换134.自然数立方的乐趣135.字符串比较136.字符串复制137.字符串加密编码138.字符串逆序139.字符串排序140.字符串替换141.字符串左中右142.组合数143.最次方数144.最大乘积145.最大整数146.最小整数147.最长回文子串148.左上角149.左下角1.“1“的传奇#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){int n,i,j,k=0,x=1,y,z,m,p,q,a,s=0;scanf("%d",&n);m=n;for(i=1;i<12;i++){m=m/10;k++;if(m==0)break;}q=n;k=k-1;for(a=1;a<=k;a++){x=x*10;}y=q%x;z=q/x;p=q-y;if(z>=2)s=s+x+z*k*(x/10); elses=s+z*k*(x/10);for(j=p;j<=n;j++) {m=j;for(i=1;i<12;i++){x=m%10;if(x==1)s++;m=m/10;if(m==0)break;}}printf("%d",s);return 0;}2.A+B#include <stdio.h>int doubi(int n,int m){n=n+m;n=n%100;return n;}int main(){int t,i,a[100],n,m;scanf("%d",&t);for (i=0;i<=(t-1);i++){scanf("%d%d",&n,&m);a[i]=doubi(n,m);}for (i=0;i<=(t-1);i++)printf("%d\n",a[i]);return 0;}3.A+BⅡ#include <stdio.h>int main(){int A,B,sum;scanf("%d%d",&A,&B);sum=A+B;printf("%d\n",sum);return 0;}4.AB#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){char s[100],q[100];double a,b,c;int n=0,i;scanf("%lf%lf",&a,&b);c=a*b;sprintf(s,"%.0lf",c);for(i=0;i<strlen(s);i++){n=n+s[i]-48;}while(n>=10){sprintf(q,"%d",n);n=0;for(i=0;i<strlen(q);i++)n=n+q[i]-48;}printf("%d",n);return 0;}5.ACKERMAN#include <stdio.h>#include <stdlib.h>int ack(int x,int y){int n;if (x==0) {n=y+1;return n;}else if (y==0) n=ack(x-1,1);else n=ack(x-1,ack(x,y-1));return n;}int main(){int m,b;scanf("%d%d",&m,&b);m=ack(m,b);printf("%d",m);return 0;}6.Arithmetic Progressions#include <stdio.h>#include <math.h>int g(int n){int i;if(n==1) return 0;if(n==2) return 1;if(n==3) return 1;for(i=2;i<=sqrt(n);i++) if(n%i==0) return 0;return 1;}int f(int a,int b,int c){int i=0,s=a-b;if(c==1&&g(a)==1) return a;if(b==0&&g(a)!=1) return -1;while(1){s=s+b;if(g(s)) i++;if(i>=c) break;}return s;int main(){int a,b,c,d[100],i=0,n;while(1){scanf("%d%d%d",&a,&b,&c);if(a==0&&b==0&&c==0) break;d[i]=f(a,b,c);i++;}n=i;for(i=0;i<n;i++)printf("%d\n",d[i]);return 0;}7.Bee#include <stdio.h>#include <stdlib.h>int main()int A[100],i=0,j,k,female=0,male=1,x;for(;;i++){scanf("%d",&A[i]);if(A[i]==-1)break;}for(j=0;j<i;j++){female=0,male=1;for(k=1;k<A[j];k++){x=female;female=male;male=x+male+1;}printf("%d %d\n",male,female+male+1);}return 0;}8.Checksum algorithm #include <stdio.h>#include <stdlib.h>#include <string.h>int main(){int i,n,t,j;char s[100][100];for(i=0;;i++){gets(s[i]);if(s[i][0]=='#') break;}n=i;for(i=0;i<n;i++){t=0;for(j=0;j<strlen(s[i]);j++)if(s[i][j]==32) t=t;else t=t+(j+1)*(s[i][j]-64);printf("%d\n",t);}return 0;}9.Coin Test#include <stdio.h>#include <stdlib.h>int main(){char A[100000];int n,i=0,a=0,b=0,j;double x;while(1){scanf("%c",&A[i]);if(A[i]=='\n')break;i++;}for(j=0;j<i;j++){if(A[j]=='S'){printf("WA");goto OH;}if(A[j]=='U')a++;if(A[j]=='D')b++;}x=a*1.0/(a+b)*1.0;if(x-0.5>0.003||x-0.5<-0.003) printf("Fail");elseprintf("%d/%d",a,a+b);OH:return 0;}10.Dexter need help#include <stdio.h>int fun(int a){if(a==1) return 1;elsereturn fun(a/2)+1;}int main(){int a,b[100],i=0,j; while(1){scanf("%d",&a);if(a==0)break;b[i]=fun(a);i++;}for(j=0;j<i;j++){printf("%d\n",b[j]); }return 0;}11.Double#include <stdio.h>#include <stdlib.h>int main(){int a[100],b[100],i,j,n,t=0;for(i=0;;i++){scanf("%d",&a[i]);if(a[i]==0) break;}n=i;for(i=0;i<n;i++)b[i]=2*a[i];for(i=0;i<n;i++)for(j=0;j<n;j++)if(a[i]==b[j]) t++;printf("%d",t);return 0;}12.Easy problem#include <stdio.h>#include <math.h>int main(){int N,i,n,j=0;scanf("%d",&N);for(i=2;i<N+1;i++){if((N+1)%i==0)j++;}printf("%d",j/2);return 0;}13.Favorite number #include <stdio.h>#include <string.h>#define MAXNUM 100000int prime_number = 0;int prime_list[MAXNUM]; bool is_prime[MAXNUM]; int ans[MAXNUM + 2];int dp[MAXNUM + 2];void set_prime() {int i, j;memset(is_prime, 0, sizeof(is_prime));for (i = 2; i < MAXNUM; i++) {if (is_prime[i] == 0) {prime_list[prime_number++] = i;if (i >= MAXNUM / i) continue;for (j = i * i; j < MAXNUM; j+=i) {is_prime[j] = 1;}}}}int main() {int i, j, k,o=0,d[100];memset(dp, -1, sizeof(dp));set_prime();ans[0] = 0;dp[1] = 0;for (i = 1; i <= MAXNUM; i++) {ans[i] = ans[i - 1] + dp[i];if (dp[i + 1] == -1 || dp[i + 1] > dp[i] + 1) { dp[i + 1] = dp[i] + 1;}for (j = 0; j < prime_number; j++) {if (i > MAXNUM / prime_list[j]) break;k = i * prime_list[j];if (dp[k] == -1 || dp[k] > dp[i] + 1) {dp[k] = dp[i] + 1;}}}while (scanf("%d%d", &i, &j) == 2 && (i || j)) { d[o]=ans[j] - ans[i - 1];o++;}for(i=0;i<o;i++)printf("%d\n",d[i]);}14.Graveyard#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){int a[100],b[100],n,i,j;double s,p,l,t;for(i=0;;i++){scanf("%d%d",&a[i],&b[i]);if(a[i]==0&&b[i]==0) break;}n=i;for(i=0;i<n;i++){p=10000;if(b[i]%a[i]==0){printf("0.0000\n");continue;};t=10000/((double)a[i]);for(j=1;j<a[i]+b[i];j++){l=10000/((double)(a[i]+b[i]));l=t-j*l;l=fabs(l);if(l<p) p=l;}s=(a[i]-1)*p;printf("%.4lf\n",s);}return 0;}15.Hailstone#include <stdio.h>#include <stdlib.h>#include <string.h>int f(int n){int s=1;while(1){if(n==1) return s;else if(n%2==0) n=n/2,s++;else n=3*n+1,s++;}}int main()int n,m,i,j=0,t;scanf("%d%d",&m,&n);printf("%d %d",m,n);if(m>n) t=m,m=n,n=t;for(i=m;i<=n;i++)if(f(i)>j) j=f(i);printf(" %d",j);return 0;}16.Hanoi Ⅱ#include <stdio.h>#include <stdlib.h>#define M 70int start[M], targe[M];long long f(int *p, int k, int fina){if(k==0) return 0;if(p[k]==fina) return f(p,k-1,fina); return f(p,k-1,6-fina-p[k])+(1LL<<(k-1));int main (){long long ans;int n;while(scanf("%d",&n),n){int i;for(i=1;i<=n;i++) scanf("%d",&start[i]);for(i=1;i<=n;i++) scanf("%d",&targe[i]);int c=n;for(;c>=1&&start[c]==targe[c];c--);if(c==0){printf("0\n"); continue;}int other=6-start[c]-targe[c];ans=f(start,c-1,other)+f(targe,c-1,other)+1;printf("%lld\n",ans);}return 0;}17.Houseboat#include <stdio.h>#include <stdlib.h>#include <math.h>#define pi 3.1415926int f(float x,float y){int i;for(i=0;;i++)if(50*i>sqrt(x*x+y*y)*sqrt(x*x+y*y)*pi/2) break;return i;}int main(){int n,i,a[100];float x,y;scanf("%d",&n);for(i=0;i<n;i++){scanf("%f%f",&x,&y);a[i]=f(x,y);}for(i=0;i<n;i++)printf("%d %d\n",i+1,a[i]);return 0;}18.Music Composer19.Redistribute wealth#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){int a[1000],b[1000],n,i,j,s,sum,t,m,mid,c[100],k=0;while(1){scanf("%d",&n);if(n==0) break;{s=0;for(i=1;i<=n;i++){scanf("%d",&a[i]);s=s+a[i];}m=s/n;b[1]=a[1]-m;b[0]=0;for(i=2;i<n;++i)b[i]=b[i-1]+a[i]-m;for(i=0;i<n;i++)for(j=0;j<n-1-i;j++)if(b[j]>b[j+1])t=b[j],b[j]=b[j+1],b[j+1]=t;mid=b[n/2];sum=0;for(i=0;i<=n-1;++i) sum=sum+fabs(mid-b[i]);c[k]=sum;k++;}}for(i=0;i<k;i++) printf("%d\n",c[i]);return 0;}20.Road trip#include <stdio.h>#include <stdlib.h>#include <math.h>int f(int n){int a[100],b[100],i,s;for(i=0;i<n;i++)scanf("%d%d",&a[i],&b[i]);s=a[0]*b[0];for(i=1;i<n;i++)s=s+a[i]*(b[i]-b[i-1]);return s;}int main(){int n,c[100],i=0;while(1){scanf("%d",&n);if(n==-1) break;c[i]=f(n);i++;}n=i;for(i=0;i<n;i++)printf("%d\n",c[i]);return 0;}21.Scoring#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){int i,j,sum,min,c,count,n,a,b;char s1[50],s2[50];scanf("%d",&n);for(i=0;i<n;i++){count=sum=0;scanf("%s",s2);for(j=0;j<4;j++){scanf("%d%d",&a,&b);if(b!=0){sum+=(a-1)*20+b;count++;}}if(i==0){c=count,min=sum;strcpy(s1,s2);}else if(count>c||(count==c&&sum<min)){min=sum;c=count;strcpy(s1,s2);}}printf("%s %d %d\n",s1,c,min);return 0;}22.Specialized Numbers#include <stdio.h>#include <stdlib.h>int main(){int i,n,sum10,sum12,sum16;for(i=2992;i<3000;i++){n=i;sum10=0;while(n){sum10+=n%10;n/=10;}n=i;sum12=0;while(n){sum12+=n%12;n/=12;}n=i;sum16=0;while(n){sum16+=n%16;n/=16;}if(sum10==sum12&&sum12==sum16) printf("%d\n",i);}return 0;}23.Sticks#include <stdio.h>#include <string.h>#include <stdlib.h>int len[64], n, minlen, get;bool b[64];int cmp(const void *a, const void *b){return *(int *)a < *(int *)b ? 1 : -1;}bool dfs(int nowlen, int nowget, int cnt){if(cnt >= n) return false;if(get == nowget) return true;int i;bool f = false;if(nowlen == 0) f = true;for(i = cnt; i < n; i++){if(!b[i]){if(len[i] + nowlen == minlen){b[i] = true;if(dfs(0, nowget+1, nowget))return true;b[i] = false;return false;}else if(len[i] + nowlen < minlen){b[i] = true;if(dfs(nowlen+len[i], nowget, i+1))return true;b[i] = false;if(f) return false;while(i + 1 < n && len[i] == len[i+1]) i++;}}}return false;}int main(){int i, tollen;while(scanf("%d", &n), n){tollen = 0;int j = 0, p;for(i = 0; i < n; i++){scanf("%d", &p);if(p <= 50){len[j] = p;tollen += len[j];j++;}}n = j;if(n == 0){printf("0\n");continue;}qsort(len, n, sizeof(int), cmp);for(minlen = len[0]; ; minlen++) {if(tollen % minlen) continue;memset(b, 0, sizeof(b));get = tollen / minlen;if(dfs(0, 0, 0)){printf("%d\n", minlen);break;}}}return 0;}24.Sum of Consecutive#include <stdio.h>#include <stdlib.h>#include <string.h>int len[64],n,minlen,get;int b[64];int cmp(const void *a,const void *b) {return *(int *)a<*(int *)b?1:-1;}int dfs(int nowlen,int nowget,int cnt) {if(cnt>=n) return 0;if(get==nowget) return 1;int i,f=0;if(nowlen==0) f=1;for(i=cnt;i<n;i++){if(len[i]+nowlen==minlen){b[i]=1;if(dfs(0,nowget+1,nowget)) return 1;b[i]=0;return 0;}else if(len[i]+nowlen<minlen){b[i]=1;if(dfs(nowlen+len[i],nowget,i+1)) return 1;b[i]=0;if(f) return 0;while(i+1<n&&len[i]==len[i+1]) i++;}}return 0;}int main(){int i,tollen,q=0,c[100];while(scanf("%d",&n),n){tollen=0;int j=0,p;for(i=0;i<n;i++){scanf("%d",&p);if(p<=50){len[j]=p;tollen+=len[j];j++;}}n=j;if(n==0){printf("0\n");continue;}qsort(len,n,sizeof(int),cmp);for(minlen=len[0];;minlen++){if(tollen%minlen) continue;memset(b,0,sizeof(b));get=tollen/minlen;if(dfs(0,0,0)){c[q]=minlen;q++;break;}}}for(i=0;i<q;i++)printf("%d\n",c[i]);return 0;}25.Symmetric Sort#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){double A[100];int i=0,j=0,k=0,l=0,sum=0;while(1){scanf("%lf",&A[i]);if(A[i]==0)break;i++;}for(j=0;j<i;j++){if(A[j]==2)printf("1\n");else{int B[10000],m=1,number=0;double n;B[0]=2;for(k=3;k<=A[j];k+=2){n=(double)k;for(l=2;l<=sqrt(n);l++){if(k%l==0)goto ai;}B[m]=k;m++;ai:;}for(k=0;k<m;k++){sum=0;for(l=k;l<m;l++){sum+=B[l];if(sum==A[j]){number++;break;}}}printf("%d\n",number);}}return 0;}26.The Clock#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){char s[100][100],a[100];int i,j,n;scanf("%d",&n);for(i=0;i<n;i++) scanf("%s",s[i]);for(i=0;i<n-1;i++)for(j=0;j<n-1-i;j++)if(strlen(s[i])>strlen(s[i+1])) strcpy(a,s[i]),strcpy(s[i],s[i+1]),strcpy(s[i+1],a);if(n%2==0){for(i=0;i<n-1;i=i+2) printf("%s ",s[i]);printf("%s ",s[n-1]);for(i=i-3;i>0;i=i-2) printf("%s ",s[i]);}else{for(i=0;i<n-1;i=i+2) printf("%s ",s[i]);printf("%s ",s[n-1]);for(i=i-1;i>0;i=i-2) printf("%s ",s[i]);}return 0;}27.The Ratio of gainers to losers #include<stdio.h>int main(){char s[5];int i,sum=0;gets(s);for(i=0;s[i]!='\0';i++){switch(s[i]){case'I': sum+=1;break; case'V': sum=5-sum;break; case'X':sum=10-sum;break; }}printf("%d\n",sum);return 0;}28.VOL大学乒乓球比赛#include <stdio.h>#include <stdlib.h>int main(){printf("A=Z\nB=X\nC=Y\n");return 0;}29.毕业设计论文打印#include <stdio.h>#include <stdlib.h>int main(){int a[100],j=1,i,n,m;scanf("%d%d",&n,&m);for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=0;i<n;i++)if(a[i]>a[m]) j++;printf("%d",j++);return 0;}30.边沿与内芯的差#include <stdio.h>#include <stdlib.h>int main(){int A[100][100],i,j,m,n,s=0,t=0;scanf("%d%d",&n,&m);for(i=1;i<=n;i++){for(j=1;j<=m;j++){scanf("%d",&A[i][j]);}}for(i=2;i<m;i++)s=s+A[1][i];for(i=2;i<m;i++)。

C语言程序设计(西安理工大学)知到章节答案智慧树2023年

C语言程序设计(西安理工大学)知到章节答案智慧树2023年

C语言程序设计(西安理工大学)知到章节测试答案智慧树2023年最新第一章测试1.一个C程序可能出现的错误有。

参考答案:其余选项都包括2.C程序中一般可以包含几个函数。

参考答案:至少一个3.C语言属于程序设计语言的哪个类别?参考答案:高级语言4.没有语法错误了,程序就是正确的。

参考答案:错5.一个C程序中可以出现两个main函数。

参考答案:错第二章测试1.类型修饰符unsigned不能修饰()参考答案:float2.16位的二进制数可以表示的整数的范围是()参考答案:[-32768,32767]3.C语言中的实型数据包括()参考答案:float和double4.以下不合法的字符常量是()参考答案:'ab'5.在以下各组标识符中,均是合法的C语言标识符是()参考答案:abc,A_4d,_student,xyz_abc6.在C语言中,要求参加运算的数必须是整数的运算符是()参考答案:%7.若有定义:char a;int b;float c;double d;则表达式a*b+d-c值的类型为()参考答案:double8.设有定义:int a=7;float x=2.5,y=4.7;则表达式x+a%3*(int)(x+y)%2/4的值是()参考答案:2.59.若有定义:double x=1,y;执行语句y=x+3/2;,则y的值是()参考答案:210.设int a=1,b=2;以下选项中不正确的语句是()参考答案:a*3=3第三章测试1.算法描述可以有多种表达方法,下面()方法不可以描述算法。

参考答案:机器语言2.执行以下程序后k的值是()int i,j=10,k=0;for(i=0;i<=j;i++){k=i+j;j--;}参考答案:103.设int a=1,b=2,c=3,d=4,m=2,n=2;执行(m=a>b)&&(n=c>d)后n的值为()参考答案:24.表示y在(-∞,3]和[23,+∞)范围内为真的表达式为()参考答案:(y<=3)||(y>=23)5.已知int x=10,y=20,z=30;则执行以下语句后x、y、z的值是()if(x>y)z=x;x=y;y=z;参考答案:x=20,y=30,z=306.设有声明语句:int a=1,b=0;则执行以下语句后输出结果为()switch(a){ case 1:switch(b){ case 0:printf("**0**");break;case 1:printf("**1**");break;}case 2:printf("**2**");break;}参考答案:**0****2**7.设有以下程序,若要使程序的输出值为2,则应该从键盘给n输入的值是()void main(){ int s=0,a=1,n;scanf("%d",&n);do{s=s+1;a=a-2;}while(a!=n);printf("%d ",s);}参考答案:-38.for(i=j=0;i<10&&j<8;i++,j+=3)控制的循环体执行的次数是()参考答案:39.以下程序段的输出结果是()int x=5;if(x--<5) printf("%d",x);else printf("%d",x++);参考答案:410.对下面程序段,描述正确的是()for(t=1;t<=100;t++){ scanf("%d",&x);if(x<0) continue;printf("%d ",x);}参考答案:最多允许输出100个非负整数第四章测试1.包含标准库函数scanf声明的头文件是()参考答案:stdio.h2.以下关于函数的叙述中正确的是()参考答案:C语言规定必须用main作为主函数名,程序将从此开始执行,在此结束3.以下关于函数的叙述中不正确的是()参考答案:在C语言程序中,被调用的函数必须在main函数中定义4.以下不正确的说法是()参考答案:在主函数内定义的变量可在其余函数中通用5.以下正确的函数定义是()参考答案:double fun(int x,int y) { double z; z=x+y; return z; }6.C语言规定,简单变量做实参时,它和对应形参之间的数据传递方式是()参考答案:单向值传递7.下列程序的运行结果是()#include <stdio.h>void fun();int main(){int a=10;fun();printf("%5d",a);return 0;}void fun(){int a=20;printf("%5d",a);}参考答案:20 108.下列程序的运行结果是()#include <stdio.h>void fun();int a=5;int main(){int a=10;fun();printf("%5d",a);return 0;}void fun(){a=a+5;printf("%5d",a);}参考答案:10 109.以下正确的描述是()参考答案:函数的定义不可以嵌套,但函数的调用可以嵌套10.下列程序的运行结果是()#include <stdio.h>int fun1(int x);void fun2();int x=5;int main(){int x=1;x=fun1(x);printf("%d",x);return 0;}int fun1(int x){x++;fun2();return x;}void fun2(){x++;}参考答案:2第五章测试1.C语言中,由固定数目的同类型的变量按一定顺序排列而构成的是()。

西工大noj答案

西工大noj答案

西工大noj答案【篇一:西工大poj100题(全新)】圆及圆球等的相关计算#includestdio.hint main(){int a,b,sum;scanf(%d %d,a,b);sum=a+b;printf(%d,sum); }#includestdio.hint main(){float r,h,l,s,sq,vq,vz,pi=3.141592653; scanf(%f %f,r,h);l=2*pi*r;s=pi*r*r;【篇二:西北工业大学 c语言 poj题目及答案_第一季】>毋庸置疑,学习程序设计就是奔着“程序员梦”去的。

编程本质是运用计算机科学的基本思想求解问题、设计系统以及理解人类的思维行为和普适技能,核心是“实现”。

因此,诸如“中国梦”、“程序员梦”是编写出来,即“coding now,programming future”。

在这个学期,你将尝试用“编写”的方式去“实现”,体验与过去完全不同的“实现”。

在这个过程中,有太多的“if”不确定、有太多的“for”死循环、有太多的“bug”愁断魂,“实现”并不容易。

有人的地方就有江湖,有江湖的地方就有武林大会。

poj(problems online judge)是学编程的江湖。

在这里,做习题叫做“刷题”,习题做错叫做“被挖”(wa=wrong answer,结果错误),习题通过叫做“a了”(ac=accepted,结果通过),简单习题称为“水题”,“刷一圈”指连续刷题12小时以上。

总会有人用一、两周的时间完成100题的oj,这不叫“刷题”,叫“梦游”。

2012学年,一个大三的哥哥将100题的源码整理出版了(长安校区超市旁的复印店),大一亲们蜂拥而至,一时间“a4纸贵”,交叉着下载、复制、粘贴、上传的能力训练,唯独不见“编写”。

待到期末上机考试,亲们那双瞠目的眼睛与希望工程那双大眼睛神似,最终贡献了两位数的gdp。

有道是出来混的,迟早要还,哥哥今昔完美毕业,亲们继续“梦游”。

西工大noj答案完整版

西工大noj答案完整版

西北工业大学 POJ答案绝对是史上最全版(不止100题哦,,按首字母排序)1.“ 1“的传奇2.A+B3.A+BⅡ4.AB5.ACKERMAN6.Arithmetic Progressions7.Bee8.Checksum algorithm9.Coin Test10.Dexter need help11.Double12.Easy problem13.Favorite number14.Graveyard15.Hailstone16.HanoiⅡ17.Houseboat18.Music Composer19.Redistribute wealth20.Road trip21.Scoring22.Specialized Numbers23.Sticks24.Sum of Consecutive25.Symmetric Sort26.The Clock27.The Ratio of gainers to losers28.VOL 大学乒乓球比赛29.毕业设计论文打印30.边沿与内芯的差31.不会吧,又是 A+B32.不屈的小蜗33.操场训练34.插入链表节点35.插入排序36.插入字符37.成绩表计算38.成绩转换39.出租车费40.除法41.创建与遍历职工链表42.大数乘法43.大数除法44.大数加法45.单词频次46.迭代求根47.多项式的猜想48.二分查找49.二分求根50.发工资的日子51.方差52.分离单词53.分数拆分54.分数化小数55.分数加减法56.复数57.高低交换58.公园喷水器59.韩信点兵60.行程编码压缩算法61.合并字符串62.猴子分桃63.火车站64.获取指定二进制位65.积分计算66.级数和67.计算 A+B68.计算 PI69.计算π70.计算成绩71.计算完全数72.检测位图长宽73.检查图像文件格式74.奖金发放75.阶乘合计76.解不等式77.精确幂乘78.恐怖水母79.快速排序80.粒子裂变81.链表动态增长或缩短82.链表节点删除83.两个整数之间所有的素数84.路痴85.冒泡排序86.你会存钱吗87.逆序整数88.排列89.排列分析90.平均值函数91.奇特的分数数列92.求建筑高度93.区间内素数94.三点顺序95.山迪的麻烦96.删除字符97.是该年的第几天98.是该年的第几天?99.数据加密100.搜索字符101.所有素数102.探索合数世纪103.特殊要求的字符串104.特殊整数105.完全数106.王的对抗107.危险的组合108.文件比较109.文章统计110.五猴分桃111.小型数据库112.幸运儿113.幸运数字” 7“114.选择排序115.寻找规律116.循环移位117.延伸的卡片118.羊羊聚会119.一维数组”赋值“120.一维数组”加法“121.勇闯天涯122.右上角123.右下角124.圆及圆球等的相关计算125.圆及圆球等相关计算126.程序员添加行号127.找出数字128.找幸运数129.找最大数130.整数位数131.重组字符串132.子序列的和133.子字符串替换134.自然数立方的乐趣135.字符串比较136.字符串复制137.字符串加密编码138.字符串逆序139.字符串排序140.字符串替换141.字符串左中右142.组合数143.最次方数144.最大乘积145.最大整数146.最小整数147.最长回文子串148.左上角149.左下角1.“ 1“的传奇#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){int n,i,j,k=0,x=1,y,z,m,p,q,a,s=0;scanf("%d",&n);m=n;for(i=1;i<12;i++){m=m/10;k++;if(m==0)break;}q=n;k=k-1;for(a=1;a<=k;a++){x=x*10;}y=q%x;z=q/x;p=q-y;if(z>=2)s=s+x+z*k*(x/10); elses=s+z*k*(x/10); for(j=p;j<=n;j++){m=j;for(i=1;i<12;i++){x=m%10;if(x==1)s++;m=m/10;if(m==0)break;}}printf("%d",s);return 0;}2.A+B#include <stdio.h>int doubi(int n,int m){n=n+m;n=n%100;return n;}int main(){int t,i,a[100],n,m; scanf("%d",&t);for (i=0;i<=(t-1);i++){scanf("%d%d",&n,&m);a[i]=doubi(n,m);}for (i=0;i<=(t-1);i++)printf("%d\n",a[i]); return 0;}3.A+BⅡ#include <stdio.h>int main(){int A,B,sum;scanf("%d%d",&A,&B);sum=A+B;printf("%d\n",sum);return 0;}4.AB#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){char s[100],q[100];double a,b,c;int n=0,i;scanf("%lf%lf",&a,&b);c=a*b;sprintf(s,"%.0lf",c);for(i=0;i<strlen(s);i++){ n=n+s[i]-48;}while(n>=10){sprintf(q,"%d",n);n=0;for(i=0;i<strlen(q);i++)n=n+q[i]-48;}printf("%d",n);return 0;}5.ACKERMAN#include <stdio.h>#include <stdlib.h>int ack(int x,int y){int n;if (x==0) {n=y+1;return n;}else if (y==0) n=ack(x-1,1);else n=ack(x-1,ack(x,y-1));return n;}int main(){int m,b;scanf("%d%d",&m,&b);m=ack(m,b);printf("%d",m);return 0;}6.Arithmetic Progressions #include <stdio.h>#include <math.h>int g(int n){int i;if(n==1) return 0;if(n==2) return 1;if(n==3) return 1;for(i=2;i<=sqrt(n);i++) if(n%i==0) return 0;return 1;}int f(int a,int b,int c){int i=0,s=a-b;if(c==1&&g(a)==1) return a;if(b==0&&g(a)!=1) return -1;while(1){s=s+b;if(g(s)) i++;if(i>=c) break;}return s;int main(){int a,b,c,d[100],i=0,n;while(1){scanf("%d%d%d",&a,&b,&c);if(a==0&&b==0&&c==0) break;d[i]=f(a,b,c);i++;}n=i;for(i=0;i<n;i++)printf("%d\n",d[i]);return 0;}7.Bee#include <stdio.h>#include <stdlib.h>int main()int A[100],i=0,j,k,female=0,male=1,x;for(;;i++){scanf("%d",&A[i]);if(A[i]==-1)break;}for(j=0;j<i;j++){female=0,male=1;for(k=1;k<A[j];k++){x=female;female=male;male=x+male+1;}printf("%d %d\n",male,female+male+1);}return 0;}8.Checksum algorithm #include <stdio.h>#include <stdlib.h>#include <string.h>int main(){int i,n,t,j;char s[100][100];for(i=0;;i++){gets(s[i]);if(s[i][0]=='#') break;}n=i;for(i=0;i<n;i++){t=0;for(j=0;j<strlen(s[i]);j++)if(s[i][j]==32) t=t;else t=t+(j+1)*(s[i][j]-64);printf("%d\n",t);}return 0;}9.Coin Test#include <stdio.h>#include <stdlib.h>int main(){char A[100000];int n,i=0,a=0,b=0,j;double x;while(1){scanf("%c",&A[i]);if(A[i]=='\n')break;i++;}for(j=0;j<i;j++){if(A[j]=='S'){printf("WA");goto OH;}if(A[j]=='U')a++;if(A[j]=='D')b++;}x=a*1.0/(a+b)*1.0;if(x-0.5>0.003||x-0.5<-0.003) printf("Fail");elseprintf("%d/%d",a,a+b);OH:return 0;}10.Dexter need help#include <stdio.h>int fun(int a){if(a==1) return 1;elsereturn fun(a/2)+1; }int main(){int a,b[100],i=0,j; while(1){scanf("%d",&a);if(a==0)break;b[i]=fun(a);i++;}for(j=0;j<i;j++){printf("%d\n",b[j]); }return 0;}11.Double#include <stdio.h> #include <stdlib.h>int main(){int a[100],b[100],i,j,n,t=0;for(i=0;;i++){scanf("%d",&a[i]);if(a[i]==0) break;}n=i;for(i=0;i<n;i++)b[i]=2*a[i];for(i=0;i<n;i++)for(j=0;j<n;j++)if(a[i]==b[j]) t++;printf("%d",t);return 0;}12.Easy problem#include <stdio.h>#include <math.h>int main(){int N,i,n,j=0;scanf("%d",&N);for(i=2;i<N+1;i++){if((N+1)%i==0)j++;}printf("%d",j/2);return 0;}13.Favorite number #include <stdio.h>#include <string.h>#define MAXNUM 100000int prime_number = 0;int prime_list[MAXNUM]; bool is_prime[MAXNUM]; int ans[MAXNUM + 2];int dp[MAXNUM + 2];void set_prime() {int i, j;memset(is_prime, 0, sizeof(is_prime));for (i = 2; i < MAXNUM; i++) {if (is_prime[i] == 0) {prime_list[prime_number++] = i;if (i >= MAXNUM / i) continue;for (j = i * i; j < MAXNUM; j+=i) {is_prime[j] = 1;}}}}int main() {int i, j, k,o=0,d[100];memset(dp, -1, sizeof(dp));set_prime();ans[0] = 0;dp[1] = 0;for (i = 1; i <= MAXNUM; i++) {ans[i] = ans[i - 1] + dp[i];if (dp[i + 1] == -1 || dp[i + 1] > dp[i] + 1){dp[i + 1] = dp[i] + 1;}for (j = 0; j < prime_number; j++) {if (i > MAXNUM / prime_list[j]) break;k = i * prime_list[j];if (dp[k] == -1 || dp[k] > dp[i] + 1) {dp[k] = dp[i] + 1;}}}while (scanf("%d%d", &i, &j) == 2 && (i || j)) { d[o]=ans[j] - ans[i - 1];o++;}for(i=0;i<o;i++)printf("%d\n",d[i]);}14.Graveyard#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){int a[100],b[100],n,i,j;double s,p,l,t;for(i=0;;i++){scanf("%d%d",&a[i],&b[i]);if(a[i]==0&&b[i]==0) break;}n=i;for(i=0;i<n;i++){p=10000;if(b[i]%a[i]==0){printf("0.0000\n");continue;};t=10000/((double)a[i]);for(j=1;j<a[i]+b[i];j++){l=10000/((double)(a[i]+b[i]));l=t-j*l;l=fabs(l);if(l<p) p=l;}s=(a[i]-1)*p;printf("%.4lf\n",s);}return 0;}15.Hailstone#include <stdio.h>#include <stdlib.h>#include <string.h>int f(int n){int s=1;while(1){if(n==1) return s;else if(n%2==0) n=n/2,s++;else n=3*n+1,s++;}}int main(){int n,m,i,j=0,t;scanf("%d%d",&m,&n);printf("%d %d",m,n);if(m>n) t=m,m=n,n=t;for(i=m;i<=n;i++)if(f(i)>j) j=f(i);printf(" %d",j);return 0;}16.HanoiⅡ#include <stdio.h>#include <stdlib.h>#define M 70int start[M], targe[M];long long f(int *p, int k, int fina) {if(k==0) return 0;if(p[k]==fina) return f(p,k-1,fina);return f(p,k-1,6-fina-p[k])+(1LL<<(k-1));}int main (){long long ans;int n;while(scanf("%d",&n),n){int i;for(i=1;i<=n;i++) scanf("%d",&start[i]);for(i=1;i<=n;i++) scanf("%d",&targe[i]);int c=n;for(;c>=1&&start[c]==targe[c];c--);if(c==0){printf("0\n"); continue;}int other=6-start[c]-targe[c]; ans=f(start,c-1,other)+f(targe,c-1,other)+1;printf("%lld\n",ans);}return 0;}17.Houseboat#include <stdio.h>#include <stdlib.h>#include <math.h>#define pi 3.1415926int f(float x,float y){int i;for(i=0;;i++)if(50*i>sqrt(x*x+y*y)*sqrt(x*x+y*y)*pi/2) break;return i;}int main(){int n,i,a[100];float x,y;scanf("%d",&n);for(i=0;i<n;i++){scanf("%f%f",&x,&y);a[i]=f(x,y);}for(i=0;i<n;i++)printf("%d %d\n",i+1,a[i]);return 0;}18.Music Composer19.Redistribute wealth#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){inta[1000],b[1000],n,i,j,s,sum,t,m,mid,c[100],k=0;while(1){scanf("%d",&n);if(n==0) break;{s=0;for(i=1;i<=n;i++){scanf("%d",&a[i]);s=s+a[i];}m=s/n;b[1]=a[1]-m;b[0]=0;for(i=2;i<n;++i)b[i]=b[i-1]+a[i]-m;for(i=0;i<n;i++)for(j=0;j<n-1-i;j++)if(b[j]>b[j+1])t=b[j],b[j]=b[j+1],b[j+1]=t;mid=b[n/2];sum=0;for(i=0;i<=n-1;++i) sum=sum+fabs(mid-b[i]);c[k]=sum;k++;}}for(i=0;i<k;i++) printf("%d\n",c[i]);return 0;}20.Road trip#include <stdio.h>#include <stdlib.h>#include <math.h>int f(int n){int a[100],b[100],i,s;for(i=0;i<n;i++)scanf("%d%d",&a[i],&b[i]);s=a[0]*b[0];for(i=1;i<n;i++)s=s+a[i]*(b[i]-b[i-1]);return s;}int main(){int n,c[100],i=0;while(1){scanf("%d",&n);if(n==-1) break;c[i]=f(n);i++;}n=i;for(i=0;i<n;i++)printf("%d\n",c[i]);return 0;}21.Scoring#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){int i,j,sum,min,c,count,n,a,b; char s1[50],s2[50];scanf("%d",&n);for(i=0;i<n;i++){count=sum=0;scanf("%s",s2);for(j=0;j<4;j++){scanf("%d%d",&a,&b);if(b!=0){sum+=(a-1)*20+b;count++;}}if(i==0){c=count,min=sum;strcpy(s1,s2);}else if(count>c||(count==c&&sum<min)) {min=sum;c=count;strcpy(s1,s2);}}printf("%s %d %d\n",s1,c,min); return 0;}22.Specialized Numbers#include <stdio.h>#include <stdlib.h>int main(){int i,n,sum10,sum12,sum16;for(i=2992;i<3000;i++){n=i;sum10=0;while(n){sum10+=n%10;n/=10;}n=i;sum12=0;while(n){sum12+=n%12;n/=12;}n=i;sum16=0;while(n){sum16+=n%16;n/=16;}if(sum10==sum12&&sum12==sum16) printf("%d\n",i);}return 0;}23.Sticks#include <stdio.h>#include <string.h>#include <stdlib.h>int len[64], n, minlen, get;bool b[64];int cmp(const void *a, const void *b){return *(int *)a < *(int *)b ? 1 : -1;}bool dfs(int nowlen, int nowget, int cnt) {if(cnt >= n) return false;if(get == nowget) return true;int i;bool f = false;if(nowlen == 0) f = true;for(i = cnt; i < n; i++){if(!b[i]){if(len[i] + nowlen == minlen){b[i] = true;if(dfs(0, nowget+1, nowget))return true;b[i] = false;return false;}else if(len[i] + nowlen < minlen){b[i] = true;if(dfs(nowlen+len[i], nowget, i+1))return true;b[i] = false;if(f) return false;while(i+ 1 < n && len[i]== len[i+1]) i++;}}}return false;}int main(){int i, tollen;while(scanf("%d", &n), n){tollen = 0;int j = 0, p;for(i = 0; i < n; i++){scanf("%d", &p);if(p <= 50){len[j] = p;tollen += len[j];j++;}}n = j;if(n == 0){printf("0\n");continue;}qsort(len, n, sizeof(int), cmp);for(minlen = len[0]; ; minlen++){if(tollen % minlen) continue;memset(b, 0, sizeof(b));get = tollen / minlen;if(dfs(0, 0, 0)){printf("%d\n", minlen);break;}}}return 0;}24.Sum of Consecutive#include <stdio.h>#include <stdlib.h>#include <string.h>int len[64],n,minlen,get;int b[64];int cmp(const void *a,const void *b) {return *(int *)a<*(int *)b?1:-1;}int dfs(int nowlen,int nowget,int cnt){if(cnt>=n) return 0;if(get==nowget) return 1;int i,f=0;if(nowlen==0) f=1;for(i=cnt;i<n;i++){if(len[i]+nowlen==minlen){b[i]=1;if(dfs(0,nowget+1,nowget)) return 1;b[i]=0;return 0;}else if(len[i]+nowlen<minlen){b[i]=1;if(dfs(nowlen+len[i],nowget,i+1)) return 1;b[i]=0;if(f) return 0;while(i+1<n&&len[i]==len[i+1]) i++;}}return 0;}int main(){int i,tollen,q=0,c[100];while(scanf("%d",&n),n){tollen=0;int j=0,p;for(i=0;i<n;i++){scanf("%d",&p);if(p<=50){len[j]=p;tollen+=len[j];j++;}}n=j;if(n==0){printf("0\n");continue;}qsort(len,n,sizeof(int),cmp);for(minlen=len[0];;minlen++){if(tollen%minlen) continue;memset(b,0,sizeof(b));get=tollen/minlen;if(dfs(0,0,0)){c[q]=minlen;q++;break;}}}for(i=0;i<q;i++)printf("%d\n",c[i]);return 0;}25.Symmetric Sort#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){double A[100];int i=0,j=0,k=0,l=0,sum=0;while(1){scanf("%lf",&A[i]);if(A[i]==0)break;i++;}for(j=0;j<i;j++){if(A[j]==2)printf("1\n");else{int B[10000],m=1,number=0;double n;B[0]=2;for(k=3;k<=A[j];k+=2){n=(double)k;for(l=2;l<=sqrt(n);l++){if(k%l==0)goto ai;}B[m]=k;m++;ai:;}for(k=0;k<m;k++){sum=0;for(l=k;l<m;l++){sum+=B[l];if(sum==A[j]){number++;break;}}}printf("%d\n",number);}}return 0;}26.The Clock#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){char s[100][100],a[100];int i,j,n;scanf("%d",&n);for(i=0;i<n;i++) scanf("%s",s[i]);for(i=0;i<n-1;i++)for(j=0;j<n-1-i;j++)if(strlen(s[i])>strlen(s[i+1]))strcpy(a,s[i]),strcpy(s[i],s[i+1]),strcpy(s[i+1],a) ;if(n%2==0){for(i=0;i<n-1;i=i+2) printf("%s ",s[i]);printf("%s ",s[n-1]);for(i=i-3;i>0;i=i-2) printf("%s ",s[i]);}else{for(i=0;i<n-1;i=i+2) printf("%s ",s[i]);printf("%s ",s[n-1]);for(i=i-1;i>0;i=i-2) printf("%s ",s[i]);}return 0;}27.The Ratio of gainers to losers #include<stdio.h>int main(){char s[5];int i,sum=0;gets(s);for(i=0;s[i]!='\0';i++){switch(s[i]){case'I': sum+=1;break; case'V': sum=5-sum;break; case'X':sum=10-sum;break; }}printf("%d\n",sum);return 0;}28.VOL大学乒乓球比赛#include <stdio.h>#include <stdlib.h>int main(){printf("A=Z\nB=X\nC=Y\n");return 0;}29.毕业设计论文打印#include <stdio.h>#include <stdlib.h>int main(){int a[100],j=1,i,n,m;scanf("%d%d",&n,&m);for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=0;i<n;i++)if(a[i]>a[m]) j++;printf("%d",j++);return 0;}30.边沿与内芯的差#include <stdio.h>#include <stdlib.h>int main(){int A[100][100],i,j,m,n,s=0,t=0;scanf("%d%d",&n,&m);for(i=1;i<=n;i++){for(j=1;j<=m;j++){scanf("%d",&A[i][j]);。

西安工业大学C语言程序设计试题及参考答案-2019

西安工业大学C语言程序设计试题及参考答案-2019

计算机程序设计基础2018-2019学年第二学期一.选择题:(共30分,每题1分)1.关于C语言中的变量,以下说法正确的是______。

A)变量的类型可以根据需要随时改变B)变量的类型是在定义的时候确定的C)变量可以没有数据类型D)变量的取值范围与其类型没有关系2.以下数据定义中,有语法错误的是______。

A)double A,a; B)float _0,*q[7]; C)char For; D)int n=6,G[n];3.以下选项中,非法的C语言常量是______。

A)'\\'B)6.18e-5 C)0x5G D)0L4.如果要使x=3.1415926正确赋值,则x应定义为______。

A)int x; B)long x; C)float x; D)double x;5.下面定义的数组中,占用内存空间最大的是______。

A)char a[20]; B)int b[5]; C)float c[4]; D)double d[3];6.若有定义:int x=5, y=7;, 则表达式++(x+y)的值是______。

A)12 B)13 C)14 D)有语法错误7.若有定义:int x=18; 则执行语句x=5/9*x+5;后,变量s的值是______。

A)5 B)10 C)15 D)08.若有定义:int x;,能使表达式 5<x<10的值为1的所有数据是______。

A)6,7,8,9 B)大于5的所有整数C)所有的整数D)小于10的所有整数9.若有定义:int m=3.8, n=3; ,则以下表达式值为0的是______。

A)m||n B)m/n C)m==n D)m>n && n>0 10.下面的C语言运算符中,优先级高于“!=”但低于“+”的运算符是____。

A)&& B)>= C)/ D)=11.若int x=5, y=6, p=0 ; 则执行以下语句后p值是______。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

一.1.第一季10题全(注:第五题问题已经解决,确认AC!)#include <stdio.h>int main(){int a,b,sum;scanf("%d%d",&a,&b);sum=a+b;printf("%d\n",sum);return 0;}2.#include <stdio.h>#define PI 3.1415926int main(){double r,h,l,s,sq,vq,vz;scanf("%lf%lf",&r,&h);l=2*PI*r;s=PI*r*r;sq=4*PI*r*r;vq=4*PI*r*r*r/3;vz=s*h;printf("%.2lf\n%.2lf\n%.2lf\n%.2lf\n%.2lf\n", l,s,sq,vq,vz);return 0; }3.#include <stdio.h>int main(){int a,b,c;double d,e;scanf("%d%d%d",&a,&b,&c); d=a+b+c;e=d/3;printf("%lf\n%lf\n",d,e);return 0;}4.#include <stdio.h>int main(){int a,b,c;scanf("%d%d%d",&a,&b,&c); if(a<b)a=b;if(a<c)a=c;printf("%d\n",a);return 0;}5.#include<stdio.h>int main(){int i=0,j=0,k=1;char a[6];while((a[i]=getchar())!='\n') {i++;}for(;i>0;i--){if(a[j]==a[i-1]){ j++;continue;}else {k=0;break;}}if(k==1)printf("yes\n"); elseprintf("no\n"); }6.#include<stdio.h>int main(){double a,c;scanf("%lf",&a);switch((int)a/10){case 0:c=a*0.1;break;case 1:c=(a-10)*0.075+10*0.1;break;case 2:case 3:c=(a-20)*0.05+10*0.075+10*0.1;br eak;case 4:case 5:c=(a-40)*0.03+20*0.05+10*0.075+1 0*0.1;break;case 6:case 7:case 8:case 9:c=(a-60)*0.015+20*0.03+20*0.05+1 0*0.075+10*0.1;break;default:c=(a-100)*0.01+40*0.015+20*0 .03+20*0.05+10*0.075+10*0.1;}printf("%lf\n",c);return 0;}7.#include<stdio.h>int main(){double a,b,c;scanf("%lf",&a);c=(int)a;if(a>c)a=c+1;if(a>15)b=(a-15)*2.1+7+13*1.5; else {if(a>2)b=(a-2)*1.5+7;else b=7;}printf("%lf\n",b);return 0; }8.#include <stdio.h>int main(){int a,b,c,e,f=30,g=31,n;scanf("%d-%d-%d",&a,&b,&c);if((a%400==0)||(a%100!=0&&a%4==0)) e=29;elsee=28;switch (b){case 1:n=c;break;case 2:n=g+c;break;case 3:n=g+e+c;break;case 4:n=g+e+g+c;break;case 5:n=g+e+g+f+c;break;case 6:n=g+e+g+f+g+c;break;case 7:n=g+e+g+f+g+f+c;break;case 8:n=g+e+g+f+g+f+g+c;break;case 9:n=g+e+g+f+g+f+g+g+c;break;case 10:n=g+e+g+f+g+f+g+g+f+c;break; case 11:n=g+e+g+f+g+f+g+g+f+g+c;break; default: n=g+e+g+f+g+f+g+g+f+g+f+c;}printf("%d\n",n);return 0;}9.#include <stdio.h> int main(){int x;scanf("%d",&x);if(x>=90&&x<=100) printf("A\n");else if (x>=80)printf("B\n");else if (x>=70)printf("C\n");else if (x>=60)printf("D\n");elseprintf("E\n");return 0;}10.#include<stdio.h>int main(){double x,y,s;scanf("%lf,%lf",&x,&y);s=(x+2)*(x+2)+(y-2)*(y-2); if(s>1){s=(x+2)*(x+2)+(y+2)*(y+2); if(s>1){s=(x-2)*(x-2)+(y+2)*(y+2);if(s>1){s=(x-2)*(x-2)+(y-2)*(y-2);if(s>1){printf("0\n");return 1;} }}}printf("10\n");return 0;}二。

第二季15题全11.#include<stdio.h>int main(){int x=0,a=0,c=0,i=1;scanf("%d%d",&x,&a);c=x;for(i=1;i<a;i++){c=(c*x)%1000;};if(c<100){if(c<10)printf("00%d\n",c);elseprintf("0%d\n",c);};if(c>=100)printf("%d\n",c);return 0;}12.#include<stdio.h>int main(){int i=1,a=0,b=0,c=1016,k=1;for(;k=1;){c=c+4;a=c;b=c;for(i=1;i<=4;i++){a=a/4*5+1;if(a%4!=0){k=0;;};};a=a/4*5+1;if(k==1){printf("%d %d\n",a,b);break;};};return 0;}13。

#include<stdio.h>#include<math.h>int main(){int i=1,n=0,b=0;double j=0,k=0,m=0;scanf("%d",&n);for(;i<=n;i++){b=i%2-1;k=pow(-1,b);m=pow(2,i);j+=k*m/(m-k)/(2*m+k);};printf("%.6lf\n",j);return 0;}14.#include<stdio.h>#include<math.h>int main(){int a=0,b=0,i=0,t;scanf("%d%d",&a,&b);if(a>b) {t=a;a=b;b=t;}for(;a<=b;a++){ int m=1;for(i=2;i<=sqrt(a);i++){if(a%i==0){m=0;break;};}if(m==1)printf("%d ",a);}printf("\n");return 0;}15.#include<stdio.h>#include<math.h>int main(){int a=0,b=0,c=0;scanf("%d",&a);b=(int)pow(a,2);c=(int)pow(a,3);if(a%2){ printf("%d*%d*%d=%d=%d",a,a,a,c,b-a/2*2);int n=1;for(;n<a;n++){printf("+%d",b-a/2*2+2*n);}printf("\n");};if(a%2==0){int n=1;printf("%d*%d*%d=%d=%d",a,a,a,c,b+1-a);for(;n<a;n++){printf("+%d",b+1-a+n*2);}printf("\n");};return 0;}16#include<stdio.h>#include<math.h>int main(){int a=0,b=0,c=0,d=0,e=0,x=0,y=0,z=0,u=0,v=0;double f=0;for(a=0;a<=2;a++){double m=0;for(b=0;b<=(20-8*a)/5;b++){for(c=0;c<=(20-8*a-5*b)/3;c++){for(d=0;d<=(20-8*a-5*b-3*c)/2;d++){e=20-8*a-5*b-3*c-2*d;m=2000*pow(1+0.0084*12*8,a)*pow(1+0.0075*12*5,b)*pow(1+0.0069*12*3,c)*pow(1+0.0066 *12*2,d)*pow(1+0.0063*12,e);if(f<m){f=m;x=a,y=b,z=c,u=d,v=e;};};};};};printf("%d %d %d %d %d\n%.2lf\n",x,y,z,u,v,f);return 0;}17.#include<stdio.h>#include<math.h>int main(){double a=0;int i=0,b=0;scanf("%lf",&a);b=(int)a;for(;(int)(b/pow(10,i));)i++;printf("%d\n",i);return 0;}18。

相关文档
最新文档