判断题
1-1
在for(表达式1;表达式2;表达式3)中,如果表达式2为空,表示循环条件默认为真。
TRUE //可将for的第二个表达式的判断移至循环体内,借助break来结束循环。
1-2
for语句循环体可能一次也不执行。
TRUE//可能不满足循环条件
1-3
continue 只能用于循环体中。
TRUE
1-4
continue 不是结束本次循环,而是终止整个循环的执行。
FALSE
1-5
在多层循环中, 一个break语句只向外跳一层。
TRUE
1-6
运行包含以下代码段的程序将可能进入死循环。
int i = 1;
while(i>0){ i++; printf("%d ",i); }
FALSE //数据的溢出现象
1-7
do-while循环的while后的分号可以省略。
FALSE
1-8
do-while循环至少要执行一次循环语句。
TRUE
1-9
以下两个程序段等价,其功能是计算 s=1+2+...+10。
/* 程序段A*/
s = 0;
i = 1;
while(i <= 10){
s = s + i;
i++;
}
/* 程序段B */
s = 0;
i = 1;
while(1){
if(i > 10){
break;
}
s = s + i;
i++;
}
TRUE
1-10
C语言的三种循环不可以互相嵌套。
FALSE
单选题
BAADC,ACCCB
程序填空题
1-1
计算并输出一个非零整数序列(序列非空)中偶数的平均值。所有输入数据用空格隔开,用0结束输入。输出数据保留2位小数。
#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
求100-999之间的水仙花数之和(水仙花数 是指一个三位数的各位数字的立方和是这个数本身,如: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
用数组来求fibonacci数列问题,打印前20项数列,每行打印4个数。
#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
输入正整数n,计算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
语句填空:下列 for循环语句将输出: 0 1 2 0 1 2 0 1 2
for( i=1; i<=9; i++ ) printf("%2d", (i-1)%3);
(i-1)%3
编程题
1-1
本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印
*****
***
*
***
*****
所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。
给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。
输入格式:
输入在一行给出1个正整数N(≤1000)和一个符号,中间以空格分隔。
输出格式:
首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。
输入样例:
19 *
输出样例:
*****
***
*
***
*****
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
输入一个整数,输出每个数字对应的拼音。当整数为负数时,先输出fu
字。十个数字对应的拼音如下:
0: ling
1: yi
2: er
3: san
4: si
5: wu
6: liu
7: qi
8: ba
9: jiu
输入格式:
输入在一行中给出一个整数,如:1234
。
提示:整数包括负数、零和正数。
输出格式:
在一行中输出这个整数对应的拼音,每个数字的拼音之间用空格分开,行末没有最后的空格。如yi er san si
。
输入样例:
-600
输出样例:
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
给定两个整数A和B,输出从A到B的所有整数以及这些数的和。
输入格式:
输入在一行中给出2个整数A和B,其中−100≤A≤B≤100,其间以空格分隔。
输出格式:
首先顺序输出从A到B的所有整数,每5个数字占一行,每个数字占5个字符宽度,向右对齐。最后在一行中按Sum = X
的格式输出全部数字的和X
。
输入样例:
-3 8
输出样例:
-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
做作业的时候,邻座的小盆友问你:“五乘以七等于多少?”你应该不失礼貌地围笑着告诉他:“五十三。”本题就要求你,对任何一对给定的正整数,倒着输出它们的乘积。
输入格式:
输入在第一行给出两个不超过 1000 的正整数 A 和 B,其间以空格分隔。
输出格式:
在一行中倒着输出 A 和 B 的乘积。
输入样例:
5 7
输出样例:
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
Cassels方程是一个在数论界产生了巨大影响的不定方程:x2+y2+z2=3xyz。该方程有无穷多自然数解。
本题并不是要你求解这个方程,只是判断给定的一组 (x,y,z) 是不是这个方程的解。
输入格式:
输入在第一行给出一个不超过 10 的正整数 N,随后 N 行,每行给出 3 个正整数 0<x≤y≤z≤1000。
输出格式:
对于每一组输入,如果是一组解,就在一行中输出 Yes
,否则输出 No
。
输入样例:
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
本题要求编写程序,输出给定正整数M和N区间内的所有三位水仙花数。三位水仙花数,即其个位、十位、百位数字的立方和等于该数本身。
输入格式:
输入在一行中给出两个正整数M和N(100≤M≤N≤999)。
输出格式:
顺序输出M和N区间内所有三位水仙花数,每一行输出一个数。若该区间内没有三位水仙花数,则无输出。
如果M或者N不符合题目的要求,则输出Invalid Value.
。
输入样例1:
100 400
输出样例1:
153
370
371
输入样例2:
500 600
输出样例2:
输入样例3:
990 101
输出样例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
本题要求编写程序,输入N个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数。
输入格式:
输入在第一行中给出正整数N,第二行输入N个字符,最后一个回车表示输入结束,不算在内。
输出格式:
在一行内按照
letter = 英文字母个数, blank = 空格或回车个数, digit = 数字字符个数, other = 其他字符个数
的格式输出。请注意,等号的左右各有一个空格,逗号后有一个空格。
输入样例:
10
aZ &
09 Az
输出样例:
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;
}