在定义一个一维数组的同时,就可以给数组中的元素赋初值:
类型名 数组名[常量表达式] = {值,值,值……}
[ ]中的各数据值即为各元素的初值,值之间用逗号分隔。
如果没有赋值,只初始化前几个元素,那么后几个元素,其存储空间里的每个字节都被写入二进制数 0 。
定义数组时,如给全部元素赋值,则可以不给出数组元素的个数。
//确定星期几
#include<iostream>
#include<string>
using namespace std;
string weekdays[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
int main()
{
int n;
cin >> n;
if(n > 7 || n < 1)
cout << "Illegal";
else
cout << weekdays[n - 1];
return 0;
}
#include<iostream>
using namespace std;
int monthDays[13] = {-1,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
int year,month,date;
int days = 0;
cin >> year >> month >> date;
for(int y = 2012;y < year;++y){
if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
days += 366;
else
days += 365;
}
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
monthDays[2] = 29;
for(int m = 1;m < month;++m)
days += monthDays[m];
}
days += date;
days -= 22;
cout << days % 7 << endl;
}