C语言期末考试编程题
数组
#include
void main()
{ int i;
int f[20]={1,1};
for(i=2;i<=19;i++)
f[i]=f[i-2]+f[i-1];
for(i=0;i<=19;i++)
{ if(i%5==0) cout<<endl;
cout<<f[i];
}
}
指针的地址传递
swap(int *p1, int *p2)
{ int p;
p=*p1;
*p1=*p2;
*p2=p;
}
void main()
{ int a,b;
int *pointer_1,*pointer_2;
cin>>a>>b;
pointer_1=&a; pointer_2=&b;
if(a<b) swap(pointer_1,pointer_2);
cout<<a<<b;
例2:输出已知一维数组中的5个整型数
(数组表示形式)
#include
void main( )
{
int a[5]={1,2,3,4,5},i;
for (i=0;i<5;i++)
cout<<a[i];
设计一个函数可以计算一维数组中所有元素之和
#include
int sum(int *p,int n)
{
int i,s=0;
for(i=0;i<n;i++,p++)
s+=*p;
return s;
}
void main()
{
int y,score[10]={90,82,88,78,100,95,87,72,91,85};
y=sum(score,10);
cout<<"总成绩为:"<<y<<endl;
}
#include main ( ) { int grade; float score;
cout<<“请输入成绩:”;
cin>>score;
grade=(int)score/10;
switch(grade)
{ case 10:
case 9:cout<<“优”;break;
case 8: cout<<“良”;break;
case 7: cout<<“中”;break;
case 6: cout<<“及格”;break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0::cout<<“不及格”;break;
default: cout<<“输入错误!”;
}
}计算并输出1~10000这10000个数的平方
#include
using namespace std;
int main()
{
int i=1;
while(i<=10000)
{
cout<<i*i;
}
return 0;
}
For yu ju
#include
using namespace std;int main ( ){
int i,sum;
sum=0; for ( i=1; i<=10; i++) sum=sum+i; cout<<“1+2+3+…+10=”<<sum; return 0;
}
#include
using namespace std;int main ( ){
int r=1;
float area;
while(1) { area=3.14159rr; if (area>100) break; cout<<"r=“<<r<<“, area=“<<area; r=r+1;
} return 0;
}
杨辉三角
#include
void main(){
int a[7][7]={0},i,j;
for(i=0;i<7;i++){
for(j=0;j<7-i;j++)
printf(" ");
for(j=0;j<i+1;j++){
if(j==0||j==i) printf("%4d",1);
else {a[i][7-i]=a[i-1][7-i]+a[i-1][7-i+1]+2;
printf("%4d",a[i][7-i]);}
}
printf("
");
}
}
汉诺塔
#include
void hanoi(int n,char no1,char no2,char no3)
{
if (n==1)
cout<<no1<<"移动到"<<no3<<endl;
else
{
hanoi(n-1,no1,no3,no2);
cout<<no1<<"移动到"<<no3<<endl;
hanoi(n-1,no2,no1,no3);
}
}
void main()
{
int m;
cout<<"请输入A柱上的金盘子总数:";
cin>>m;
cout<<"当有"<<m<<"个金盘子时,移动步骤依次为:"<<endl;
hanoi(m,'A','B','C');
}
连乘
#include
void main()
{
int i,n,t=1;
cin>>n;
if (n<0)
cout<<“输入有误!
";
else
for(i=1;i<=n;i++)
t=t*i;
cout<<n<<“!=”<<t<<endl;
数组
#include
void main ( )
{
int a[2][3]={{1,2,3},{4,5,6}},b[2][3] ={{7,8,9},{10,11,12}};
int c[2][3],i,j;
for (i=0;i<=1;i++)
for (j=0;j<=2;j++)
c[i][j]=b[i][j]+a[i][j];
cout<<"数组c:"<<endl;
for (i=0;i<=1;i++)
{
for (j=0;j<=2;j++)
cout<<" "<<c[i][j];
cout<<endl;
}
}
输出***1
#include
using namespace std;
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=3;j++)
cout<< "*";
cout<<endl;
}
return 0;
}
*****2
#include
using namespace std;
int main()
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=1;j<=5-i;j++)
cout<<" ";
for(k=1;k<=2*i-1;k++)
cout<< "*";
cout<<endl;
}
return 0;
}
九九乘法表
#include
void main()
{
int i,j;
for(i=1;i<=9;i++)
{
for(j=1;j<=i;j++)
cout<<i<<"x"<<j<<"="<<i*j<<" ";
cout<<endl;
斐波那契数列
1)#include
using namespace std;
int main()
{
long int f1, f2,f3,f4,f5,f6,f7,f8,f9,f10;
f1=1; f2=1;
f3=f1+f2;
f4=f2+f3;
f5=f3+f4;
f6=f4+f5;
f7=f5+f6;
f8=f6+f7;
f9=f7+f8;
f10=f8+f9;
cout<<f1<<" "<<f2<<" "<<f3<<" "<<f4<<" "<<f5<<" "<<f6<<" "<<f7<<" "<<f8<<" "<<f9<<" "<<f10;
return 0;
2)#include
using namespace std;
int main()
{
long int f1, f2;
int i;
f1=1; f2=1;
for (i=1; i<=5; i++)
{
cout<<" "<<f1<<" "<<f2;
f1=f1+f2;
f2=f2+f1;
}
return 0;
}
数组
#include
void main()
{ int i;
int f[20]={1,1};
for(i=2;i<=19;i++)
f[i]=f[i-2]+f[i-1];
for(i=0;i<=19;i++)
{ if(i%5==0) cout<<endl;
cout<<f[i];
}
}
指针的地址传递
swap(int *p1, int *p2)
{ int p;
p=*p1;
*p1=*p2;
*p2=p;
}
void main()
{ int a,b;
int *pointer_1,*pointer_2;
cin>>a>>b;
pointer_1=&a; pointer_2=&b;
if(a<b) swap(pointer_1,pointer_2);
cout<<a<<b;
例2:输出已知一维数组中的5个整型数
(数组表示形式)
#include
void main( )
{
int a[5]={1,2,3,4,5},i;
for (i=0;i<5;i++)
cout<<a[i];
设计一个函数可以计算一维数组中所有元素之和
#include
int sum(int *p,int n)
{
int i,s=0;
for(i=0;i<n;i++,p++)
s+=*p;
return s;
}
void main()
{
int y,score[10]={90,82,88,78,100,95,87,72,91,85};
y=sum(score,10);
cout<<"总成绩为:"<<y<<endl;
}
#include main ( ) { int grade; float score;
cout<<“请输入成绩:”;
cin>>score;
grade=(int)score/10;
switch(grade)
{ case 10:
case 9:cout<<“优”;break;
case 8: cout<<“良”;break;
case 7: cout<<“中”;break;
case 6: cout<<“及格”;break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0::cout<<“不及格”;break;
default: cout<<“输入错误!”;
}
}计算并输出1~10000这10000个数的平方
#include
using namespace std;
int main()
{
int i=1;
while(i<=10000)
{
cout<<i*i;
}
return 0;
}
For yu ju
#include
using namespace std;int main ( ){
int i,sum;
sum=0; for ( i=1; i<=10; i++) sum=sum+i; cout<<“1+2+3+…+10=”<<sum; return 0;
}
#include
using namespace std;int main ( ){
int r=1;
float area;
while(1) { area=3.14159rr; if (area>100) break; cout<<"r=“<<r<<“, area=“<<area; r=r+1;
} return 0;
}
循环体没有被执行 程序直接被结束
for中间那个表达式是用来判断的
而程序中仅仅用了一个赋值表达式而不是条件表达式
(注意 n=0 和 n==0 是有本质区别的)
赋值表达式的值就是被赋值的那个值 在本题中恰好为0
而0和false是同义的 因此for语句中的条件直接被认为不成立
因此就没有执行
要求: 1)输出结果时说明平方和是大于100还是小于100( >100或<100 )
#include<stdio.h>
int main(){
int a, b, c;
scanf("%d%d", &a, &b);
c = a * a + b * b;
if(c > 100) printf("平方和大于100\n%d\n", c%100);
else printf("平方和小于等于100\n%d\n", a + b);
return 0;
}
2输入一个整数,判断是否是5和7的公倍数,若是则输出:5and7yes,否则再判断是否是3的倍数,若是3的倍数输出:3yes,若都不是则输出:no
#include<stdio.h>
int main(){
int input;
scanf("%d", &input);
if(input % 5 == 0 && input % 7 == 0)printf("5and7yes\n");
else if(input % 3 == 0) printf("3yes\n");
else printf("no\n");
return 0;
}
第三,四题和第二题差不多
5计算公式: [ 2*x x=2
y= [ x*x+1 x<2
[ 2*x*x+3*x+1 x>2
要求: 1)从键盘输入x的值,根据x的值求y的值
2)输出y的值
#include<stdio.h>
int main(){
int x, y;
scanf("%d", &x);
if(x == 2) y = 2*x;
else if(x < 2) y = x*x+1;
else y = 2 * x * x + 3 * x + 1;
printf("%d\n", y);
return 0;
}
8 和7差不多
下面原理都差不多 用if基本都能搞定 判断条件就行了 应该LZ能搞定了 东西有点多 -。- 就写这些吧 其他的就不一一写了^ ^
1、
#include <stdio.h>
main()
{
int a, b, square;
scanf (“%d%d”, &a, &b);
square = a * a + b * b;
if (square > 100) \*判断a、b的平方和是否大于100 *、
{
printf (“their square is bigger than 100\n”);
printf (“the digitale bigger than 100 is :%d”, square / 100);
}
else
{
printf (“their square is smaller than 100\n”);
printf (“their addtion is: %d”, a + b);
}
}
2、
#include <stdio.h>
main()
{
int n;
if ((n % 5 == 0) && (n % 7 == 0)) \* 判断n是否为5和7的公倍数 *\
{
printf (“5 and 7 yes”);
}
else
{
if (n % 3 == 0) \* 判断是否能被3整除 *\
printf (“ 3 yes”);
else
printf (“no”);
}
}
3、(3题和平共处题与第2题相似的,只要把条件改一下就可以了)
#include <stdio.h>
main()
{
int n;
if ((n % 3 == 0) && (n % 5 == 0))
{
printf (“3 and 5 yes”);
}
else
{
if (n % 7 == 0)
printf (“ 7 yes”);
else
printf (“no”);
}
}
4、
#include <stdio.h>
main()
{
int n;
if ((n % 2 == 0) && (n % 3 == 0))
{
printf (“2&3 yes”);
}
else
{
if (n % 7 == 0)
printf (“ 3 yes”);
else
printf (“no”);
}
}
5、
#include <stdio.h>
main()
{
int x, y;
printf ("x =");
scanf ("%d", &x);
if (x == 2) \* 用一个多分支语句将几种情况分开计算*\
y = 2 * x;
else if (x < 2)
y = x * x + 1;
else
y = 2 * x * x + 3 * x + 1;
printf ("y = %d", y);
}
6、(6 题和7题还有8题都与5题相似)
#include <stdio.h>
main()
{
int x, y;
printf ("x =");
scanf ("%d", &x);
if (x == 1)
y = 1;
else if (x < 1)
y = x * x;
else
y = x * x * x;
printf ("y = %d", y);
}
7、
#include <stdio.h>
main()
{
int x, y;
printf ("x =");
scanf ("%d", &x);
if (x < 0)
y = x + 1;
else if (x <= 2)
y = x * x + 2;
else
y = x * x * x + 3;
printf ("y = %d", y);
}
8、
#include <stdio.h>
main()
{
int x, y;
printf ("x =");
scanf ("%d", &x);
if (x < 1)
y = x;
else if (x <= 10)
y =2 * x - 1;
else
y = 3 * x - 11;
printf ("y = %d", y);
}
9、
#include <stdio.h>
main()
{
float score;
char grade;
printf ("please enter the score:");
scanf ("%f", &score);
if (score > 100 || score < 0) \*判断成绩是否输入正确*\
{
printf ("enter error!");
}
if (score >= 90)
grade = 'A';
else if (score >= 80)
\*在这里的else if 语句条件中已经排除了在于等于90的情况,即此处等同于"score >= 80 && score < 90"*\
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'E';
printf ("the grade is :%c", grade);
}
10、
#include <stdio.h>
main()
{
int n, price;
printf ("please enter quantity:");
scanf ("%d", &n);
if (n <= 10)
price = 60;
else if (n < 40) \* 此处方法与上题相同*\
price = 50;
else
price = 45;
printf ("the total money is :%d", price * n);
}
11、
#include <stdio.h>
#include <math.h>
main()
{
int a, b,final;
printf ("enter a,b:");
scanf ("%d%d", &a, &b);
if (a % b == 0)
final = a * a + b * b;
else if ( b % a == 0)
final = a * a * a + b * b * b;
else
final = abs(a - b);
printf ("final = %d", final);
}
12、
#include <stdio.h>
#include <math.h>
main()
{
float a, b, f;
printf ("enter a,b:");
scanf ("%f%f", &a, &b);
if (a > b)
f = fabs(a - b);
else if (a < b)
f = a * b;
else
f = ((int)a % 10) * ((int)b % 10); \*用强制转换将a、b转换成整数再除10求余即得个位数字*\
printf ("f = %.2f", f);
}
13、
#include <stdio.h>
#include <math.h>
main()
{
int a, b, c, disc;
int x1, x2,x;
printf ("a,b,c=");
scanf ("%d%d%d", &a, &b, &c);
if (a == 0)
{
printf ("this is not a equation");
}
else
{
disc = b * b - 4 * a * c;
if ( disc > 0)
{
x1 = (- b + sqrt(disc)) / (2 * a);
x2 = - b - sqrt(disc) / (2 * a);
printf ("there are two deferent root:%d %d", x1, x2);
}
else
{
x = - b / ( 2 * a);
printf ("there are two same root:%d", x);
}
}
}
14、
#include <stdio.h>
main()
{
int n;
int n1, n2, n3;
printf ("please enter a number:");
scanf ("%d", &n);
if (n >= 100 && n <= 999)
{
n1 = n / 10;\*取出n的个、十、百各位数*\
n2 = (n - n1* 10) / 10;
n3 = n % 10;
if (n1 * n1 * n1 + n2 * n2 * n2 + n3 * n3 * n3 == n)\* 判断个、十、百位平方和是否等于n *\
printf ("the number is shuixianhua digitale");
else
printf ("the number is not shuixianhua digitale");
}
else
printf ("the number is not a 3 bit number");
}
15、
#include <stdio.h>
main()
{
enum workday {monday = 1, tuesday, wednesday, thursday, friday, saturday, sunday};
enum workday workdays;
int n;
printf ("please enter a days:");
scanf ("%1d", &n);
if (n >= 1 && n <= 7)
{
workdays = (enum workday)n;
switch(workdays)
{
case 1: printf ("moday"); break;
case 2: printf ("tuesday"); break;
case 3: printf ("wednesday"); break;
case 4: printf ("thursday"); break;
case 5: printf ("friday"); break;
case 6: printf ("saturday"); break;
case 7: printf ("sunday"); break;
}
}
else
printf ("enter error");
}
16、
#include <stdio.h>
main()
{
int i, n;
float t, s = 0; \* 用t 产生各项,s 为各项之和 *\
printf ("n = ");
scanf ("%d", &n);
for (i = 1; i <= n; i++)
{
t = 1.0 / n; \* 因为n 是整数,在1后面加小数使得结果不至于为零*\
s = s + t;
n = n + 2;
}
printf ("the addtion is :%f", s);
}
这么简单的程序自己怎么不写啊,这是多么基础的啊!
30分就帮你编16个程序!?你在做梦吧?!
C语言的编程题!
1.从键盘上输入若干字符,直到输入’#”结束。统计英文字母、数字、空格、其他字符的个数并输出。include "stdio.h"main(){ char c;int letter,number,space,other;letter=number=space=other=0;printf("\\nPlease input some characters:(end of '#')\\n");while((c=getchar())!='#'){ ...
C语言编程题2道!急!!!
1:include<stdio.h> int main(){ int a[10];int i;int max = 0;int min = 100;int maxi=0;int mini =0;for(i=0;i<10;i++){ scanf("%d",&a[i]);if(a[i]<min){ min = a[i];mini = i;} if(a[i]>max){ max = a[i];maxi = i;} } int sum =0;for(i=0...
一道C语言编程题
程序:\/ 题目:某班有50位学生,编程统计该班C语言期末考试成绩,找出最高分,最低分,计算及格率。输出全部成绩,并将最高分放置第一个位置,最后输出该班的及格率。要求:从键盘输入成绩,数组,函数调用,指针。请哪位高手帮我解答一下,急用。非常感谢!\/ include<stdio.h> int findmax(int *...
C语言期末考试编程题
第三,四题和第二题差不多 5计算公式: [ 2*x x=2 y= [ x*x+1 x<2 [ 2*x*x+3*x+1 x>2 要求: 1)从键盘输入x的值,根据x的值求y的值 2)输出y的值 include<stdio.h> int main(){ int x, y;scanf("%d", &x);if(x == 2) y = 2*x;else if(x <...
求C语言大神帮忙编程,考试题目。
第一道题:include "stdio.h"define N 20 int main(void){ int age[N];int d[11]={0};int i=0;for(;i<20;i++){ scanf("%d",&age[i]);} for(i=0;i<20;i++){ switch(age[i]\/10){ case 0:d[0]++;break;case 1:d[1]++;break;case 2:d[2]++;break;case 3:d[...
C语言编程题
\/\/输入15个学生的信息void insertTest(STU *stu);\/\/测试,用随机数代替输入,我懒得输入那么多成绩,你要手输,调用insertSTU替换该函数void printfSTU(STU *stu);\/\/按输入顺序打印15名学生7模块成绩 及 每个模块前三名的成绩int main(){ STU stu[stuNum];\/\/定义15个学生 \/\/ int i,...
求帮忙~计算机C语言的编程题!大学选的辅修课没去过,要考试了不会呀...
楼主 我来拯救你~~~ 10道题全部奉上~~!!!有些题别人已经给出了,我引用一下啊(懒得重写了),大家不要喷我哦~~~第一题 include<stdio.h> int main(){ int d1,d2,d3,n=253;d1=n%10;d2=n\/10%10;d3=n\/100%10;printf("253的:\\n个位数为%d\\n十位数为%d\\n百位数为%d\\n...
C语言编程题:编写一个程序,求出200到300之间的数,且满足条件:它们三个...
main(){ int i;int a,b,c;printf("符合条件的数为:");for(i=200;i<=300;i++){ a=i%10;b=(i\/10)%10;c=i\/100;if(a*b*c==42&&a+b+c==12)printf("%d\\t",i);} }
请教几道C语言编程题,急用
1.include <stdio.h> main(){ int x,y;printf("please input x :");scanf("%d",&x);\/*下面三句可以用这一句代替: y=(x<0)?(x-1):(x=0?0:1);*\/ if(x<0) y=x-1;else if(x==0) y=0;else y=1;printf("the value of y=%d",y);getch();} 2.include <stdio.h...
大一期末考试复习题,求教C语言编程高手
2、A吧。这个不太确定。c语言中不允许出现A=B=C形式的赋值。C中没有**这个东西,D中,强制转换为y=(float)i;3、B 所谓保留字,一般是指C语言留作关键字了,比如int,我们不能定义一个变量名叫int的。4、8 参照题目一,依次计算各个表达式,取最后一个表达式的值,结果为8。参考资料:me ...
仝虏骨痛:[答案] #include#includeint main(){ /* FILE *fp; fp=fopen("C:\\1.txt","r"); */ char ch; int wordnum[10]={0}; int i,isword=0,isbegin=0,wordsize=0; int wordcount=0; //ch=fgetc(fp); ch=getchar(); while(1){ //printf("%c",ch); if(ch==' '||ch=='\t'||ch=='\n'||ch==','||ch=='.'||...
克拉玛依市19819752938: 大一期末考试复习题,求教C语言编程高手1.设int a=2,b=6,c=8;,表达式(a,b),(c,10)的值是( )A.2 B.6C.8 D.102设int i=8,a,b;double x=1.42,y=5.2;,正确... - ?
仝虏骨痛:[答案] 1、10 逗号(,)分隔开的连续表达式,取最后一个表达式的值为整个表达式的值.2、A吧.这个不太确定.c语言中不允许出现A=B=C形式的赋值.C中没有**这个东西,D中,强制转换为y=(float)i;3、B 所谓保留字,一般是指C语言留...
克拉玛依市19819752938: c语言期末考试题 - ?
仝虏骨痛: #include "stdio.h"#define N 30void swap(int &a, int &b){ if(a!=b) { a = a^b; b = a^b; a = a^b; }}void sort(int a[],int low, int high){ int i = low; int j = high,midvalue...
克拉玛依市19819752938: C语言期末测试题目,求高手帮忙写写 - ?
仝虏骨痛: #include <stdio.h> void main() { int n; scanf("%d",&n); if (n%5==0 || n%11==0) printf("YES\n"); else printf("NO\n"); } #include <stdio.h> void main() { int a,max,min; max=-1; min=9999999; while(1) { scanf("%d",&a); if (a<0) break; if (min>a)...
克拉玛依市19819752938: c语言期末考试题 - ?
仝虏骨痛: 1、 有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 2、 题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成...
克拉玛依市19819752938: C语言、、期末考试的题啊 求正解!?
仝虏骨痛: #include<stdio.h> void main() { float sal=500; int profit; printf("请输入利润值:"); scanf("%d",&profit); if(profit>1000&&profit<=2000) sal = sal+profit*0.1; if(profit>2000&&profit<=5000) sal = sal+profit*...
克拉玛依市19819752938: 求一张C语言的期末考试卷,我是大一的新生,想看看试卷的大概格式和内容.?
仝虏骨痛: 本试卷共8页,共5大题,考生在作答前应先检查是否有缺页、白页,以防漏答.答题前,请先填写学号、姓名、专业及班级. 一、单项选择题(共10题,每题2分,共20分.在备选答案中选择一个最佳答案.多选、错选、不选不得分) 1、不是...
克拉玛依市19819752938: C语言的编程题?
仝虏骨痛: 1.main() { int x,y; printf("Input x:"); scanf("%d",&x); if(x<=1) y=x; else if(x>=10) y=3*x-11; else y=2*x-1; printf("%d",y); getch(); } 2.main() { int a[10],b[10],i; printf("Input 10 int numbers\n"); for(i=0;i<10;i++) { scanf("%d",&a[i]); b[9-i]=a[i]; } for(...
克拉玛依市19819752938: C语言程序设计试题 - ?
仝虏骨痛: 1. x>0 x>=02. x=y; y=temp;3. m%i==0 i>k4. n/100 i*i*i+j*j*j+k*k*k4. c=...
克拉玛依市19819752938: C语言编程题?
仝虏骨痛: while(true) { temp=a; a=x; x=a; temp=a; a=x; x=a; }