免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 發帖

c++練習題8

1542183111055.jpg
2018-11-14 16:14
  1. #include<stdio.h>
  2. int IsPrime(int x){
  3.         if(x==1){
  4.                 return 0;
  5.         }
  6.         for(int i=2;i*i<=x;i++){
  7.                 if(x%i==0){
  8.                         return 0;
  9.                 }
  10.         }
  11.         return 1;
  12. }
  13. int main(void){
  14.         int x;
  15.         printf("輸入數字: ");
  16.         scanf("%d",&x);
  17.         printf("%d",IsPrime(x));
  18. }
複製代碼
  1. #include<stdio.h>
  2. int EvalPoly(int a,int b,int c,int x){
  3.         int pro;
  4.         pro = a*x*x+b*x+c;
  5.         return pro;
  6. }

  7. int main(void){
  8.         int a,b,c,x;
  9.         printf("輸入a、b、c、x: ");
  10.         scanf("%d %d %d %d",&a,&b,&c,&x);
  11.         printf("%d",EvalPoly(a,b,c,x));
  12. }
複製代碼
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

1542183123075.jpg
2018-11-14 16:16
  1. #include<stdio.h>
  2. double Round(double x,double n){
  3.         double a;
  4.         int b;
  5.         a = x;
  6.         for(int i=1;i<=n;i++){
  7.                 a *= 10;
  8.         }
  9.         b = int(a+0.5);
  10.         a = b;
  11.         for(int i=1;i<=n;i++){
  12.                 a /= 10;
  13.         }
  14.         return a;
  15. }

  16. int main(void){
  17.         double x,n;
  18.         printf("輸入數字和有效位數: ");
  19.         scanf("%lf %lf",&x,&n);
  20.         printf("%lf",Round(x,n));
  21. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

好吧我不確定題目的意思。

1542183154892.jpg
2018-11-14 16:16
  1. #include<stdio.h>
  2. int force(double volt){
  3.         if(volt>0.2){
  4.                 return 2;
  5.         }
  6.         else if(volt<-0.2){
  7.                 return -1;
  8.         }else{
  9.                 return 0;
  10.         }
  11. }

  12. int main(void){
  13.         double pos,volt;
  14.         printf("輸入初始位置: ");
  15.         scanf("%lf",&pos);
  16.        
  17.         while(scanf("%lf",&volt)){
  18.                 pos += force(volt);
  19.         }
  20.        
  21.         printf("\n最終位置: %lf",pos);
  22. }
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

1542183183693.jpg
2018-11-14 16:18

成果圖:
1542183466004.jpg
2018-11-14 16:18

1542183545670.jpg
2018-11-14 16:19
  1. #include<stdio.h>
  2. int firstday(int x){
  3.         int a;
  4.         a = (x + (x-1)/4 - (x-1)/100 + (x-1)/400) % 7;
  5.         return a;
  6. }

  7. int leapyear(int x){
  8.         if(x%400==0){
  9.                 return 1;
  10.         }
  11.         if(x%4==0 && x%100!=0){
  12.                 return 1;
  13.         }
  14.         return 0;
  15. }

  16. void block(int day){
  17.         for(int i=1;i<=day;i++){
  18.                 printf("   ");
  19.         }
  20. }

  21. int main(void){
  22.         int x,day,sign=28;
  23.         printf("輸入年份: ");
  24.         scanf("%d",&x);
  25.        
  26.         printf("year: %d",x);
  27.         day = firstday(x);
  28.        
  29.         if(leapyear(x)==1){
  30.                 sign = 29;
  31.         }

  32.         printf("\n一月  \n  S  M  T  W  T  F  S \n");
  33.         block(day);
  34.         for(int j=1;j<=31;j++){
  35.                 printf("%3d",j);
  36.                 day++;
  37.                 if(day==7){
  38.                         day = 0;
  39.                         printf("\n");
  40.                 }
  41.         }
  42.        
  43.         printf("\n二月  \n  S  M  T  W  T  F  S \n");
  44.         block(day);
  45.         for(int j=1;j<=sign;j++){       
  46.                 printf("%3d",j);       
  47.                 day++;
  48.                 if(day==7){       
  49.                         day = 0;
  50.                         printf("\n");
  51.                 }
  52.         }
  53.        
  54.         printf("\n三月  \n  S  M  T  W  T  F  S \n");
  55.         block(day);
  56.         for(int j=1;j<=31;j++){
  57.                 printf("%3d",j);
  58.                 day++;
  59.                 if(day==7){
  60.                         day = 0;
  61.                         printf("\n");
  62.                 }
  63.         }
  64.        
  65.         printf("\n四月  \n  S  M  T  W  T  F  S \n");
  66.         block(day);
  67.         for(int j=1;j<=30;j++){
  68.                 printf("%3d",j);
  69.                 day++;
  70.                 if(day==7){
  71.                         day = 0;
  72.                         printf("\n");
  73.                 }
  74.         }
  75.        
  76.         printf("\n五月  \n  S  M  T  W  T  F  S \n");
  77.         block(day);
  78.         for(int j=1;j<=31;j++){
  79.                 printf("%3d",j);
  80.                 day++;
  81.                 if(day==7){
  82.                         day = 0;
  83.                         printf("\n");
  84.                 }
  85.         }
  86.        
  87.         printf("\n六月  \n  S  M  T  W  T  F  S \n");
  88.         block(day);
  89.         for(int j=1;j<=30;j++){
  90.                 printf("%3d",j);
  91.                 day++;
  92.                 if(day==7){
  93.                         day = 0;
  94.                         printf("\n");
  95.                 }
  96.         }
  97.        
  98.         printf("\n七月  \n  S  M  T  W  T  F  S \n");
  99.         block(day);
  100.         for(int j=1;j<=31;j++){
  101.                 printf("%3d",j);
  102.                 day++;
  103.                 if(day==7){
  104.                         day = 0;
  105.                         printf("\n");
  106.                 }
  107.         }

  108.         printf("\n八月  \n  S  M  T  W  T  F  S \n");
  109.         block(day);
  110.         for(int j=1;j<=31;j++){
  111.                 printf("%3d",j);
  112.                 day++;
  113.                 if(day==7){
  114.                         day = 0;
  115.                         printf("\n");
  116.                 }
  117.         }
  118.        
  119.         printf("\n九月  \n  S  M  T  W  T  F  S \n");       
  120.         block(day);
  121.         for(int j=1;j<=30;j++){
  122.                 printf("%3d",j);
  123.                 day++;
  124.                 if(day==7){
  125.                         day = 0;
  126.                         printf("\n");
  127.                 }
  128.         }

  129.         printf("\n十月  \n  S  M  T  W  T  F  S \n");
  130.         block(day);
  131.         for(int j=1;j<=31;j++){
  132.                 printf("%3d",j);
  133.                 day++;
  134.                 if(day==7){
  135.                         day = 0;
  136.                         printf("\n");
  137.                 }
  138.         }
  139.        
  140.         printf("\n十一月  \n  S  M  T  W  T  F  S \n");
  141.         block(day);
  142.         for(int j=1;j<=30;j++){
  143.                 printf("%3d",j);
  144.                 day++;
  145.                 if(day==7){
  146.                         day = 0;
  147.                         printf("\n");
  148.                 }
  149.         }
  150.        
  151.         printf("\n十二月  \n  S  M  T  W  T  F  S \n");
  152.         block(day);
  153.         for(int j=1;j<=31;j++){
  154.                 printf("%3d",j);
  155.                 day++;
  156.                 if(day==7){
  157.                         day = 0;
  158.                         printf("\n");
  159.                 }
  160.         }               
  161. }                 
複製代碼

小貓貓2024了喔!
(點一下露米亞傳送到小貓貓2024大事記)

TOP

返回列表