A set of C ++ exercises (including answers) for your reference! Author: yaozheng

Source: Internet
Author: User
Some of the questions I have helped others do today are representative. I will post all the answers here for your reference. All programs are successfully debugged in the Visual C ++ 6.0 environment. If you have a better solution to a program, you are welcome to share with us. Because of the limited time, no comments are written. If you do not understand any program, you can also share with me.

I. multiple choice questions:
1. The extension of the C ++ source program file is:
A). cpp B). c). dll d). exe

2. Assign the lowercase letter N to the character variable one_char. The correct operation is: C.
A) one_char = '/N'; B) one_char = "N ";
C) one_char = 110; d) one_char = 'n ';

3. After I defines the integer variable, the initial value is assigned as B.
Int I = 2.8*6;
A) 12 B) 16 C) 17 d) 18

4. Which of the following expressions is false? C
A) 1 <3 & 5 <7 B )! (2> 4) c) 3 & 0 & 1 d )! (5 <8) | (2 <8)

5. Set int A = 10, B = 11, c = 12; expression (A + B). The value of <C & B = C is B.
A) 2 B) 0 C)-2 d) 1

6. After the following programs are executed, the value of X is: c
Int x = 0;
For (int K = 0; k <90; k ++)
If (k) x ++;
A) 0 B) 30 C) 89 d) 90

7. The number of cycles of the following program segments is:
Int x =-10;
While (++ X) cout <x <Endl;
A) 9 B) 10 C) 11 d) Unlimited

8. It indicates the number "greater than 10 and less than 20". Correct: d
A) 10 <x <20 B) x> 10 | x <20 C) x> 10 & x <20 d )! (X <= 10 | x> = 20)

9. If the integer variable X = 2, the result of expression x <2 is: d
A) 2 B) 4 C) 6 d) 8

10. If expression a = 1 and B = 2, the values of the expressions (A ++) + B and A ++ B are:
A) 3, 3 B) 3, 4 C) 4, 3 d) 4

2. Fill in the program blank.
1. The following program calculates the sum of natural numbers that can be divisible by 3 within 1000.
# Include <iostream. h>
Void main ()
{Int x = 1, sum;
Sum = 0 _______;
While (true)
{If (x> 1000) break;
If (X % 3 = 0) sum + = X;
X ++;
}
Cout <sum <Endl;
}
3. Input 10 integers: 32, 64, 53,87, 54,32, 98,56, 98,83. What are the output results of the following programs?
# Include <iostream. h>
Void main ()
{Int A, B, C, X;
A = B = C = 0;
For (int K = 0; k <10; k ++)
{CIN> X;
Switch (X % 3)
{Case 0: A + = x; break;
Case 1: B + = x; break;
Case 2: C ++ = x; break;
}
}
Cout <A <"," <B <"," <C <Endl;
}
Result:
141,64, 452

4. write out the following program running results.
# Include <iostream. h>
Void main ()
{Int J, K;
For (j = 5; j> 0; j --)
{For (k = J; k> 0; k --)
Cout <"*";
Cout <Endl;
}
}
Result:
*****
****
***
**
*

5. Write the running results of the following programs.
# Include <iostream. h>
# Include <iomanip. h>
Void main ()
{
Cout <"x_width =" <cout. Width () <Endl;
Cout <"x_fill =" <cout. Fill () <Endl;
Cout <"x_precision =" <cout. Precision () <Endl;
Cout <123 <"<123.45678 <Endl;
Cout <"*** x_width = 10, x_fill = &, x_precision = 8 ***" <Endl;
Cout. Width (10 );
Cout. Fill ('&');
Cout. Precision (8 );
Cout <123 <"" <123.45678 <Endl;
Cout. SETF (IOs: Left );
Cout <123 <"" <123.45678 <Endl;
Cout. Width (10 );
Cout <123 <"" <123.45678 <Endl;
Cout <"x_width =" <cout. Width () <Endl;
Cout <"x_fill =" <cout. Fill () <Endl;
Cout <"x_precision =" <cout. Precision () <Endl;
}
Result:
X_width = 0
X_fill =
X_precision = 6
123 123.457
* ** X_width = 10, x_fill = &, x_pre
& 123 123.45678
123 123.45678
123 & 123.45678
X_width = 0
X_fill = &
X_precision = 8

6. Program questions.
1. Write a program to solve the root of the equation ax2 + bx + c = 0.
# Include <iostream>
# Include <cmath>
Using namespace STD;
Void main ()
{
Int A, B, C;
Float x1, x2, Z;
Cin> A> B> C;
Z = B * B-4 * a * C;
If (z> 0)
{
X1 = (-B) + SQRT (z)/(2 * );
X2 = (-B)-SQRT (z)/(2 * );
Cout <"the result: X1 =" <X1 <"X2 =" <X2 <Endl;
}
Else
If (Z = 0)
{
X1 =-B/(2 * );
Cout <"the result: X1 =" <X1 <Endl;
}
Else
Cout <"no result ";
}

2. Write a program to output all the daffodils. The number of daffodils refers to a three-digit number, and the cubes of each of them are equal to this number. Example: 153 = 13 + 53 + 33.
# Include <iostream>
Using namespace STD;
Void main ()
{
Int A, B, C;
For (INT I = 100; I <= 999; I ++)
{
A = I/100;
B = I % 100/10;
C = I % 10;
If (A * a * A + B * B * B + C * c = I)
Cout <I <Endl;
}
}

3. Write a program and calculate S = 1 + (1 + 2) + (1 + 2 + 3) +... + (1 + 2 + 3 +... + N.
# Include <iostream>
Using namespace STD;
Void main ()
{
Int N, S, sum = 0;
Cin> N;
For (INT I = 1; I <= N; I ++)
{
S = 0;
For (Int J = 1; j <= I; j ++)
S + = J;
Sum + = s;
}
Cout <sum <Endl;
}

4. A department store adopts the shopping discount method for promotion purposes.
(1) If the price is higher than 1000 yuan, the discount is off;
(2) If the price is higher than 2000 Yuan, a discount is offered;
(3) If the price is higher than 3000 yuan, a discount is offered;
(4) If you are more than 5000 yuan, you will be entitled to a discount.
Write a program, enter the number of shopping items, calculate and output the discount price. (Switch statements are required)
# Include <iostream>
Using namespace STD;
Void main ()
{
Int cost;
Double price, D;
Cin> cost;
If (Cost & gt; 5000)
D = 0.8;
Switch (Cost/1000)
{
Case 0: D = 1.0; break;
Case 1: D = 0.95; break;
Case 2: D = 0.9; break;
Case 3: Case 4: D = 0.85; break;
}
Price = Cost * D;
Cout <price <Endl;
}

4. Exchange a one-dollar banknote into one cent, two-cent, and five-cent coins. Assume that each item has at least one coin, calculate the total number of exchange methods, and print out various exchange methods.
# Include <iostream>
Using namespace STD;
Void main ()
{
Int A, B, C, Count = 0;
For (A = 1; A <100; A ++)
For (B = 1; B <100; B ++)
For (C = 1; C <100; C ++)
If (C * 5 + B * 2 + A = 100)
{
Count ++;
Cout <"1 point:" <A <", 2 points:" <B <", 5 points:" <C <Endl;
}
Cout <"counts:" <count <Endl;
}

6. "homogeneous number" refers to an integer that appears on the right side of the number of operators. For example, 376*376 = 141376. Find all "homogeneous numbers" within 10000 ".
# Include <iostream>
Using namespace STD;
Void main ()
{
For (INT I = 1; I <10000; I ++)
{
If (I <10 & I = I * I % 10)
Cout <I <Endl;
Else
If (I <100 & I = I * I % 100)
Cout <I <Endl;
Else
If (I <1000 & I = I * I % 1000)
Cout <I <Endl;
Else
If (I = I * I % 10000)
Cout <I <Endl;
}
}


Mate selection criteria:
Select Top 100 PPMM from girl where age between 20 and 24 and height between 155 and 165 and Area = 'shanghai' and wedlock = NULL and program in house order by beautiful DESC

Welcome to programming enthusiast's website
Http://www.programfan.com/

Below are the follow-up posts

# Include <iostream>
Using namespace STD;
Void main ()
{
Int N, S = 0, sum = 0;
Cin> N;
For (INT I = 1; I <= N; I ++)
{
S = S + I;
Sum + = s;
}
Cout <sum <Endl;
}

# Include <stdio. h>
Void main ()
{
Int I, N, S, sum, counter;
Sum = 0;
S = 0;
Counter = 1;
Printf ("/ninput a number! /N ");
Scanf ("% d", & N );
For (I = 1; I <= N; I ++)
{
S + = counter;
Sum + = s;
Counter ++;
}
Pritnf ("result is: % d/N", sum );
}
This program is implemented in C language,
A loop is missing from the answer,
It may be more efficient!

// 3. Write a program and calculate S = 1 + (1 + 2) + (1 + 2 + 3) +... + (1 + 2 + 3 +... + N) Value
// If anyone uses the formula |, ^, <, >>, & and so on, I really admire it!

# Include <iostream>
Using namespace STD;

Void main (){
Int N;
Cin> N;

Cout <"Result :"
<(N * (n + 1) * (N + 2)/6
<Endl;
}

// I think this should be the most efficient

 

I just learned C ++. Please give me some advice!

# Include <iostream. h>
Long fun (int)
{
S + =;
Sum + = s;
If (a = 1)
Returm sum;
Else
Returm fun (A-1); // is the call function correct?

}
Void main ()
{
Long fun (int)
Int X, S, sum;
S = 0;
Sum = 0;

Long y;
Cout <"enter an integer :";
Cin> X;
Y = fun (X );
Cout <"sum =" <Y;
Returm;
}

The younger brother just learned the C language and made some questions about Lo and VE, which is not good. But I want to focus on participation in everything, so I will stick it to it. Mo Xiao:
# Include <stdio. h>
Main ()
{
Int N, S = 0, j = 1, I;
Printf ("Please input num:/N ");
Scanf ("% d", & N );
For (I = N; I> = 0; I --)
{
S + = J * I;
J ++;
}
Printf ("% d", S );
}

 

Lo and VE

The method may be better than I did on the 6th floor! Please ask him to publish his answer.

// The following is a low-efficiency Recursive Implementation.
# Include <iostream>
Using namespace STD;

Long CAL (int n ){
Return (n = 1 )? 1: Cal (n-1) + N * (n + 1)/2;
}
Void main (){
Int I;
Cin> I;
Cout <"Result:" <CAL (I) <Endl;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.