四川大学C语言题库_第1页
四川大学C语言题库_第2页
四川大学C语言题库_第3页
四川大学C语言题库_第4页
四川大学C语言题库_第5页
已阅读5页,还剩361页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、输入十进制数,输出该数的十六进制数。#include <stdio.h>int main(void)int n;printf("input:");scanf("%d", &n);printf("output:%Xn", n);return 0;输入实型数据a,b,然后输出a、b的值。#include <stdio.h>int main(void)double number1, number2;printf("please input two numbers:");scanf(&quo

2、t;%lf,%lf", &number1, &number2);printf("Output:n");printf("a=%f,b=%fn", number1, number2);return 0;输入一个小写英文字母,首先输出它及其ASCII码,然后输出其对应的大写字母及其ASCII码int main(void)char ch;printf("Input a lowercase letter:");ch = getchar();printf("Output:n");printf(&quo

3、t;%c(%d)n", ch, ch);printf("%c(%d)n", ch - 32, ch - 32);return 0;从键盘输入一个正三角形的边长(整型),计算该三角形的面积和周长#include <stdio.h>int main(void)int side;printf("Input a side of triangle: ");scanf("%d", &side);printf("Output:n");printf("The area of triangle

4、 is %.2f, the circle of triangle is %.0f.n", 1.0 * side * side * sqrt(3) / 4, 3.0 * side);return 0;从键盘读入一个任意字符,输出该字符ASCII的十六进制小写值#include <stdio.h>int main(void)char ch;printf("Input a character: ");ch = getchar();printf("Output:%xn", ch);return 0;从键盘输入一个正方体的边长(整型),计算该

5、正方体的体积和表面积#include <stdio.h>int main(void)int side;printf("Input a side of cube: ");scanf("%d", &side);printf("Output:nThe volume of cube is %d, the surface area of cube is %d.n", side * side * side, 6 * side * side);return 0;#include <stdio.h>从键盘输入X,Y,Z

6、的值#include <stdio.h>int main(void)int float_z;double float_x, float_y;printf("please input x,y,z:");scanf("%lf,%lf,%d", &float_x, &float_y, &float_z);printf("Output:");printf("%fn", float_x + float_z % 3 * (int)(float_x + float_y) % 2 / 4);re

7、turn 0;从键盘输入一日期,年月日之间以“-”分隔,并以同样的形式但以“/”作分隔符输出。#include <stdio.h>int main(void)int year, month, day;printf("please input a data:");scanf("%d-%d-%d", &year, &month, &day);printf("Output:n");printf("the date is:%d/%d/%dn", year, month, day);ret

8、urn 0;从键盘上输入一个四位整数,计算各个位上的数字之和。#include <stdio.h>int main(void)int number;printf("Input a number with 4-digit: ");scanf("%d", &number);printf("Output:n");printf("sum=%dn", number / 1000 + number / 100 % 10 + number / 10 % 10 + number % 10);return 0;已

9、知某产品单价是30 ,输入其数量是num,输出其总价。#include <stdio.h>int main(void)int num;printf("please input num:");scanf("%d", &num);printf("Output:ntotal=%dn", 30 * num);return 0;输入两个整数,输出这两个整数的和#include <stdio.h>int main(void)int number1, number2;printf("please input

10、 data: ");scanf("%d%d", &number1, &number2);printf("Output:n");printf("%4d+%4d=%4dn", number1, number2, number1 + number2);return 0;用scanf输入圆半径r,圆柱高h,求圆周长C1(2r)、圆面积S(r2)、圆柱体积V(r2h)。#include <stdio.h>int main(void)double rad, high, c1, s, ver;printf(&q

11、uot;Input: ");scanf("r=%lf,h=%lf", &rad, &high);c1 = 2 * rad * 3.14;s = 3.14 * rad * rad;ver = s * high;printf("Output:n");printf("C1=%.2fn", c1);printf("S=%.2fn", s);printf("V=%.2fn", ver);return 0;输入一个华氏温度,要求输出摄氏温度,计算公式为c=5(F-32)/9<

12、b style="float:right;color:red">#include <stdio.h>int main(void)double cel, fra;printf("Input the degree:");scanf("%lf", &fra);cel = 5 * (fra - 32) / 9;printf("Output:n");printf("F(%.2f)=C(%.2f)n", fra, cel);return 0;2151发奖金#include <

13、stdio.h>int LastRemaining_Solution2(int n, unsigned int m)int lastinteger=0;int i = 2 ;if(n <= 0 | m < 0)return -1;for (i = 2; i <= n; i +) lastinteger = (lastinteger + m) % i;return lastinteger;int main() int a,b;printf("the number of people:");scanf("%d",&a);pri

14、ntf("the lucky number is:");scanf("%d",&b);printf("Output:nthe lucky one's number is:"); while(a&&b)!=EOF)printf("%dn",LastRemaining_Solution2(a,b);break; return 0;数列求和#include<stdio.h>int main() float sum,term,deno,sign,n;printf("Pl

15、ease input n:n");scanf("%f",&n);sum=1;deno=2;sign=1;printf("Output:n");if(n<=0)printf("Input error!n");return 0;while(deno<=n)sign=sign*(-1);term=sign*(1/deno);sum=sum+term;deno+;printf("the ans is %.2fn",sum); return 0; 2139汉诺塔3#include<stdi

16、o.h>long f(int n) if(n=1) return 2; return f(n-1)*3+2;int main() int n;printf("Please input n:n");scanf("%d",&n);printf("Output:nthe times are:n"); while(n!=EOF)printf("%lldn",f(n);break; return 0;2803晨跑#include <stdio.h>#include <string.h>#

17、define inf 1000000000int n, l, g1616, deg16, odd16, flag16;int dfs() int i, j, t, r = inf; for (i = 0; i < l; i+) if (!flagi) break; if (i = l) return 0; flagi = 1; for (j = i + 1; j < l; j+) if (!flagj) flagj = 1; t = dfs() + goddioddj; r = r < t ? r : t; flagj = 0; flagi = 0; return r;int

18、 main() int m, i, j, k, s, e, v, d;printf("请输入花坛的个数:n");scanf("%d", &n); while (n!= EOF, n)printf("请输入小路的个数:n"); scanf("%d", &m); for (i = 1; i <= n; i+) for (j = 1; j <= n; j+) gij = inf; memset(deg, 0, sizeof(deg);printf("输入各小路的关系:n")

19、; for (d = i = 0; i < m; i+) scanf("%d%d%d", &s, &e, &v); degs+; dege+; d += v; if (v < gse) gse = ges = v; for (k = 1; k <= n; k+) for (i = 1; i <= n; i+) for (j = 1; j <= n; j+) if (gik + gkj < gij) gij = gik + gkj; for (l = 0, i = 1; i <= n; i+) if (degi

20、 % 2) oddl+ = i; memset(flag, 0, sizeof(flag);printf("Output:n");printf("最短路程为:n"); printf("%dn", d + dfs();break; return 0;迷障#include<stdio.h>#include<stdlib.h>int cmp(const void *a,const void *b) return *(int*)a)-*(int*)b);int main() int i,n,v,w,cnt; doubl

21、e now; int as109,cas;printf("测试数据的组数:n"); scanf("%d",&cas); while( cas- )printf("万能药水的种数和体积:n"); scanf("%d%d",&n,&v);printf("解药的最大浓度:n");scanf("%d",&w); now=0; cnt=0; for( i=0; i<n; i+ )printf("第%d种药水的浓度:n",i+1)

22、; scanf("%d",as+i); qsort(as,n,sizeof(as0),cmp); for( i=0; i<n; i+ ) if( now+asi > (cnt+1)*w ) break; else now+=asi,cnt+; if( cnt ) now=now/cnt/100;printf("Output:n");printf("解药的最大体积解药的浓度:n"); printf("%d %.2lfn",cnt*v,now);printf("n"); return 0

23、;2352求上网#include <stdio.h>int main() int n, i; double x100, y100, r100, a, b;printf("请输入WIFI热点的个数n:n");scanf("%d", &n); while (n!= EOF, n) printf("请输入x,y,r:n"); for (i = 0; i < n; i+) scanf("%lf%lf%lf", &xi, &yi, &ri);printf("请输入a

24、,b:n"); scanf("%lf%lf", &a, &b); for (i = 0; i < n; i+) if (a - xi) * (a - xi) + (b - yi) * (b - yi) <= ri * ri) break; printf("Output:n"); puts(i < n ? "YES" : "NO");break; return 0;【1175】计算温度#include <stdio.h> #include <math.h&

25、gt; #define PI 3int main() float F, c;printf("Input F: "); scanf("%f", &F);printf("Output:n"); c = 5.0 / 9.0 * (F - 32); printf("%.2fn", c); return 0; 1155简单计算#include <stdio.h> int main() double x, y, ans; int a;printf("Input x,a,y

26、: "); scanf("%lf %d %lf", &x, &a, &y);printf("Output:n");printf("The ans is "); ans = x + a % 3 * (int)(x + y) % 2 / 4; printf("%.6fn", ans); return 0; 2141不容易系列之一#include<stdio.h>int main() long a21=0,0,1; int n,i; for(i=3;i<=20;i+)

27、ai=(i-1)*ai-1+(i-1)*ai-2;printf("Please input the number of friends:n"); while(scanf("%d",&n)!=EOF)&&n<=20&&n>0)printf("Output:n"); printf("%lldn",an);break;if(n>20|n<0) printf("Output:n");printf("input error!n&qu

28、ot;); return 0;对a计算#include <stdio.h>int main() int a;printf("Input:n"); scanf("%d", &a); a += a -= a *= a; printf("Output:n%dn", a); return 0;1157简单计算#include <stdio.h> int main() int a;printf("Input: "); scanf("%d", &a);printf(&

29、quot;Output:n"); a += a; printf("%dn", a); return 0; 1156简单计算#include <stdio.h> int main() float x, y, ans; int a, b; printf("Input: "); scanf("%d %d %f %f", &a, &b, &x, &y);printf("Output:n"); ans = (float)(a + b) / 2 + (int)x % (in

30、t)y; printf("The ans is %.6fn", ans); return 0;粗心的小明#include <stdio.h>int a,b;void run() int c,k; c=a+b;printf("nOutput:nthe right answer is:n"); printf("%d ",c);/这个程序是由正常的和来反求不进位的和(或者直接按位相加也是可以的 )。 k=1; while(k<=a|k<=b) if(a/k%10+b/k%10>9) c-=k*10;/在某一位上

31、的进位被忽略,相当于总和减小了这一位权值的十倍,个位进位被忽略,总和就少了10。 k*=10; printf("nnOutput:nthe wrong answer is:n"); printf("%dn",c);int main()printf("please input two numbers:n"); while(scanf("%d%d",&a,&b)!=EOF)run();break; return 0;输入输出#include <stdio.h>#include <mat

32、h.h> int main() int a, b, c, delta; double ans0, ans1, sqrt_delta;printf("please input a,b,c: "); scanf("%d %d %d", &a, &b, &c);printf("output:n"); /* 计算delta的值(b*b-4*a*c)并分类讨论 */ delta = b * b - 4 * a * c; if (delta > 0) sqrt_delta = sqrt(delta); ans0

33、 = (-b - sqrt_delta) / (2.0 * a); ans1 = (-b + sqrt_delta) / (2.0 * a); printf("x1=%.4f, x2=%.4fn", ans0, ans1); else if (delta = 0) ans0 = (-b) / (2.0 * a); printf("x=%.4fn", ans0); else printf("no answern"); return 0; 1189输入任一点的坐标,求该点的建筑高度(塔外的高度为0)#include <stdio.h

34、>int main() int h = 10; float x1=2,y1=2,x2=-2,y2=2,x3=-2,y3=-2,x4=2,y4=-2; float x,y,d1,d2,d3,d4;printf("Input:n请输入坐标:"); scanf("%f %f", &x, &y); /*求该点到各中心点的距离*/ d1=(x-x1) * (x-x1)+(y-y1) * (y-y1); d2=(x-x2) * (x-x2)+(y-y2) * (y-y2); d3=(x-x3) * (x-x3)+(y-y3) * (y-y3);

35、 d4=(x-x4) * (x-x4)+(y-y4) * (y-y4); if (d1>1 && d2>1 && d3>1 && d4>1) h=0; /*判断该点是否在塔外*/ printf("Output:n该点建筑物高度为:%.2fn", h + .0); return 0;2060 点的距离#include <stdio.h>#include <math.h>int main() int t; int ax, ay, bx, by; printf("Plseae

36、 input ax,ay bx,by:");scanf("%d,%d %d,%d",&ax,&ay,&bx,&by);printf("Output:nThe distance from a to b is:%.2f.n",sqrt(ax-bx)*(ax-bx)+(ay-by)*(ay-by); return 0;1159运算符的使用#include <stdio.h> int main() int a;printf("Input: "); scanf("%d",

37、 &a);printf("Output:n"); a *= 2 + 3; printf("%dn", a); return 0; 【1158】运算符的运用#include <stdio.h> int main() int a;printf("Input: "); scanf("%d", &a);printf("Output:n"); a -= 2; printf("%dn", a); return 0; 1160运算符的运用#include <

38、;stdio.h> int main() int a;printf("Input: "); scanf("%d", &a);printf("Output:n"); a /= a + a; printf("%dn", a); return 0; 【1161】运算符的使用#include <stdio.h> int main() int a, n; printf("Input a,n: "); scanf("%d %d", &a, &n)

39、;printf("Output:n"); a %= (n %= 2); printf("%dn", a); return 0; 【1162】运算符的使用#include <stdio.h> int main() int a;printf("Input: "); scanf("%d", &a);printf("Output:n"); a += a -= a *= a; printf("%dn", a); return 0; 【1153】进制转化#includ

40、e <stdio.h> int main() int n;printf("Input: "); scanf("%d", &n);printf("Output:n"); if (n < 0) printf("-%o -%xn", -n, -n); else printf("%o %xn", n, n); return 0; 处理一个数字的每一位#include "stdio.h" #include "conio.h" main( )

41、 long a,b,c,d,e,x; printf("Please input a number:n"); scanf("%ld",&x); a=x/10000;/*分解出万位*/ b=x%10000/1000;/*分解出千位*/ c=x%1000/100;/*分解出百位*/ d=x%100/10;/*分解出十位*/ e=x%10;/*分解出个位*/ printf("Output:n"); if (a!=0) printf("there are 5, %ld %ld %ld %ld %ldn",e,d,c,

42、b,a); else if (b!=0) printf("there are 4, %ld %ld %ld %ldn",e,d,c,b); else if (c!=0) printf(" there are 3,%ld %ld %ldn",e,d,c); else if (d!=0) printf("there are 2, %ld %ldn",e,d); else if (e!=0) printf(" there are 1,%ldn",e); 四舍五入#include<stdio.h>int mai

43、n()double num;int n;printf("Please input num and n: n");scanf("%lf,%d",&num,&n);printf("Output:n");printf("%.*fn",n,num);return 0;输入三角形的三边长a、b、c(边长可以是小数),求三角形面积area,并输出#include <stdio.h>#include <math.h>int main(void)double side1, side2, si

44、de3, circle, area;printf("please input triange sides:");scanf("%lf,%lf,%lf", &side1, &side2, &side3);printf("Output:n");if (side1 + side2 > side3 && side1 + side3 > side2 && side2 + side3 > side1)circle = (side1 + side2 + side3) / 2;

45、area = sqrt(circle * (circle - side1) * (circle - side2) * (circle - side3);printf("area=%.2fn", area);elseprintf("data errorn");return 0;输入x,y两个整数,输出其中较大的数。#include <stdio.h>int main(void)int number1, number2, max;printf("please input x,y:");scanf("%d,%d&quo

46、t;, &number1, &number2);if (number1 > number2)max = number1;elsemax = number2;printf("Output:n%d is biggern", max);return 0;输入月份,打印2003年该月有几天。当输入的月份超范围时,应显示“Invalid month input”。#include <stdio.h>int main(void)int month, day;printf("please input the month number:"

47、;);scanf("%d", &month);switch (month)case 1:case 3:case 5:case 7:case 8:case 10:case 12:day = 31;break;case 4:case 6:case 9:case 11:day = 30;break;case 2:day = 28;break;default:day = 0;break;printf("Output:n");if (day = 0)printf("Invalid month input !n");elseprintf(

48、"2003.%d has %d days.n", month, day);return 0;【1200】测定一串字符的英文字母,空格,数字和其他字符的个数#include <stdio.h> #include <math.h> int main() char c; int letter, space, digit, other;printf("Input: "); letter = 0; space = 0; digit = 0; other = 0; while (c = getchar() != 'n') if

49、 ('A' <= c && c <= 'Z') | ('a' <= c && c <= 'z') letter+; else if (c = ' ') space+; else if ('0' <= c && c <= '9') digit+; else other+; printf("Output:n"); printf("%d %d %d %dn", letter, space, digit, other); return 0; 企业发放奖金#include"stdio.h"#include"conio.h"main()long int i;int bonus1,bonus2,bonus4,bonus6,bonus10,bonus;printf("Please Input lrn");scanf("%ld",&i);bonus1=100000*0.1;bonus2=bonus1+100000*0.75;bonus4=bonus2

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论