1.模拟法:记住某天是星期几(例如公元1月1日是星期一),然后一天天模拟计算
#include
using namespace std;
int whatday(int y,int m,int d){
int ans=0;
//计算过去当前年之后是星期几,从1月1日星期一开始计算
for(int i=1;i>y>>m>>d;
cout<>y;
cout<<"月"; cin>>m;
cout<<"日"; cin>>d;
if(m==1){
m=13;
y=y-1;
}
if(m==2){
m=14;
y=y-1;
}
w=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
cout<<"星期"<
小明为某机构设计了一个十字型的徽标(并非红十字会啊),如下所示:
..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..
对方同时也需要在电脑dos窗口中以字符的形式输出该标志,并能任意控制层数。
输入格式
一个正整数 n (n<30) 表示要求打印图形的层数。
输出格式
对应包围层数的该标志。
样例输入1
1
样例输出1
..$$$$$..
..$...$..
$$$.$.$$$
$...$...$
$.$$$$$.$
$...$...$
$$$.$.$$$
..$...$..
..$$$$$..
样例输入2
3
样例输出2
..$$$$$$$$$$$$$..
..$...........$..
$$$.$$$$$$$$$.$$$
$...$.......$...$
$.$$$.$$$$$.$$$.$
$.$...$...$...$.$
$.$.$$$.$.$$$.$.$
$.$.$...$...$.$.$
$.$.$.$$$$$.$.$.$
$.$.$...$...$.$.$
$.$.$$$.$.$$$.$.$
$.$...$...$...$.$
$.$$$.$$$$$.$$$.$
$...$.......$...$
$$$.$$$$$$$$$.$$$
..$...........$..
..$$$$$$$$$$$$$..
#include
using namespace std;
const int MAX=130; //需设置为大于5+4*30的数
bool map[MAX][MAX];
void print(int n)
{
//通过n计算出打印十字图的规格(N*N)
int N=5+4*n;
//首先标记最内层的十字架
for(int i=N/2-1;i<=N/2+3;i++)
map[N/2+1][i]=map[i][N/2+1]=true; //一横一竖
//然后从最外层到里层逐个标记十字架
for(int i=1;i<=n;i++)
{
//接下来标记四个边角部分
int x=2*i+1,y=2*i+1;
//左上边角
map[x][y]=map[x][y-1]=map[x-1][y]=true;
//右上边角
y=N-2*i;
map[x][y]=map[x][y+1]=map[x-1][y]=true;
//右下边角
x=N-2*i;
map[x][y]=map[x][y+1]=map[x+1][y]=true;
//左下边角
y=2*i+1;
map[x][y]=map[x][y-1]=map[x+1][y]=true;
//接下来标记十字架的非边角部位(即“四个墙壁”)
for(int j=2*i+1;j<=N-2*i;j++)
map[2*i-1][j]=map[j][N-2*(i-1)]=map[N-2*(i-1)][j]=map[j][2*i-1]=true;
}
//打印
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++)
if(map[i][j]) cout<<"$";
else cout<<".";
cout<>n;
print(n);
return 0;
}
小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在 1960 年 1 月 1 日至 2059 年 12 月 31 日。令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。
更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。
比如 02/03/04,可能是 2002 年 03 月 04 日、2004 年 02 月 03 日或 2004 年 03 月 02 日。
给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?
输出若干个不相同的日期,每个日期一行,格式是 “yyyy-MM-dd”。多个日期按从早到晚排列。
输入 |
---|
02/03/04 |
输出 |
---|
2002-03-04 |
2004-02-03 |
2004-03-02 |
#include
using namespace std;
int arr[13]= { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//记录每个月的天数
struct date {//日期的结构体
int year, month, day;
};
date da[3] = { 0 };//结构体数组
bool isLeap(int y) {
return ((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0)); //判断是否为闰年
}
void f(int a, int b, int c,int d)//判断日期是否合法同时存入数组中
{
if (a >= 60)
{
a += 1900;
}
else
{
a += 2000;
}
da[d].year = a;
if (isLeap(a)) arr[2] = 29;
if (b > 0 && b < 13 && c>0 && c <= arr[b]) {
da[d].month = b;
da[d].day = c;
}
}
bool bijiao(date a, date b)//sort排序函数对应的排序规则
{
if (a.year == b.year)
{
if (a.month == b.month)
{
return a.day < b.day;
}
else
{
return a.month < b.month;
}
}
else return a.year < b.year;
}
int main()
{
string s;
cin >> s;
int a, b, c;
a = stoi(s.substr(0, 2));
b = stoi(s.substr(3, 2));
c = stoi(s.substr(6, 2));
f(a, b, c, 0);//此处分别对三种情况进行判断
f(c, b, a, 1);
f(c, a, b, 2);
int arr1[3] = { 0 };
sort(da,da+3, bijiao);//将合法的日期排序
date dd = da[0];
for (int i = 1; i <= 2; i++)//去除相同的日期
{
if (da[i].year == dd.year&& da[i].month == dd.month && da[i].day == dd.day)
{
da[i] = { 0,0,0 };
}
else
{
dd = da[i];
}
}
for (int i = 0; i < 3; i++)//循环输出结果
{
if (da[i].day != 0)
{
cout << da[i].year << "-" << setw(2) << setfill('0') << da[i].month << "-" << setw(2) << setfill('0') << da[i].day << endl;
}
}
return 0;
}
48–'0'
65–'A'
97–'a'
int main(){
// char 转化为 ASCII 码
char c1='A';
cout<<(char)(c1+1)<
#include
using namespace std;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
/* string的一种用法
string str=string(字符的个数,字符);
第二个变量要写字符,而不是字符串,因为多个字符组合成字符串
*/
string space=string(n-i,' ');
string ch=string(2*i-1,'A'+i-1);
cout<
主要是知道string的这种用法即可
/*
输入:
F
输出:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
输入:
5
输出:
1
121
12321
1234321
123454321
*/
#include
using namespace std;
int main() {
char c;
cin>>c;
if(c>='A'&&c<='Z') {
for(int i=1; i<=c-'A'+1; i++) {
for(int j=1; j<=c-'A'+1-i; j++) {
cout<<" ";
}
for(int j=1; j<=i; j++) {
cout<<(char)('A'+j-1);
}
for(int j=i-1; j>=1; j--) {
cout<<(char)('A'+j-1);
}
cout<=1; j--) {
cout<<(char)('1'+j-1);
}
cout<
先正着输到最中间,然后保留那个数再倒着输
有一些日期,日期格式为“AA/BB/CCCC”。编程将日期从小到大排列。
输入一个整数N,代表输入日期的个数。接下来N行,输入N个格式为“AA/BB/CCCC”的日期
输出排序后的日期,一个日期占一行
示例1
输入
6
10/22/2003
02/12/2004
15/12/1999
12/31/2005
10/21/2003
11/30/2005
输出
15/12/1999
10/21/2003
10/22/2003
02/12/2004
11/30/2005
12/31/2005
#include
using namespace std;
class date{
public:
date(int year,int month,int day);
bool operator < (const date& d);
void print(){
cout<year=year;
this->month=month;
this->day=day;
}
bool date::operator < (const date& d){
if(this->yearyear>d.year){
return false;
}else{
if(this->monthmonth>d.month){
return false;
}else{
if(this->day a;
for(int i = 0; i>d>>c>>m>>c>>y;
date b(y,m,d);
a.push_back(b);
}
sort(a.begin(), a.end());
for(int i = 0 ; i < n ; ++i){
a[i].print();
}
}
这题的需要注意的地方在于输入和输出的问题
scanf("%d/%d/%d",&d,&m,&y) 像这样就可以满足题目的那种格式,scanf可以自定义格式
或者像我的题解那样单独再设个char c 来吸收'/'
输出格式——setw()只会影响它后面 一个 的输出