山东大学(威海)2022级大一下C习题集(1)

  • 1-7-1 求幂级数展开的部分和
  • 1-7-2 查询水果价格
  • 1-7-3 猜数字游戏
  • 1-7-4 特殊a串数列求和
  • 1-7-5 成绩统计分析表
  • 1-7-6 换硬币
  • 1-7-7 验证“哥德巴赫猜想”

1-7-1 求幂级数展开的部分和

#include#includeint main(){double x = 0;scanf("%lf",&x);double add = 1.0,sum = 1.0;int i = 1;for(i = 1;;i++){add=(add*x)/i;sum+=add;if(add<0.00001) break;}printf("%.4lf",sum);return 0;}

1-7-2 查询水果价格

#includevoid menu(){printf("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit\n");}int main(){menu();int c = 0, n;while(++c<6){scanf("%d",&n);if(n == 0)break;switch(n){case 1:printf("price = 3.00\n");break;case 2:printf("price = 2.50\n");break;case 3:printf("price = 4.10\n");break;case 4:printf("price = 10.20\n");break;default:printf("price = 0.00\n");break;}}return 0;}

1-7-3 猜数字游戏

#includeint main(){ int rad1, N; scanf("%d%d", &rad1, &N);int gs = 0; if(rad1>0&&rad1<=100&&N>3){for(int i = 0;i<N;i++) { scanf("%d", &gs); if (gs <= 0) { printf("Game Over"); return 0; }else if (gs < rad1)printf("Too small\n");else if(gs>rad1)printf("Too big\n");else if(gs==rad1&&i == 0){printf("Bingo!\n");return 0;}else if(gs == rad1&&(i == 1||i==2)){printf("Lucky You!\n"); return 0;}else if(gs == rad1){printf("Good Guess!\n"); return 0;}}printf("Game Over\n");return 0;}}

1-7-4 特殊a串数列求和

#includeint main(){int a,n;scanf("%d%d",&a,&n);int s = 0,sum = 0;for(int i = 1;i<=n;i++){s = s*10+a;sum += s;}printf("s = %d",sum);}

1-7-5 成绩统计分析表

#includevoid Print_s(double i)//把打印*封装成一个函数,方便{ int n = (int)floor(i+0.5);//四舍五入 for(int j = 0;j<n;j++)printf("*");}int main(){int n;scanf("%d", &n);double arr[1000] = { 0.0 };double sum = 0.0,max = 0.0,min = 1000.0,avg;int a=0,b=0,c=0,d=0,e=0;for (int i = 0; i < n; i++){scanf("%lf", &arr[i]);sum += arr[i];if(arr[i]>max)max = arr[i];if(arr[i]<min)min = arr[i];if(arr[i]>=90&&arr[i]<=100)a++;else if(arr[i]>=80&&arr[i]<90)b++;else if(arr[i]>=70&&arr[i]<80)c++;else if(arr[i]>=60&&arr[i]<70)d++;else if(arr[i]>=0&&arr[i]<60)e++;}for (int i = 0; i < n; i++){printf("%03d:%6.1lf ", i+1, arr[i]);Print_s(arr[i]);printf("\n");}avg = sum / n;printf("\nMax: %5.1lf ",max);Print_s(max);printf("\nMin: %5.1lf ",min);Print_s(min);printf("\nAvg: %5.1lf ",avg);Print_s(avg);printf("\n");double A = 100.0*a/n,B=100.0*b/n,C = 100.0*c/n,D = 100.0*d/n,E = 100.0*e/n;//printf里打印一个百分号“%”要写两个:%%printf("\nA:%6.1lf%%",A);printf(" ");Print_s(A);printf("\nB:%6.1lf%%",B);printf(" ");Print_s(B);printf("\nC:%6.1lf%%",C);printf(" ");Print_s(C);printf("\nD:%6.1lf%%",D);printf(" ");Print_s(D);printf("\nE:%6.1lf%%",E);printf(" ");Print_s(E);return 0;}

1-7-6 换硬币

#includeint main(){int x = 0;scanf("%d", &x);int f5 = 0, f2 = 0, f1 = 0;int count = 0;for (int i = x/5;; i--){f5 = i;for (int j = (x-5*f5)/2;; j--){f2 = j;f1 = x - 5 * f5 - 2 * f2;if (f2 == 0){ break;}if (f1 <= 0){continue;}printf("fen5:%d, fen2:%d, fen1:%d, total:%d\n", f5, f2, f1, f1 + f2 + f5);count++;}if ((f5 == 1)){printf("count = %d\n", count);break;}}return 0;}

1-7-7 验证“哥德巴赫猜想”

#include#includeint Prime(long long i){int c = 0;for(long long int j = 2;j<=sqrt(i);j++){if(i%j==0)return 0;}return 1;}int main(){long long int n;scanf("%lld",&n);for(long long int i = 2;i<n;i++){if(Prime(i)&&Prime(n-i)){printf("%lld = %lld + %lld",n,i,n-i);break;}else continue;}return 0;}