目录
注意点
局部变量与全局变量
1
在{}的代码块里面定义的变量就叫局部变量,一个局部变量必须要初始化否则就是一个随机值
脱离代码块的就叫全局变量,倘若局部变量与全局变量冲突时以局部变量为主,若全局变量为初始化,默认为0
#include<stdio.h>
int b=20;//全局变量
int main()
{
int a=0;//局部变量
return 0;
}
#include<stdio.h>
int a=20;
int main()
{
int a=10;
printf("%d",a);//打出的是局部变量10而非20;
return 0;
}
#include<stdio.h>
int a;
int main()
{
printf("%d",a);//0
return 0;
}
数组
#include<stdio.h>
int main()
{
int arr[n];//[]里面不可以是个未知变量,应该是一个常量
return 0;
}
#include<stdio.h>
int main()
{
int *arr=(int *)malloc(10*sizeof(int));//使用动态开辟的数组
return 0;
}
练习题
题1
法1
本题要求编写程序计算某年某月某日是该年中的第几天。
输入格式:
输入在一行中按照格式“yyyy/mm/dd”(即“年/月/日”)给出日期。注意:闰年的判别条件是该年年份能被4整除但不能被100整除、或者能被400整除。闰年的2月有29天。
输出格式:
在一行输出日期是该年中的第几天。
输入样例1:
2009/03/02
结尾无空行
输出样例1:
61
结尾无空行
输入样例2:
2000/03/02
输出样例2:
62
#include<stdio.h>
int main()
{
int day;
int year, month, date;
scanf("%d/%d/%d", &year, &month, &date);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
if (month >= 3)
{
if (month == 3)
{
day = 31 + 29 + date;
printf("%d", day);
}
if (month == 4)
{
day = 31 + 29 + 31 + date;
printf("%d", day);
}
if (month == 5)
{
day = 31 + 29 + 31 + 30 + date;
printf("%d", day);
}
if (month == 6)
{
day = 31 + 29 + 31 + 30 + 31 + date;
printf("%d", day);
}
if (month == 7)
{
day = 31 + 29 + 31 + 30 + 31 + 30 + date;
printf("%d", day);
}
if (month == 8)
{
day = 31 + 29 + 31 + 30 + 31 + 30 + 31 + date;
printf("%d", day);
}
if (month == 9)
{
day = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + date;
printf("%d", day);
}
if (month == 10)
{
day = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date;
printf("%d", day);
}
if (month == 11)
{
day = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date;
printf("%d", day);
}
if (month == 12)
{
day = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date;
printf("%d", day);
}
}
else
{
if (month == 1)
{
day = date;
printf("%d", day);
}
if (month == 2)
{
day = 31 + date;
printf("%d", day);
}
}
}
else
{
if (month == 1)
{
day = date;
printf("%d", day);
}
if (month == 2)
{
day = 31 + date;
printf("%d", day);
}
if (month == 3)
{
day = 31 + 28 + date;
printf("%d", day);
}
if (month == 4)
{
day = 31 + 28 + 31 + date;
printf("%d", day);
}
if (month == 5)
{
day = 31 + 28 + 31 + 30 + date;
printf("%d", day);
}
if (month == 6)
{
day = 31 + 28 + 31 + 30 + 31 + date;
printf("%d", day);
}
if (month == 7)
{
day = 31 + 28 + 31 + 30 + 31 + 30 + date;
printf("%d", day);
}
if (month == 8)
{
day = 31 + 28 + 31 + 30 + 31 + 30 + 31 + date;
printf("%d", day);
}
if (month == 9)
{
day = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + date;
printf("%d", day);
}
if (month == 10)
{
day = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date;
printf("%d", day);
}
if (month == 11)
{
day = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date;
printf("%d", day);
}
if (month == 12)
{
day = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date;
printf("%d", day);
}
}
return 0;
}
法2
#include<stdio.h>
int main()
{
int year, m, d, date;
int arr1[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int arr2[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
int i = 1;
int ret=0;
scanf("%d %d %d", &year, &m, &d);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
for (i = 1; i < m; i++)
{
ret += arr2[i];
}
date = ret + d;
}
else
{
for (i = 1; i < m; i++)
{
ret += arr1[i];
}
date = ret + d;
}
printf("%d", date);
return 0;
}
法3
#include<stdio.h>
int main()
{
int year, month, day;
int sum = 0, flag = 0;
scanf("%d/%d/%d", &year, &month, &day);
switch (month)
{
case 1: sum = 0; break;
case 2: sum = 31; break;
case 3: sum = 59; break;
case 4: sum = 90; break;
case 5: sum = 120; break;
case 6: sum = 151; break;
case 7: sum = 181; break;
case 8: sum = 212; break;
case 9: sum = 243; break;
case 10: sum = 273; break;
case 11: sum = 304; break;
default: sum = 334; break;
}
sum += day;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
flag = 1;
if (flag == 1 && month > 2)
sum++;
printf("%d", sum);
return 0;
}
我认为还可以用递归做,但我实力有限暂时没想出来 ,如果有人可以做出了欢迎在评论去留言
题2
某电视台的娱乐节目有个表演评审环节,每次安排两位艺人表演,他们的胜负由观众投票和 3 名评委投票两部分共同决定。规则为:如果一位艺人的观众票数高,且得到至少 1 名评委的认可,该艺人就胜出;或艺人的观众票数低,但得到全部评委的认可,也可以胜出。节目保证投票的观众人数为奇数,所以不存在平票的情况。本题就请你用程序判断谁是赢家。
输入格式:
输入第一行给出 2 个不超过 1000 的正整数 Pa 和 Pb,分别是艺人 a 和艺人 b 得到的观众票数。题目保证这两个数字不相等。随后第二行给出 3 名评委的投票结果。数字 0 代表投票给 a,数字 1 代表投票给 b,其间以一个空格分隔。
输出格式:
按以下格式输出赢家:
The winner is x: P1 + P2
其中
x
是代表赢家的字母,P1
是赢家得到的观众票数,P2
是赢家得到的评委票数。输入样例:
327 129 1 0 1
结尾无空行
输出样例:
The winner is a: 327 + 1
#include<stdio.h>
int main()
{
int Pa, Pb;
scanf("%d %d", &Pa, &Pb);
int n1, n2, n3;
scanf("%d %d %d", &n1, &n2, &n3);
int count = n1 + n2 + n3;
int sh;
if (count == 3)
{
printf("The winner is b: %d + %d", Pb, count);
}
if (count == 0)
{
printf("The winner is a: %d + %d", Pa, count);
}
if (count == 1)
{
sh = 3 - count;
if ((Pa + sh) >= (Pb + count))
{
printf("The winner is a: %d + %d", Pa, sh);
}
else
{
printf("The winner is b: %d + %d", Pb, count);
}
}
if (count == 2)
{
sh = 3 - count;
if ((Pa + sh) >= (Pb + count))
{
printf("The winner is a: %d + %d", Pa, sh);
}
else
{
printf("The winner is b: %d + %d", Pb, count);
}
}
return 0;
}