1. 程式人生 > 實用技巧 >Spark_4:Spark常見操作函式

Spark_4:Spark常見操作函式

實驗任務1:

#include <math.h>
#include <stdio.h>
int main(){
  float a, b, c, x1, x2;
  float delta, real, imag;

  printf("Enter a, b, c:   " );

  while (scanf("%f%f%f",&a,&b,&c ) != EOF) {
    if (a == 0)
      printf("not quadratic equation.\n\n");
    else{
      delta = b*b -4
*a*c; if (delta >= 0) { x1 = (-b + sqrt(delta)) / (2*a); x2 = (-b - sqrt(delta)) / (2*a); printf("x1 = %.2f,x2 = %.2f\n\n",x1, x2); } else{ real = -b/(2*a); imag = sqrt(-delta) / (2*a); printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n\n"
, real,imag, real, imag); } } printf("Enter a, b, c: "); } return 0; }

實驗任務2:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
int main()
{
    int x, n;
    srand( time( 0 ) );
    n = 0;
    do
    {
        n++;
        x = rand() % 10;
        printf( 
"%3d", x ); } while ( n < N ); printf( "\n" ); return(0); }

實驗任務3:

#include <stdio.h>
#include <math.h>

int main(){
  int x,y=100,z=0,i;
  while (y<=199){
    y+=1;
    x=(int)sqrt( (double) y);
    
    for (i = 2; i <= y; i++)
      if(y%i==0)
      break;
      
    if (i>x){
        z+=1;
        if (z%5==0)
          printf("%d\n",y);
          else
        printf("%d ",y);
          }
    }
}

實驗任務4:

從最低位開始判斷,用能否整除2來判讀是否是奇數,每判斷一位,使原始的數字/10。

#include <stdio.h>
#include <math.h>

int main(){
  long x;
  int y,z,i;
  printf("Enter a number:");
  while (scanf("%ld",&x)!=EOF) {
    i=0,y=0;
    while (x!=0) {
      z=x%10;
      if (z%2!=0) {
        y=pow(10,i)*z+y;
        i+=1;
      }
      x=x/10;

    }
        printf("new number is:%d\n",y );
        printf("\nEnter a number:");

  }
}

實驗任務5:

#include <stdio.h>
int main(){
  int n,i,c,f;
  double s,b;
  printf("Enter n(1~10):");
  while (scanf("%d",&n)!=EOF) {
    c = 1,f=-1,s=0;
    for (i = 1; i <= n; i++) {
      f=f*(-1);
      c=c*i;
      b=1.0/c*f;
      s=s+b;
    }
  printf("n=%d, s=%f\n",n,s);
    printf("\nEnter n(1~10):");
  }
  return 0;
}

實驗任務6:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
  int date,cai,i,a=0;
  srand(time(0));
  date=rand()%31;
  printf("猜猜2020年12月哪一天會是你的lucky day\n開始嘍,你有三次機會,猜吧(1~31):");
  for (i = 0; i < 3; i++) {
    scanf("%d",&cai);
    if(cai>date)printf("\n你猜的日期晚了,lucky day悄悄溜到前面啦");
    else if(cai<date)printf("\n你猜的日期早了,lucky day還沒到呢");
    else {
      a=1;
      printf("\n猜中了!");
      break;
    }
    if(i!=2)printf("\n再猜(1~31): ");

  }
  if (a==0){
    printf("\n\n次數用完啦。偷偷告訴你:12月,你的lucky day是%d號",date);
  }
}