# CPP2022-07-Circular Structure 01

True or False

1-1

In for( expression1;expression2;expression3), if expression2 is empty, the loop condition defaults to true.

TRUE //The judgment of the second expression of for can be moved to the body of the loop, and the loop can be ended with break.

1-2

The body of the for statement loop may not be executed once.

TRUE//The loop condition may not be met

1-3

continue can only be used within the body of a loop.

TRUE

1-4

continue does not end the current loop, but terminates the execution of the entire loop.

FALSE

1-5

In a multi-level loop, a break statement only jumps out one level.

TRUE

1-6

Running a program containing the following code segment will likely enter an infinite loop.

int i = 1；
while(i>0){ i++; printf("%d ",i); }

FALSE //Data overflow phenomenon

1-7

The semicolon after the while in the do-while loop can be omitted.

FALSE

1-8

A do-while loop executes the loop statement at least once.

TRUE

1-9

The following two blocks are equivalent and their function is to calculate s=1+2+...+10.

/* block A*/
s = 0;
i = 1;
while(i <= 10){
s = s + i;
i++;
}
/* block B */
s = 0;
i = 1;
while(1){
if(i > 10){
break;
}
s = s + i;
i++;
}

TRUE

1-10

The three loops in C language cannot be nested in each other.

FALSE

multiple choice

program fill-in-the-blank questions

1-1

Computes and outputs the mean of the even numbers in a sequence of nonzero integers (sequence is not empty). All input data are separated by spaces, and the input is terminated with 0. The output data has 2 decimal places.

#include <stdio.h>
int main()
{
int number;
int sum,n;
double average;
n = 0;
sum = 0;
scanf("%d",&number);

while(number != 0){
if(number%2 == 0){
sum += number;
n ++;
}
scanf("%d",&number);
}
average = sum*1.0/n;
printf("%.2lf\n",average);
return 0;
}

number != 0

scanf("%d",&number)

average = sum*1.0/n

1-2

Find the sum of the daffodil numbers between 100 and 999 (the daffodil number refers to the sum of the cubes of each digit of a three-digit number is the number itself, such as: 153=1^3+5^3+3^3). .

#include <stdio.h>
int main()
{
int d1,d2,d3;
int n;
int sum;
sum = 0;
for(
d1=1; d1<=9; d1++){
for(d2=0; d2<=9; d2++){
for(d3=0; d3<=9; d3++){
n = d1*100 + d2*10 + d3;
if(n == d1*d1*d1 + d2*d2*d2 + d3*d3*d3){
sum += n;
}
}
}
}
printf("%d\n",sum);
return 0;
}

d1=1; d1<=9; d1++

d2=0; d2<=9; d2++

n == d1*d1*d1 + d2*d2*d2 + d3*d3*d3

1-3

Use an array to find the fibonacci sequence problem, print the first 20 items of the sequence, and print 4 numbers per line.

#include<stdio.h>
int main()
{
int i;
int f[20]={1,1};
for (i=2;i<20;i++)
f[i]=f[i-1]+f[i-2];
for (int i=0;i<20;i++)
{if((i+1)%4==0)printf("\n");
printf("%d ",f[i]);
}
return 0;
}

f[i-1]+f[i-2]

int i=0

(i+1)%4==0

1-4

Enter a positive integer n and calculate the value of s = 1/1! + 1/2! + 1/3! + …+ 1/n!.

#include <stdio.h>
int main( void)
{
int j, k, n;
double f, s;

scanf("%d", &n);
s=0.0;
for (k=1; k<=n; k++){
f=1.0;
for(j=1; j<=k; j++)
f=f*j;
s=s+1.0/f;
}
printf("sum=%f\n", s);
return 0;
}

s=0.0

f=1.0

j<=k

f=f*j

1-5

Statement Fill in the Blanks: The following for loop statement will output: 0 1 2 0 1 2 0 1 2

for( i=1; i<=9; i++ ) printf("%2d", (i-1)%3);

(i-1)%3

programming questions

1-1

This problem asks you to write a program to print a given symbol in the shape of an hourglass. For example, given 17 "*", it is required to print in the following format

*****
***
*
***
*****

The so-called "hourglass shape" means that each line outputs an odd number of symbols; the centers of the symbols in each line are aligned; the number of symbols in two adjacent lines is 2; numbers are equal.

Given any N symbols, it is not always possible to form exactly an hourglass. The hourglass is required to be printed using as many symbols as possible.

### Input format:

Input gives 1 positive integer N (≤1000) and a symbol on one line, separated by spaces.

### Output format:

First print the largest hourglass shape consisting of the given symbols, and finally output the number of unused symbols on one line.

19 *

### Sample output:

*****
***
*
***
*****
2

#include<bits/stdc++.h>
using namespace std;
int main()
{
int i,j=1,x=0,y=0,rest,N;
char C;
cin>>N>>C;
while(2*j*j-1<=N)
{
j++;
}
j--;
y=2*j-1;
rest=N-2*j*j+1;
while(y>0)
{
for(i=0;i<x;i++)
cout<<" ";
for(i=0;i<y;i++)
cout<<C;
cout<<endl;
x++;
y-=2;
}
x--;
y+=2;
while(x>0)
{
x--;
y+=2;
for(i=0;i<x;i++)
cout<<" ";
for(i=0;i<y;i++)
cout<<C;
cout<<endl;
}
cout<<rest;
return 0;
}

1-2

Input an integer and output the pinyin corresponding to each number. When the integer is negative, the fu word is output first. The pinyin corresponding to the ten numbers is as follows:

0: ling
1: yi
2: er
3: san
4: si
5: wu
6: liu
7: qi
8: ba
9: jiu

### Input format:

Input gives an integer in one line, eg: 1234.

Tip: Integers include negative, zero, and positive numbers.

### Output format:

Output the pinyin corresponding to the integer in one line. The pinyin of each number is separated by a space, and there is no final space at the end of the line. like
yi er san si.

-600

### Sample output:

fu liu ling ling
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str[11]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu","fu"};
string a;
cin>>a;
char m=a.length();
for(int i=0;i<a.length();i++)
{
switch(a[i])
{
case('-'):
cout<<str[10];
break;
case('0'):
cout<<str[0];
break;
case('1'):
cout<<str[1];
break;
case('2'):
cout<<str[2];
break;
case('3'):
cout<<str[3];
break;
case('4'):
cout<<str[4];
break;
case('5'):
cout<<str[5];
break;
case('6'):
cout<<str[6];
break;
case('7'):
cout<<str[7];
break;
case('8'):
cout<<str[8];
break;
case('9'):
cout<<str[9];
break;
}
if(i!=a.length()-1)
cout<<' ';
}
return 0;
}

1-3

Given two integers A and B, output all integers from A to B and the sum of these numbers.

### Input format:

The input gives 2 integers A and B in one line, where −100≤A≤B≤100, separated by spaces.

### Output format:

First, output all integers from A to B sequentially, each 5 numbers occupy a line, each number occupies 5 characters width, right-aligned. Finally, output the sum X of all numbers in one line in the format Sum = X.

-3 8

### Sample output:

-3   -2   -1    0    1
2    3    4    5    6
7    8
Sum = 30

#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c=0,sum=0;
cin>>a>>b;
for(int i=a;i<=b;i++)
{
if(a>b)
{
break;
}
c++;
sum=sum+i;
cout<<setw(5)<<setfill(' ')<<i;
if(c%5==0)
{
cout<<endl;
}
else if(i==b)
{
cout<<endl;
}
}
cout<<"Sum = "<<sum<<endl;
return 0;
}

1-4

When you are doing your homework, the little friend next to you asks you, "What is five times seven?" You should politely smile and tell him, "Fifty-three." Given positive integers, output their product in reverse.

### Input format:

Input gives two positive integers A and B up to 1000 on the first line, separated by spaces.

### Output format:

Print the product of A and B backwards in a row.

5 7

### Sample output:

53

#include<bits/stdc++.h>
using namespace std;
int main()
{
int A,B;
int s[100];
cin>>A>>B;
int C=A*B;
int k=0,flag=0;
while(C>0)
{
s[++k]=C%10;
C=C/10;
}
for(int i=1;i<=k;i++)
{
if(s[i]||flag)
{
cout<<s[i];
flag=1;
}

}

return 0;
}

1-5

The Cassels equation is an indeterminate equation that has had a huge impact on the number theory community: x2+y2+z2=3xyz. This equation has infinitely many natural number solutions.

This question does not ask you to solve this equation, but to determine whether a given set of (x,y,z) is a solution to this equation.

### Input format:

The input gives a positive integer N up to 10 on the first line, followed by N lines, each giving 3 positive integers 0<x≤y≤z≤1000.

### Output format:

For each set of inputs, if it is a set of solutions, output Yes in one line, otherwise output No.

### Input sample:

2
1 1 1
5 6 7

#include<bits/stdc++.h>
using namespace std;
int main()
{
int length, x, y, z, i;
cin>>length;

if (length > 0)
{
for (i = 1; i <= length; i++)
{
cin>>x>>y>>z;
if (x*x + y * y + z * z == 3 * x*y*z)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}

}
return 0;
}

7-6

This problem requires writing a program that outputs all three-digit daffodil numbers in the interval M and N given positive integers. A three-digit daffodil number, that is, the sum of the cubes of its ones, tens, and hundreds digits is equal to the number itself.

### Input format:

The input gives two positive integers M and N in one line (100≤M≤N≤999).

### Output format:

Sequentially output all three-digit daffodil numbers in the M and N intervals, one number per line. If there are no three-digit daffodils in the interval, there is no output.

If M or N does not meet the requirements of the title, Invalid Value. is output.

100 400

153
370
371

500 600

990 101

### Sample output 3:

Invalid Value.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int m,n,x,a,b,c;
cin>>m>>n;
x=m;

if(n>=m&&m>=100&&n<=999){
for(x=m;x<=n;x++)
{

a=x/100;
b=x/10%10;
c=x%10;

if(x==pow(c,3)+pow(b,3)+pow(a,3))
{
cout<<x<<endl;
}
}
}
else
{cout<<"Invalid Value.";}
return 0;
}

1-7

This question requires to write a program, input N characters, and count the number of English letters, spaces or carriage returns, numeric characters and other characters.

### Input format:

The input gives a positive integer N in the first line, N characters in the second line, and the last carriage return indicates the end of the input, not counting.

### Output format:

in a line according to

letter = Number of English letters, blank = Number of spaces or carriage returns, digit = Number of numeric characters, other = other characters

format output. Note that there is a space to the left and right of the equal sign, and a space after the comma.

10
aZ &
09 Az

### Sample output:

letter = 4, blank = 3, digit = 2, other = 1
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,b=0,c=0,d=0,e=0;
cin>>n;
char a;
for(int i=0;i<=n;i++)
{
a=getchar();
if((a<='z'&&a>='a')||(a<='Z'&&a>='A'))
{
b++;
}
else if(a==' '||a=='\n')
{
c++;
}
else if(a>='0'&&a<='9')
{
d++;
}
else
{
e++;
}
}
cout<<"letter = "<<b<<", blank = "<<c-1<<", digit = "<<d<<", other = "<<e;
return 0;
}

Posted by crazycaddy on Tue, 18 Oct 2022 03:22:08 +0530