Demo entry 6642381

f90

   

Submitted by zyx on Sep 23, 2017 at 08:34
Language: Fortran. Code size: 1.2 kB.

program zyx01
implicit none
  integer n
  real(8) x
  real(8),allocatable::a(:)!声明一个可变大小的双精度浮点数数组
  real(4),allocatable::b(:)!声明一个可变大小的单精度浮点数数组
  !real(4),allocatable::x(:)!声明一个可变大小的单精度浮点数数组
  real(8)::add_1=0.0
  real(4)::add_2=0.0
  real(4)::add_3,add_4,add_5
  real(4)::s,c,y,t,temp
  integer i,j
  write(*,*) "input n"
  read (*,*) n
  allocate(a(n)) !给可变大小的数组给定大小
  allocate(b(n))

  !方案一和方案二
  do i=1,n
    call random_number(x) !产生【0,1】之间的随机数
    a(i)=x
    b(i)=x
    add_1=add_1+a(i)!使用双精度及随机数产生的顺序求和
    add_2=add_2+b(i)!使用单精度及随机数产生的顺序求和
    write(*,*) "双",i,"=",a(i),"单",i,"=",b(i)
  end do

  !方案三
  s=b(1)
  c=0

  do i=2,n !按题目所给的方式处理随机数(第三种方法)
    y=b(i)-c
    t=s+y
    c=(t-s)-y
    s=t
  end do
  add_3=s

  !方案四和方案五
  do i=1,n-1!将单精度浮点数数组从大到小进行排序
    do j=i+1,n
      if(b(i)<b(j)) then
        temp=b(i)
        b(i)=b(j)
        b(j)=temp
      end if
    end do
  end do

  add_4=0.0
  add_5=0.0
  do i=1,n  !按从大到小的顺序求和
    add_4=add_4+b(i)
  end do

  do i=n,1,-1 !按从小到大的顺序求和
    add_5=add_5+b(i)
  end do

  write(*,*) "第一种方法",add_1,"第二种方法",add_2," 第三种方法",add_3,"第四种方法",add_4,"第五种方法",add_5
end program

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).