SDUWH - 《高级程序设计语言(C)》 - 2020级 - 摸底

7-1 输出闰年 (20分)
输出21世纪中截止某个年份以来的所有闰年年份。注意:闰年的判别条件是该年年份能被4整除但不能被100整除、或者能被400整除。

输入格式:
输入在一行中给出21世纪的某个截止年份。

输出格式:
逐行输出满足条件的所有闰年年份,即每个年份占一行。输入若非21世纪的年份则输出"Invalid year!"。若不存在任何闰年,则输出“None”。

输入样例1:
2048
输出样例1:
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048
输入样例2:
2000
输出样例2:
Invalid year!

#include<stdio.h>
int main()
{
	int a,b,c=0;
	scanf("%d",&a);
	if(a<=2000||a>2100)
	   printf("Invalid year!");
	else
	{
		for(b=2001;b<=a;b++)
		{
			if((b%4==0&&b%100!=0)||b%400==0)
			{
				c++;
				printf("%d\n",b);
			}
		}
		if(0==c)
		printf("None");
	}
	return 0;
 } 

7-2 Calculating an expression (25分)
Given an mathematic expression, you suppose to print out the value of this expression.

Input Specification:
Given an mathematic expression, include 2 integer operands and 1 operator (+,-,*,/ ). No extra space allowed.

Output Specification:
Output the answer. If the operator is “/”, keep 2 decimal digits after point; else, output in integer.

Sample Input 1:
10+15
Sample Output 1:
25
Sample Input 2:
5/3
Sample Output 2:
1.67

#include <stdio.h>
int main()
{
    double a, b, c;
    char d;
    scanf("%lf %c %lf",&a, &d, &b);
    if(d=='+')
    {
    	c = a + b;
        printf("%.0f\n", c);
	}
    if(d=='-')
	{
    	c = a - b;
        printf("%.0f\n", c);
	}
    if(d=='*')
	{
		c = a / b;
        printf("%.2f\n", c);
    }
    if(d=='/')
    {
    	c = a / b;
        printf("%.2f\n", c);
	}
 
    return 0;
}
 

7-3 打印九九口诀表 (25分)
下面是一个完整的下三角九九口诀表:

11=1
1
2=2 22=4
1
3=3 23=6 33=9
14=4 24=8 34=12 44=16
15=5 25=10 35=15 45=20 55=25
1
6=6 26=12 36=18 46=24 56=30 66=36
1
7=7 27=14 37=21 47=28 57=35 67=42 77=49
18=8 28=16 38=24 48=32 58=40 68=48 78=56 88=64
19=9 29=18 39=27 49=36 59=45 69=54 79=63 89=72 9*9=81

本题要求对任意给定的一位正整数N,输出从11到NN的部分口诀表。

输入格式:
输入在一行中给出一个正整数N(1≤N≤9)。

输出格式:
输出下三角N*N部分口诀表,其中等号右边数字占4位、左对齐。

输入样例:
4
输出样例:
11=1
1
2=2 22=4
1
3=3 23=6 33=9
14=4 24=8 34=12 44=16

#include <stdio.h>
int main()
{
    int i,j,c,d,e;
    scanf("%d",&c);
    for(i=1;i<=c;i++)
    {
    	for(j=1;j<=i;j++)
    	{
    		e=i*j;
    		if(e<10)
    			printf("%d*%d=%d   ",j,i,e);
    		else
    			printf("%d*%d=%d  ",j,i,e);
		}
    	printf("\n");
	}
    return 0;
}
 

7-4 求N分之一序列前N项和 (25分)
本题要求编写程序,计算序列 1 + 1/2 + 1/3 + … 的前N项之和。

输入格式:
输入在一行中给出一个正整数N。

输出格式:
在一行中按照“sum = S”的格式输出部分和的值S,精确到小数点后6位。题目保证计算结果不超过双精度范围。

输入样例:
6
输出样例:
sum = 2.450000

#include <stdio.h>
int main()
{
    int a=1,b;
    double sum=0.0;
    scanf("%d",&b);
    for(;a<=b;a++)
    	sum=sum+(1.0/a);
    printf("sum = %.6f",sum);
    return 0;
}
 

7-5 1.1-3 Hello World 3 (5分)
本题要求输出下列文字三遍:

hello, world
输入格式:
本题无输入。

输出格式:
如题目所示要求,在三行中输出文字:

hello, world
输入样例:

输出样例:
hello, world
hello, world
hello, world

#include <stdio.h>
int main()
{
    printf("hello, world\n");
    printf("hello, world\n");
    printf("hello, world");
    return 0;
}
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值