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

[自high] c++練習題3

1.
Write programs to accumulate and print the following:
where n reads from the keyboard
  (a).  20 +22 + 24 +…+ 2n(對,原題目就是寫2n,讓人匪夷所思,我自認為是2n+18)  
  (b).  1 + 1/3 + 1/5 +. . .+ 1/101(同上,題目連n都不見了,我自認為是1/2n-1)    
  (c).  1 + 1/2! + 1/3! + . . .+ 1/n!  
  (d).  1-2 + 3 -4 + 5 + . . . +   n
  (e).  1+ (1+2) + (1+2+3) +(1+2+3+4) +…+(1+2+3+…+ n)
  1. #include<stdio.h>
  2. int main(void){
  3.        
  4.         int n,i,j;
  5.         double q1,q2,q3,q4,q5;

  6.         printf("input the n for test : ");
  7.         scanf("%d",&n);
  8.        
  9.         for(i=0;i<n;++i){
  10.                 q1 += 20+2*i;
  11.         }
  12.        
  13.         for(i=0;i<n;++i){
  14.                 q2 += 1.0/(2*i+1);
  15.         }
  16.        
  17.         for(i=1,j=1;i<n+1;++i){
  18.                 j = i*j;
  19.                 q3 += 1.0/j;
  20.         }
  21.        
  22.         for(i=1,j=-1;i<n+1;++i){
  23.                 j = j*-1;
  24.                 q4 += i*j;
  25.         }
  26.        
  27.         for(i=1,j=0;i<n+1;++i){
  28.                 j = j+i;
  29.                 q5 += j;
  30.         }
  31.        
  32.         printf("\nA1: %8lf",q1);
  33.         printf("\nA2: %8lf",q2);
  34.         printf("\nA3: %8lf",q3);
  35.         printf("\nA4: %8lf",q4);
  36.         printf("\nA5: %8lf",q5);
  37.        
  38.         return 0;
  39. }
複製代碼
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友

小貓貓2025了喔!
(點一下彌宙傳送到小貓貓2025大事記)

4.
A common highway patro speed-detection radar unit emits a beam of microwaves at a frequency f0.
The beam is reflected off an approaching car, and the reflected beam is picked up and analyzed by the radar unit.
The frequency of the reflected beam is shifted slightly from f0 to f1 due to the motion of the car.
The relationship between the speed of the car, v, in miles per hour and the two microwave frequencies is   
v = (6.685*10^8)*(f1-f0)/(f1+f0)   
Where the emitted waves have a frequency of f0= 2*10^10 sec-1
(a).Using this formula, write a program to calculate and display the speed corresponding to a received frequency of 2.0000004*10^10sec-1
(b). Modify the above program to determine the frequency that will be returned by a car traveling at 55 miles per hour.
  1. #include<stdio.h>
  2. int main(void){
  3.         const double det=6.685e+8,f0=2e+10;
  4.         double f1,v;
  5.        
  6.         printf("case1 input f1 : ");
  7.         scanf("%lf",&f1);
  8.         v = det*(f1-f0)/(f1+f0);
  9.         printf("the speed is %lf",v);
  10.        
  11.         printf("\ncase2 input v : ");
  12.         scanf("%lf",&v);
  13.         f1 = ((v+det)*f0)/(det-v);
  14.         printf("the f1 is %.10le",f1);
  15.        
  16.         return 0;
  17. }
複製代碼

小貓貓2025了喔!
(點一下彌宙傳送到小貓貓2025大事記)

TOP

3.
A polynomial an x^n + an-1  x^n-1 + … + a0  can be evaluated(計值)
方法(A). In a straightforward way by performing the indicated operations and using power function.
方法(B). An alternative method is to factor the polynomial according to the following formula, known as HORNER’S Rule:
        (…( ( an x + an-1  ) x + an-2 ) x + …+ a1  ) + a0
(1). Write two programs to evaluate polynomials by these two different methods.
     為簡化題目,多項式最高項次方小於等於5,係數由keyboard key in
(2). Please compare these two methods.
(方法A,B需幾次的乘算及加算)
Ex: f(x)=5x^4 +4x^3 +3x^2 +2x+1 ( input x from keyboard)
方法(B):  let a0 =1,  a1 =2,  a2 =3,  a3 =4,  a4 =5
We have : f0 = a4 ,  f1 = f0 * x + a3 ,  f2 = f1 * x + a2 ,  f3 = f2 * x + a1
f4  = f3  * x + a0  = 5x^4 +4x^3 +3x^2 +2x+1  ------->Answer
( In general form f  = f *x + a  ,  f =a  )
  1. #include<stdio.h>
  2. int main(void){
  3.         int a1,a2,a3,a4,a5,r;
  4.         double x,ans;
  5.        
  6.         printf("please input a1 :");
  7.         scanf("%d",&a1);
  8.         printf("please input a2 :");
  9.         scanf("%d",&a2);
  10.         printf("please input a3 :");
  11.         scanf("%d",&a3);
  12.         printf("please input a4 :");
  13.         scanf("%d",&a4);
  14.         printf("please input a5 :");
  15.         scanf("%d",&a5);
  16.         printf("please input r :");
  17.         scanf("%d",&r);
  18.        
  19.         printf("please input x :");
  20.         scanf("%lf",&x);
  21.        
  22.         //case1
  23.        
  24.         ans = a1*x*x*x*x*x+a2*x*x*x*x+a3*x*x*x+a4*x*x+a5*x+r;
  25.         printf("\ncase 1 answer is %lf",ans);
  26.        
  27.         //case2
  28.        
  29.         double f1,f2,f3,f4;
  30.        
  31.         f1 = a1*x+a2;
  32.         f2 = f1*x+a3;
  33.         f3 = f2*x+a4;
  34.         f4 = f3*x+a5;
  35.        
  36.         ans = f4*x+r;
  37.         printf("\ncase 2 answer is %lf",ans);
  38.        
  39.         //compare
  40.        
  41.         printf("\nmethod 1 use '*' 15 times and '+' 5 times  ");
  42.         printf("\nmethod 2 use '*'  5 times and '+' 5 times ");

  43.         return 0;
  44. }
複製代碼

小貓貓2025了喔!
(點一下彌宙傳送到小貓貓2025大事記)

TOP

2.
Write a program that displays the ASCII chart. Display numeric codes as well as characters so that there are four columns and as many rows as it takes.
For an extra bonus, display the chart so that its outside borders are made up of double line ASCII characters, and separate each column with a single vertical ASCII character.
  1. #include<stdio.h>
  2. int main(void){
  3.         int i;
  4.         printf("╔══════════╗\n");

  5.         for(i=0;i<129;++i){
  6.                 printf("║ %c │%3d ",i,i);
  7.                 if(i<128){
  8.                         i += 1;
  9.                         printf("│ %c │%3d ║\n",i,i);
  10.                         printf("╠══════════╣\n");
  11.                 }else{
  12.                         printf("│   │    ║\n");
  13.                 }
  14.         }
  15.        
  16.         printf("╚══════════╝");
  17.        
  18. }
複製代碼

小貓貓2025了喔!
(點一下彌宙傳送到小貓貓2025大事記)

TOP

返回列表