C语言:基本控制结构

更新时间:2024-03-26 14:45:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

计算题

Time Limit: 1000MS Memory limit: 65536K

题目描述

一个简单的计算,你需要计算f(m,n),其定义如下: 当m=1时,f(m,n)=n; 当n=1时,f(m,n)=m;

当m>1,n>1时,f(m,n)= f(m-1,n)+ f(m,n-1) 输入

第一行包含一个整数T(1<=T<=100),表示下面的数据组数。

以下T行,其中每组数据有两个整数m,n(1<=m,n<=2000),中间用空格隔开。 输出

对每组输入数据,你需要计算出f(m,n),并输出。每个结果占一行。 示例输入 2 1 1 2 3

示例输出 1 7

#include int f(int m,int n) { if(m==1) return n; else if(n==1) return m; else return f(m-1,n)+f(m,n-1); }

void main() { int m,n,t,sum; scanf(\ while(t--) { scanf(\ sum=f(m,n); printf(\ } }

简单计算

Time Limit: 1000MS Memory limit: 65536K

题目描述

接受从键盘输入的N个整数,输出其中的最大值、最小值和平均值。 输入

第一行一个正整数N(N<=100);

第二行有N个用空格隔开的整数Ti (1<= i <=N, 0<= Ti <= 1000) ouput 输出

三个有空格隔开的整数分别为最大值、最小值和平均值。 示例输入 5

1 2 3 5 4

示例输出 5 1 3

#include void main() {

int n,a=0,b,i,j,t[100]; scanf(\ for(i=0;i<=n-1;i++) {

scanf(\ a+=t[i]; }

for(i=0;i<=n-2;i++) {

for(j=0;j<=n-i-2;j++) {

if(t[j]>t[j+1]) {

b=t[j];

t[j]=t[j+1]; t[j+1]=b; } } }

printf(\ }

IBM Minus One

Time Limit: 1000MS Memory limit: 65536K

题目描述

You may have heard of the book '2001 - A Space Odyssey' by Arthur C. Clarke, or the film of the same name by Stanley Kubrick. In it a spaceship is sent from Earth to Saturn. The crew is put into stasis for the long flight, only two men are awake, and the ship is controlled by the intelligent computer HAL. But during the flight HAL is acting more and more strangely, and even starts to kill the crew on board. We don't tell you how the story ends, in case you want to read the book for yourself :-)

After the movie was released and became very popular, there was some discussion as to what the name 'HAL' actually meant. Some thought that it might be an abbreviation for 'Heuristic ALgorithm'. But the most popular explanation is the following: if you replace every letter in the word HAL by its successor in the alphabet, you get ... IBM.

Perhaps there are even more acronyms related in this strange way! You are to write a program that may help to find this out. 输入

The input starts with the integer n on a line by itself - this is the number of strings to follow. The following n lines each contain one string of at most 50 upper-case letters. 输出

For each string in the input, first output the number of the string, as shown in the sample output. The print the string start is derived from the input string by replacing every time by the following letter in the alphabet, and replacing 'Z' by 'A'.

Print a blank line after each test case. 示例输入 2 HAL SWERC 示例输出 String #1 IBM

String #2 TXFSD

#include #include void main() {

char s[50]; int i,a,b,k=1; scanf(\ while(b--) {

scanf(\ a=strlen(s);

printf(\ for(i=0;i

if(s[i]>='A'&&s[i]<='Y') printf(\ else if(s[i]=='Z')

printf(\ }

printf(\ k++; } }

No Brainer

Time Limit: 1000MS Memory limit: 65536K

题目描述

Zombies love to eat brains. Yum. 输入

The first line contains a single integer n indicating the number of data sets.

The following n lines each represent a data set. Each data set will be formatted according to the following description:

A single data set consists of a line \eats and Y is the number of brains the zombie requires to stay alive. 输出

For each data set, there will be exactly one line of output. This line will be \BRAINS\

number of brains the zombie requires to stay alive. Otherwise, the line will be \BRAINS\示例输入 3 4 5 3 3 4 3

示例输出 NO BRAINS MMM BRAINS MMM BRAINS

#include void main() {

int n,i,k;

scanf(\ while(n--) {

scanf(\

if(i

else if(i>=k) printf(\ } }

C语言实验——交换两个整数的值(顺序结构) Time Limit: 1000MS Memory limit: 65536K

题目描述

交换两个变量的值,由终端输入两个整数给变量x、y,然后交换x和y的值后,输出x和y。 输入

从键盘输入两个整数变量x和y; 输出

在交换x、y的值后将x和y输出! 示例输入 4 6

示例输出 6 4

#include

void main() { int x,y,t; scanf(\ t=x; x=y; y=t; printf(\}

C语言实验——转换字母(顺序结构) Time Limit: 1000MS Memory limit: 65536K

题目描述

从键盘上输入一个小写字母,然后将小写字母装换成大写字母输出! 输入

从键盘上输入一个小写字母。 输出

小写字母装换成大写字母输出。 示例输入 a

示例输出 A

#include void main() { char a; scanf(\ if(a>='a'||a<='z') printf(\}

C语言实验——大小写转换

Time Limit: 1000MS Memory limit: 65536K

题目描述

把一个字符串里所有的大写字母换成小写字母,小写字母换成大写字母。其他字符保持不变。 输入

输入为一行字符串,其中不含空格。长度不超过80个字符。

输出

输出转换好的字符串。 示例输入 ABCD123efgh

示例输出 abcd123EFGH

#include #include void main() { char a[80],b[80]; int t,k; scanf(\ t=strlen(a); for(k=0;k='a'&&a[k]<='z') a[k]=a[k]-32; else if(a[k]>='A'&&a[k]<='Z') a[k]=a[k]+32; } for(k=0;k

C语言实验——求绝对值(选择结构) Time Limit: 1000MS Memory limit: 65536K

题目描述

从键盘上输入任意一个整数,然后输出它的绝对值! 输入

从键盘上输入任意一个整数。 输出

输出它的绝对值。 示例输入 -4 示例输出 4

#include void main() {

int a;

scanf(\

if(a<0) printf(\ else printf(\}

相加和最大值

Time Limit: 1000MS Memory limit: 65536K

题目描述

输入三个整数a,b,c。并进行两两相加,最后比较相加和的最大值。 输入

输入数据包含三个整数,用空格分开。 输出

输出两两相加后的最大值。 示例输入 1 2 3

示例输出 5

#include void main() { int a,b,c,x,y,z,t; scanf(\ x=a+b; y=a+c; z=b+c; if(x>=y) { t=x; x=y; y=t; } if(y>=z) { t=y; y=z; z=t; } printf(\}

C语言实验——分数序列

Time Limit: 1000MS Memory limit: 65536K

题目描述

有一个分数序列:2/1, 3/2, 5/3, 8/5, 13/8, …编写程序求出这个序列的前n项之和。 输入

输入只有一个正整数n,1≤n≤10。 输出

输出该序列前n项和,结果保留小数后6位。 示例输入 3

示例输出 5.166667

#include void main() { int n,i,j,k=2,h=1; float s=0.0; scanf(\ for(i=1;i<=n;i++) { s=s+(k+0.0)/h; j=k; k=j+h; h=j; } printf(\}

C语言实验——保留整数

Time Limit: 1000MS Memory limit: 65536K

题目描述

输入一个字符串str1,把其中的连续非数字的字符子串换成一个‘*’,存入字符数组str2 中,所有数字字符也必须依次存入 str2 中。输出str2。 输入

输入为一行字符串str1,其中可能包含空格。字符串长度不超过80个字符。 输出

输出处理好的字符串str2。 示例输入

$Ts!47&*s456 a23* +B9k

示例输出

*47*456*23*9*

#include #include main() { char s[80],a[80]; int i,n=0; gets(s); for(i=0;s[i]!='\\0';i++) { if(s[i]>='0'&&s[i]<='9') { a[n++]=s[i]; } else if((s[i+1]!='\\0')&&!(s[i+1]>='0'&&s[i+1]<='9')) ; else a[n++]='*'; } for(i=0;i

#include #include void main() { char str1[80],str2[80]; int a,k,n=0; scanf(\ a=strlen(str1); for(k=0;k='0'&&str1[k]<='9') { str2[n]=str1[k]; n++; } else if((str1[k+1]!='\\0')&&(str1[k+1]<'0')&&(str1[k+1]>'9')); else str2[n]='*'; } for(k=0;k

优越数

Time Limit: 1000MS Memory limit: 65536K

题目描述

给定3个数,如果有两个数大于他们的平均数则称这组数为优越数。(定义纯属虚构)

输入

输入第一行是一个整数: 表示测试数据的组数。 对于每组测试数据,仅一行3个整数。

输出

对于每组输入数据输出一行,判断它是否为一组优越数,如果是输出“Yes”(输出不包括引号),否则输出“No”。

示例输入 2 1 2 3 1 4 4

示例输出 No Yes

#include void main() { int a,b,c,h,n,t; scanf(\ while(n--) { scanf(\ if(a>b) {t=a; a=b; b=t;} if(b>c) {t=b; b=c; c=t;} h=(a+b+c)/3; if(h

计算球体积

Time Limit: 1000MS Memory limit: 65536K

题目描述

根据输入的半径值,计算球的体积。 输入

输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。 输出

输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。 示例输入 1 1.5 示例输出 4.189 14.137 提示

#define PI 3.1415927

#include #include

#define PI 3.1415927 void main() { float n,v; while(scanf(\ { v=(4*PI*n*n*n)/3; printf(\ } }

数列求和

Time Limit: 1000MS Memory limit: 4096K

题目描述

数列求和是一类常见的问题,本题有一定的代表性: 求s=a+aa+aaa+aaaa+……+aa…aa(n位) 其中,a的值由键盘输入,位数n也由键盘输入。 输入

第一行输入a的值; 第二行输入位数n。 输出

输出对n个数完成求和运算后的结果。

比如a=3,n=6时,s=3+33+333+3333+33333+333333 示例输入 3 6

示例输出 370368

#include #include void main() { int a,n,s=0,i; scanf(\ scanf(\ for(i=1;i<=n;i++) { s=s+a*(pow(10,i)-1)/9; } printf(\}

a的n次方

#include {

pow(a,n); }

水仙花数

Time Limit: 1000MS Memory limit: 65536K

题目描述

春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,是这样定义的: “水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=13+53+33。 现在要求输出所有在m和n范围内的水仙花数。 输入

输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。 输出

对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;

如果给定的范围内不存在水仙花数,则输出no; 每个测试实例的输出占一行。 示例输入 100 120 300 380

示例输出 no

370 371

#include #include void main() { int m,n,a,b,c,i,k=0; while(scanf(\ { scanf(\ for(i=m;i<=n;i++) { a=i/100; b=(i-a*100)/10; c=i-a*100-b*10; if(i==pow(a,3)+pow(b,3)+pow(c,3)) { printf(\ k++; } } if(k==0) printf(\ } }

母牛的故事

Time Limit: 1000MS Memory limit: 65536K

题目描述

有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛? 输入

输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0< n< 55),n的含义如题目中描述。 n=0表示输入数据的结束,不做处理。 输出

对于每个测试实例,输出在第n年的时候母牛的数量。 每个输出占一行。 示例输入 2 4 5 0

示例输出 2 4 6

#include void main() { int f[100],i,n; f[0]=1;f[1]=2;f[2]=3;f[3]=4; while(scanf(\ { for(i=5;i<=n;i++) { f[i-1]=f[i-2]+f[i-4]; } printf(\ } }

查找最大元素

Time Limit: 1000MS Memory limit: 65536K

题目描述

对于输入的每个字符串,查找其中的最大字母,在该字母后面插入字符串“(max)”。 输入

输入数据包括多个测试实例,每个实例由一行长度不超过100的字符串组成,字符串仅由大小写字母构成。 输出

对于每个测试实例输出一行字符串,输出的结果是插入字符串“(max)”后的结果,如果存在多个最大的字母,就在每一个最大字母后面都插入\。 示例输入

abcdefgfedcba

xxxxx

示例输出

abcdefg(max)fedcba

x(max)x(max)x(max)x(max)x(max)

#include #include void main() { char s[100],a[100],t; int l,i,j; while(scanf(\ { l=strlen(s); for(i=0;i*十点半

Time Limit: 1000MS Memory limit: 65536K

题目描述

十点半是一个纸牌游戏,或者说数字游戏。这里简化一下,规则是每个人摸两张牌,然后只通过加减运算,如果能够得到十点半的话就算赢,否则就输。扑克从2到K分别代表2~13点,A代表半点,然后王或老头或司令随便你怎么叫,不分大小,都代表半点。

输入

输入有多组数据。第一行一个正整数T代表数据的组数。接下来N行,每行两张牌。其中11到13的牌是J,Q,K,王是S。

输出

输出也要N行,每行的格式是如果赢了Case P: WIN,输了Case P: LOSE。其中P代表是第几组数据。 示例输入 4 10 A A J 10 S 2 8

示例输出 Case 1: WIN Case 2: WIN Case 3: WIN Case 4: LOSE

#include void main() {

int i,n,j;

float q,w,sum=0,cha=0,t; char a[2],d[2]; scanf(\ for(j=1;j<=n;j++) {

scanf(\ if(a[0]=='2') q=2;

else if(a[0]=='3') q=3; else if(a[0]=='4') q=4; else if(a[0]=='5') q=5; else if(a[0]=='6') q=6; else if(a[0]=='7') q=7; else if(a[0]=='8') q=8; else if(a[0]=='9') q=9;

else if(a[0]=='1') q=10;//==1 是因为10是两个字符了,a[0]是一个字符的位置,而前面也没有1,所以用1就可以了 else if(a[0]=='J') q=12; else if(a[0]=='Q') q=11; else if(a[0]=='K') q=13; else if(a[0]=='A') q=0.5; else if(a[0]=='S') q=0.5;

if(d[0]=='2') w=2;

else if(d[0]=='3') w=3; else if(d[0]=='4') w=4; else if(d[0]=='5') w=5; else if(d[0]=='6') w=6; else if(d[0]=='7') w=7; else if(d[0]=='8') w=8; else if(d[0]=='9') w=9; else if(d[0]=='1') w=10; else if(d[0]=='J') w=11; else if(d[0]=='Q') w=12; else if(d[0]=='K') w=13; else if(d[0]=='A') w=0.5; else if(d[0]=='S') w=0.5;

// printf(\

// printf(\ for(i=0;i<2;i++) {

if(q>w) {

t=q; q=w; w=t; } }

sum=w+q; cha=w-q;

// printf(\ if(sum==10.5||cha==10.5)

printf(\ else printf(\ } }

C语言实验——保留字母

Time Limit: 1000MS Memory limit: 65536K

题目描述

编一个程序,输入一个字符串,将组成字符串的所有非英文字母的字符删除后输出。 输入

一个字符串,长度不超过80个字符。 输出

删掉非英文字母后的字符串。 示例输入

abc123+xyz.5 示例输出 abcxyz

#include #include void main()

{ int i,a,j=0; char s[80],k[80]; scanf(\ a=strlen(s); for(i=0;i='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z') printf(\ } printf(\}

#include #include void main() { int i,a,j=0; char s[80],k[80]; scanf(\ a=strlen(s); for(i=0;i='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z') { k[j]=s[i]; j++; } } for(i=0;i

C语言实验——三个整数和、积与平均值 Time Limit: 1000MS Memory limit: 65536K

题目描述

给出三个整数,请你设计一个程序,求出这三个数的和、乘积和平均数。 输入

输入只有三个正整数a、b、c。 输出

输出一行,包括三个的和、乘积、平均数。 数据之间用一个空格隔开,其中平均数保留小数后面两位。

示例输入 1 2 3

示例输出 6 6 2.00

#include void main() { int a,b,c,sum,pro; float ave; scanf(\ sum=a+b+c; pro=a*b*c; ave=sum/3.0; printf(\}

C语言实验——圆周率

Time Limit: 1000MS Memory limit: 65536K

题目描述

输入n值,并利用下列格里高里公式计算并输出圆周率:

输入

输入公式中的n值。 输出

输出圆周率,保留5位小数。 示例输入 1

示例输出 2.66667

#include void main() { int i,n; float k; scanf(\ for(i=1;i<=n;i++) { k=4*(1.0/(4*i-3))-4*(1.0/(4*i-1)); } printf(\}

求三角形面积

Time Limit: 1000MS Memory limit: 65536K

题目描述

已知三角形的边长a、b和c,求其面积。 输入

输入三边a、b、c。 输出

输出面积,保留3位小数。 示例输入 1 2 2.5

示例输出 0.950

#include #include void main() { float a,b,c,p,area; scanf(\ p=(a+b+c)/2; area=sqrt(p*(p-a)*(p-b)*(p-c)); printf(\}

C语言实验——温度转换

Time Limit: 1000MS Memory limit: 65536K

题目描述

输入一个华氏温度,输出摄氏温度,其转换公式为:C=5(F-32)/9。 输入

输入数据只有一个实数,即华氏温度。 输出

输出数据只有一个,即摄氏温度,保留2位小数。 示例输入 32.0

示例输出 0.00

#include #include

void main() { float F,C; scanf(\ C=5*(F-32)/9; printf(\}

分段函数求值

Time Limit: 1000MS Memory limit: 65536K

题目描述 有如下分段函数

F(x) = x^2 + 1 当x> 0时; F(x) = -x 当x<0时; F(x) = 100.00 当x=0时;

编程根据输入的不同x(x为实数且|x| <= 1000),输出其对应的函数值 输入

多组输入,每组一个实数x。处理到文件结束。 输出

对于每组输入x,输出其对应的F(x),每组一行,结果保留1位有效数字。 示例输入 8.00 -5.0

示例输出 65.0 5.0

#include #include void main() { float x,f; while(scanf(\ { if(x>0) f=1+pow(x,2); else if(x<0) f=-x; else if(x==0) f=100.00; printf(\ } }

C语言实验——买糖果

Time Limit: 1000MS Memory limit: 65536K

题目描述

小瑜是个爱吃糖果的馋鬼,天天嚷着要爸爸买糖果,可是爸爸很忙,哪有时间啊,于是就让小瑜自己去了,糖果3角钱一块,爸爸给小瑜n元钱,请你告诉小瑜最多能买几块糖,还剩几角钱? 输入

输入爸爸给小瑜的钱n元,n为整数。 输出

小瑜最多能买回的糖块数以及剩下的钱(单位为:角),用空格分隔。 示例输入 2

示例输出 6 2

#include void main() { int n,r,t; scanf(\ t=10*n/3; r=(10*n)%3; printf(\}

Doubles

Time Limit: 1000MS Memory limit: 65536K

题目描述

As part of an arithmetic competency program, your students will be given randomly generated lists of from 2 to 15 unique positive integers and asked to determine how many items in each list are twice some other item in the same list. You will need a program to help you with the grading. This program should be able to scan the lists and output the correct answer for each one. For example, given the list

1 4 3 2 9 7 18 22

your program should answer 3, as 2 is twice 1, 4 is twice 2, and 18 is twice 9. 输入

The input file will consist of one or more lists of numbers. There will be one list of numbers per line. Each list will contain from 2 to 15 unique positive integers. No integer will be larger than 99. Each line will be terminated with the integer 0, which is not considered part of the list. A line with the single number -1 will mark the end of the file. The example input below shows 3 separate lists. Some lists may not contain any doubles.

输出

The output will consist of one line per input list, containing a count of the items that are double some other item. 示例输入

1 4 3 2 9 7 18 22 0 2 4 8 10 0

7 5 11 13 1 3 0 -1

示例输出 3 2

#include void main() { int n,k,j,a[99]; while(scanf(\ { int i=1,s=0; if(a[0]==-1) break; else { while(scanf(\ { i++; } for(k=0;k

素数

Time Limit: 1000MS Memory limit: 32768K

题目描述

给你一个正整数N(N<=1000),希望你求出1~N内的所有素数。 输入

只有一个正整数N。 输出

输出一个整数,即1~N内的素数个数。 示例输入 10

示例输出 4

#include #include void main() { int n,i,j,k,s=0; scanf(\ for(i=1;i<=n;i=i+2) { for(j=2;j<=sqrt(i);j++) if(i%j==0) break; if(j>sqrt(i)) s++; } printf(\}

图案打印

Time Limit: 1000MS Memory limit: 65536K

题目描述

一年一度的植树节就要到了,计算机学院学生准备在学院教学楼门前的空地上种植树木。为使树木排列得更加美观,大家决定把树木排列成菱形。现在告诉你我们所拥有的树木能排列成边长为N的菱形,请你编程输出树木所排列的图案(用*号代表树木)。 输入

一个整数N(1≤N≤10)。 输出

排列成菱形图案的*号。请注意在图案中:每行树木之间无空行,每列树木之间均有一个空列。 示例输入

3

示例输出 * * * * * * * *

#include int main() {

int n, i, j;

scanf(\

for (i=1; i<=(n-1)*2; i++) printf(\ printf(\

for (i=2; i

if (i <= n) {

for (j=1; j<=(n-i)*2; j++) printf(\ printf(\

for (j=1; j<=(i-1)*4-1; j++) printf(\ printf(\ } else {

for (j=1; j<=(i-n)*2; j++) printf(\ printf(\

for (j=1; j<=(n-1)*4-1-(i-n)*4; j++) printf(\ printf(\ }

printf(\ }

for (i=1; i<=(n-1)*2; i++) printf(\ printf(\ return 0; }

数列求和

Time Limit: 1000MS Memory limit: 4096K

题目描述

数列求和是一类常见的问题,本题有一定的代表性: 求s=a+aa+aaa+aaaa+??+aa?aa(n位)

其中,a的值由键盘输入,位数n也由键盘输入。 输入

第一行输入a的值; 第二行输入位数n。 输出

输出对n个数完成求和运算后的结果。

比如a=3,n=6时,s=3+33+333+3333+33333+333333 示例输入 3 6

示例输出 370368

#include #include int main() {

int a,n,k,s=0,i;

scanf(\ for(i=1;i<=n;i++) { k=(pow(10,i)-1)*a/9; s+=k; }

printf(\}

C/C++经典程序训练5---图形打印问题 Time Limit: 1000MS Memory limit: 4096K

题目描述

图形的规则如下 ,要求输入n的值,按照图形的打印规则打印出相关的图形:

输入

输入整数n。 输出

按图形的规律打印出相关的图形。 示例输入 4

示例输出 + +*+ +***+ +*****+ +***+ +*+ +

#include int main() {

int n, i, j,k; scanf(\

for (i=1; i<=n-1; i++) printf(\ printf(\

for (i=2; i

if (i <= n) {

for (j=1; j<=n-i; j++) printf(\ printf(\

for (k=1; k<=(i-2)*2+1; k++) printf(\ printf(\ }

else {

for (j=1; j<=i-n; j++) printf(\ printf(\

for (k=1; k<=(n-2)*2+1-(i-n)*2; k++) printf(\ printf(\ }

printf(\ }

for (i=1; i<=n-1; i++) printf(\ printf(\ return 0; }

C语言实验——分数序列

Time Limit: 1000MS Memory limit: 65536K

题目描述

有一个分数序列:2/1, 3/2, 5/3, 8/5, 13/8, ?编写程序求出这个序列的前n项之和。 输入

输入只有一个正整数n,1≤n≤10。 输出

输出该序列前n项和,结果保留小数后6位。 示例输入 3

示例输出 5.166667

#include void main() { int n,i,m,k=1,j=1; double s,d=0; scanf(\ for(i=1;i<=n;i++) { m=k+j; s=(m+0.0)/k; d=d+s; j=k; k=m;

}

}

printf(\

C语言实验——打印菱形

Time Limit: 1000MS Memory limit: 65536K

题目描述

从键盘输入一个整数n(1≤n≤9),打印出指定的菱形。 输入

正整数n(1≤n≤9)。 输出

指定的菱形。

第一行前面有n-1个空格,第二行有n-2个空格,依此类推。 示例输入 5

示例输出 * *** ***** ******* ********* ******* ***** *** *

#include int main() { int n,i,k,l; scanf(\ for(i=1;i<=n;i++) { for(k=1;k<=n-i;k++) { printf(\ } for(l=1;l<=2*i-1;l++) { printf(\

}

} printf(\}

for(i=n-1;i>0;i--) { for(k=1;k

return 0;

C语言实验——打印数字图形

Time Limit: 1000MS Memory limit: 65536K

题目描述

从键盘输入一个整数n(1≤n≤9),打印出指定的数字图形。 输入

正整数n(1≤n≤9)。 输出

指定数字图形。 示例输入 5

示例输出 1 121 12321 1234321 123454321 1234321 12321 121 1

#include

int main() {

int n, i, j,k; scanf(\

for (i=1; i

if (i <= n) {

for (j=1; j<=n-i; j++) printf(\

for (k=1; k

for (j=1; j<=i-n; j++) printf(\

for (k=1; k<=2*n-i; k++) printf(\ for (k=1; k<2*n-i; k++) printf(\ }

printf(\ }

return 0; }

杨辉三角

Time Limit: 1000MS Memory limit: 65536K

题目描述 1 1 1 1 2 1 1 3 3 1 1 4 6 41

1 5 10 10 5 1

上面的图形熟悉吗?它就是我们中学时候学过的杨辉三角。 输入

输入数据包含多组测试数据。

每组测试数据的输入只有一个正整数n(1≤n≤30),表示将要输出的杨辉三角的层数。 输入以0结束。

输出

对应于每一个输入,请输出相应层数的杨辉三角,每一层的整数之间用一个空格隔开,每一个杨辉三角后面加一个空行。 示例输入 2 3 0

示例输出 1 1 1 1 1 1 1 2 1

#include void main() { int n,i,j,a[30][30]; while(scanf(\ { a[1][1]=1; a[2][1]=1;a[2][2]=1; for(i=3;i<=n;i++) { a[i][1]=1;a[i][i]=1; for(j=2;j

求绝对值最大值

Time Limit: 1000MS Memory limit: 65536K

题目描述

求n个整数中的绝对值最大的数。 输入

输入数据有2行,第一行为n,第二行是n个整数。 输出

输出n个整数中绝对值最大的数。 示例输入 5

-1 2 3 4 -5 示例输出 -5

#include void main() { int n,i,k,s,t,a[100000]; scanf(\ for(i=0;ia[k]) { t=a[i]; a[i]=a[k]; a[k]=t; } } } if(a[0]<0&&a[n-1]<0) { if(-a[0]<-a[n-1])

}

printf(\ if(-a[0]>-a[n-1]) printf(\}

if(a[0]<0&&a[n-1]>0) { if(-a[0]a[n-1]) printf(\}

if(a[0]>0) printf(\

相加和最大值

Time Limit: 1000MS Memory limit: 65536K

题目描述

输入三个整数a,b,c。并进行两两相加,最后比较相加和的最大值。 输入

输入数据包含三个整数,用空格分开。 输出

输出两两相加后的最大值。 示例输入 1 2 3 示例输出 5

#include void main() { int a,b,c,m,n,t,max; scanf(\ m=a+b; n=a+c; t=b+c; max=m; if(max

}

C语言实验——求三个整数的最大值 Time Limit: 1000MS Memory limit: 65536K

题目描述

请编写程序,输入三个整数,求出其中的最大值输出。 输入

在一行上输入三个整数,整数间用逗号分隔。 输出

输出三个数中的最大值。 示例输入 5,7,9 示例输出 max=9

#include void main() { int a,b,c,max; scanf(\ max=a; if(max

C语言实验——最小公倍数和最大公约数 Time Limit: 1000MS Memory limit: 65536K

题目描述

从键盘输入两个正整数,求这两个正整数的最小公倍数和最大公约数,并输出。

输入

输入包括一行。

两个以空格分开的正整数。 输出

两个整数的最小公倍数和最大公约数。 示例输入 6 8

示例输出 24 2

#include void main() { int a,b,m,n,t,i,y,x; scanf(\ m=a; n=b; for(i=1;;i++) { if(m%n!=0) { t=m%n; m=n; n=t; } if(m%n==0) break; } y=n; x=a*b/y; printf(\}

C语言实验——素数

Time Limit: 1000MS Memory limit: 65536K

题目描述

输出100->200之间的素数的个数,以及所有的素数。 输入

无 输出

100->200之间的素数的个数,以及所有的素数。 示例输入 示例输出 21

101 103 ... 197 199

#include #include void main() { int i,j,k,s=0; for(i=101;i<=200;i=i+2) { for(j=2;j<=sqrt(i);j++) if(i%j==0) break; if(j>sqrt(i)) s++; } printf(\ for(i=101;i<=200;i=i+2) { for(j=2;j<=sqrt(i);j++) if(i%j==0) break; if(j>sqrt(i)) printf(\ } printf(\}

#include #include void main() { int i,j,k,s=0,a[100]; for(i=101;i<=200;i=i+2) { for(j=2;j<=sqrt(i);j++) if(i%j==0) break; if(j>sqrt(i)) { a[s]=i; s++; }

}

}

printf(\for(j=0;j

C/C++经典程序训练2---斐波那契数列 Time Limit: 1000MS Memory limit: 65536K

题目描述

编写计算斐波那契(Fibonacci)数列的第n项函数fib(n)(n<40)。 数列: f1=f2==1;

fn=fn-1+fn-2(n>=3)。 输入

输入整数n的值。 输出

输出fib(n)的值。 示例输入 7

示例输出 13

#include void main() { int f[100],n,i; f[0]=1; f[1]=1; scanf(\ for(i=2;i

C语言实验——从大到小输出a、b、c(选择结构) Time Limit: 1000MS Memory limit: 65536K

题目描述

从键盘输入三个整数a、b、c,要求将输出的数据按从大到小排序后输出。 输入

从键盘上输入三个整数a、b、c,每个整数之间用空格分开。 输出

从大到小顺序输出a、b、c的值。 示例输入 4 3 5 示例输出 5 4 3

#include void main() { int a,b,c,t; scanf(\ if(a

C语言实验——合法的C标识符

Time Limit: 1000MS Memory limit: 65536K

题目描述

给出一个标识符,请你判断它是否是C语言合法的标识符。 输入

输入一个标识符,长度不超过100。 输出

判断是否合法,如果是输出YES,否则输出NO。

示例输入 123You 示例输出 NO 提示

C语言规定:标识符只能由字母、数字和下划线3种字符组成,且第一个字符必须为字母或下划线。

#include #include void main() { char s[100]; while(scanf(\ { int l,flag,i; l=strlen(s); if((s[0]>'a'&&s[0]<'z')||(s[0]>'A'&&s[0]<'Z')||(s[0]=='_')) { for(i=1;i

if((s[i]>'a'&&s[i]<'z')||(s[i]>'A'&&s[i]<'Z')||(s[i]>'0'&&s[i]<'9')||s[i]=='_') flag=1; else { flag=0; break;//出现错误,跳出循环 } } } else flag=0; if(flag==0) printf(\ if(flag==1) printf(\ } }

简单密码破解

Time Limit: 1000MS Memory limit: 65536K

题目描述

密码是我们生活中非常重要的东东,我们的那么一点不能说的秘密就全靠它了。哇哈哈. 接下来渊子要在密码之上再加一套密码,虽然简单但也安全。

假设渊子原来一个BBS上的密码为zvbo941987,为了方便记忆,他通过一种算法把这个密码变换成YUANzi1987,这个密码是他的名字和出生年份,怎么忘都忘不了,而且可以明目张胆地放在显眼的地方而不被别人知道真正的密码。

他是这么变换的,大家都知道手机上的字母: 1--1, abc--2, def--3, ghi--4, jkl--5, mno--6, pqrs--7, tuv--8 wxyz--9, 0--0,就这么简单,渊子把密码中出现的小写字母都变成对应的数字,数字和其他的符号都不做变换,声明:密码中没有空格,而密码中出现的大写字母则变成小写之后往后移一位,如:X,先边成小写,再往后移一位,不就是y了嘛,简单吧。记住,z往后移是a哦。 输入

输入包括多个测试数据。输入是一个明文,密码长度不超过100个字符,输入直到文件结尾。 输出

输出渊子真正的密文。 示例输入 YUANzi1987 示例输出 zvbo941987

#include #include void main() { int i,l; char s[100]; scanf(\ l=strlen(s); for(i=0;i='A'&&s[i]<='Y') printf(\ if(s[i]=='Z') printf(\ if(s[i]>='0'&&s[i]<='9') printf(\ } }

闰年

Time Limit: 1000MS Memory limit: 32768K

题目描述

时间过得真快啊,又要过年了,同时,我们的人生也增长了一年的阅历,又成熟了一些。可是,你注意过今年是不是闰年呢,明年呢?

以上是闰年的计算方法的流程图,聪明的你能否通过编程计算任意给出的一个年份是否是闰年呢?相信这个问题你能很快解决掉。 输入

只有一个整数year,代表年份范围在1900~2060之间。 输出

如果是闰年输出Yes,否则输出No。 示例输入 2000 示例输出 Yes

#include void main() { int year; scanf(\ if((year%4==0&&year0!=0)||year@0==0) printf(\ else printf(\}

Friday the Thirteenth

Time Limit: 1000MS Memory limit: 65536K

题目描述

Is Friday the 13th really an unusual event?

That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is non-negative and will not exceed 400.

There are few facts you need to know before you can solve this problem:

? January 1, 1900 was on a Monday.

? Thirty days has September, April, June, and November, all the rest have 31 except for

February which has 28 except in leap years when it has 29.

? Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year,

but the year 1990 is not a leap year)

? The rule above does not hold for century years. Century years divisible by 400 are leap

years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year. Do not use any built-in date functions in your computer language. Don't just precompute the answers, either, please. PROGRAM NAME: friday

输入

there are several test cases, each have the following format: One line with the integer N.

输出 For each input, there is an output correspond to it, each have the following format: Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday. 示例输入 20

示例输出

36 33 34 33 35 35 34

Geometry Made Simple

Time Limit: 1000MS Memory limit: 65536K

题目描述

Mathematics can be so easy when you have a computer. Consider the following example. You probably know that in a right-angled triangle, the length of the three sides a, b, c (where c is the longest side, called the hypotenuse) satisfy the relation a*a+b*b=c*c. This is called Pythagora's Law.

Here we consider the problem of computing the length of the third side, if two are given.

输入

The input contains the descriptions of several triangles. Each description consists of a line containing three integers a, b and c, giving the lengths of the respective sides of a right-angled triangle. Exactly one of the three numbers is equal to -1 (the 'unknown' side), the others are positive (the 'given' sides).

A description having a=b=c=0 terminates the input. 输出

For each triangle description in the input, first output the number of the triangle, as shown in the sample output. Then print \side lengths. Otherwise output the length of the 'unknown' side in the format \the name of the unknown side (a, b or c), and l is its length. l must be printed exact to three digits to the right of the decimal point.

Print a blank line after each test case. 示例输入 3 4 -1 -1 2 7 5 -1 3 0 0 0 示例输出

Triangle #1 c = 5.000

Triangle #2 a = 6.708

Triangle #3 Impossible.

C语言实验——输入数字星期,输出英文(switch语句)

Time Limit: 1000MS Memory limit: 65536K

题目描述

从键盘上输入数字星期,然后输出它的英文。 其对应关系是: 1 Monday 2 Tuesday 3 Wednesday 4 Thursday 5 Friday 6 Saturday 7 Sunday

输入

从键盘输入数字星期,输入数字在1-7之间。 输出

输出该数字对应的英文星期表示。 示例输入 2

示例输出 Tuesday

#include void main() { int k; scanf(\ switch(k) {

case 1: printf(\ case 2: printf(\ case 3: printf(\ case 4: printf(\ case 5: printf(\ case 6: printf(\ case 7: printf(\dafault: printf(\ } }

C语言实验——数日子

Time Limit: 1000MS Memory limit: 65536K

题目描述

光阴似箭,日月如梭,大学的时间真是宝贵,要抓紧时间AC^_^。你知道今天是这一年第几天吗,掐指一算还是要算好久,呵呵还是让计算机来做吧。这里的问题就是让你来写一个程序,输入某年某月某日,判断这一天是这一年的第几天? 输入

输入数据有多组,第一行是数据的组数n,下面n行是n组数据,每组数据由3个正整数组成,分别为年、月、日,我们保证每组数据都是有效的日期。 输出

输出所输入的日期是这一年的第几天。 示例输入 2

2009 1 1 2008 1 3 示例输出 1 3

#include void main() { int n; scanf(\

while(n--) { int sum=0,year,month,day; scanf(\ switch(month) { case 1: sum=sum+day; break; case 2: sum=31+day; break; case 3: sum=60+day; sum=91+day; sum=121+day; sum=152+day; sum=182+day; case 4: case 5: case 6: case 7: case 8: { if((year%4==0&&year0!=0)||(year@0==0)) else sum=59+day; break; } { if((year%4==0&&year0!=0)||(year@0==0)) else sum=90+day; break; } { if((year%4==0&&year0!=0)||(year@0==0)) else sum=120+day; break; } { if((year%4==0&&year0!=0)||(year@0==0)) else sum=151+day; break; } { if((year%4==0&&year0!=0)||(year@0==0)) else sum=181+day; break; } {

sum=213+day; sum=244+day; sum=274+day; sum=305+day; sum=335+day; } }

case 9:

} {

if((year%4==0&&year0!=0)||(year@0==0)) else sum=212+day; break;

if((year%4==0&&year0!=0)||(year@0==0)) else sum=243+day; break;

} case 10: { } case 11: { } case 12: {

if((year%4==0&&year0!=0)||(year@0==0)) else sum=273+day;

break;

if((year%4==0&&year0!=0)||(year@0==0)) else sum=304+day; break;

if((year%4==0&&year0!=0)||(year@0==0))

else sum=334+day; break; }

default: printf(\}

printf(\

C语言实验——某年某月的天数

Time Limit: 1000MS Memory limit: 65536K

题目描述

输入年和月,判断该月有几天? 输入

输入年和月,格式为年\\月。 输出

输出该月的天数。 示例输入 2009\\1 示例输出 31

#include void main() { int y,m; scanf(\ switch(m) { case 1 : printf(\ case 2 : { if((y%4==0&&y0!=0)||(y@0==0)) printf(\ else printf(\ break; } case 3 : printf(\ case 4 : printf(\ case 5 : printf(\ case 6 : printf(\ case 7 : printf(\ case 8 : printf(\ case 9 : printf(\ case 10 : printf(\ case 11 : printf(\ case 12 : printf(\ } }

期末考试之分等级

Time Limit: 1000MS Memory limit: 65536K

题目描述

本文来源:https://www.bwwdw.com/article/qjcr.html

Top