| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 432 人关注过本帖
标题:求为啥会错了!
只看楼主 加入收藏
zyz0811
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2023-5-21
收藏
 问题点数:0 回复次数:2 
求为啥会错了!
问题 A: 日历输出
内存限制:512 MB
时间限制:1.000 S
评测方式:文本比较
命题人:外部导入
提交:217
解决:34
题目描述
已知2007年1月1日为星期一。
设计一函数按照下述格式打印2007年以后(含)某年某月的日历,2007年以前的拒绝打印。
为完成此函数,设计必要的辅助函数可能也是必要的。其中输入为年份和月份。
比如输入2007 1
则输出为
---------------------
 Su Mo Tu We Th Fr Sa
---------------------
     1  2  3  4  5  6
  7  8  9 10 11 12 13
 14 15 16 17 18 19 20
 21 22 23 24 25 26 27
 28 29 30 31
---------------------


输入
输入数据包含多组,每组有两个整数m,n。m(m > 2007)表示年份,n表示月份,输入数据保证合法。
输出
对于每组输出数据,按照题目中的格式输出,日历中每个数字占三列。
样例输入 复制
2010 9
样例输出 复制
---------------------
 Su Mo Tu We Th Fr Sa
---------------------
           1  2  3  4
  5  6  7  8  9 10 11
 12 13 14 15 16 17 18
 19 20 21 22 23 24 25
 26 27 28 29 30
---------------------


提示
短线“-”个数要与题目中一致,否则系统会判为错误。输出时每个数字占三列,再强调一次。
#include<bits/stdc++.h>
using namespace std;
int yy[9999] = {0};
int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int pdrn(int s) {
    if (s % 4 == 0 && s % 100 != 0) {
        return 1;
    }
}
int ms(int g) {
    int sss = 0;
    if (g == 1) {
        return 31;
    } else {
        int sss = 31;
        sss += m[g];
        g--;
    }
    return sss;
}
long long nts(int n) {
    long long sum = 0;
    for (int i = 2007; i <= n - 1; i++) {
        sum += yy[i];
    }
    return sum;
}
int main() {
    int n, y;
    cin >> n >> y;

    if (pdrn(n)) {
        m[2 + 1] = 29;
    }
    for (int i = 2007; i <= n - 1; i++) {
        if (pdrn(i) == 1) {
            yy[i] = 366;
        } else {
            yy[i] = 365;
        }
    }
    int ks = (ms(y) + nts(n)) % 7;
    cout << "---------------------" << endl;
    cout << "Su Mo Tu We Th Fr Sa" << endl;
    cout << "---------------------" << endl;
    for (int i = 1; i <= ks; i++) {
        cout <<"   ";
    }
    int f = 0;
    for (int i = 1; i <= 7 -  ks; i++) {
        //cout <<i<<"  ";
        printf("%3d",i);
        f = i;
    }
    cout<<endl;
    for (int i = f + 1; i <= m[y] - f; i++) {
        //cout << i <<"  ";
        printf("%3d",i);
        if ((i - f) % 7 == 0) {
            cout << endl;
        }
    }
    return 0;
}
搜索更多相关主题的帖子: cout 输出 输入 for int 
2023-05-21 20:08
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:216
帖 子:1479
专家分:9035
注 册:2010-3-16
收藏
得分:0 
你这第一个闰年判断函数就不合适
简单修改了下,供参考
程序代码:


#include <bits/stdc++.h>
using namespace std;

// int yy[9999] = {0};
int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int pdrn(int s)
{
    if ((s % 4 == 0 && s % 100 != 0) || s % 400 == 0)
    {
        return 1;
    }
    return 0; //
}
int ms(int g)
{
    int sss = 0;
    for (int i = 1; i < g; i++)
    {
        sss += m[i];
    }
    return sss;
}
long long nts(int n)
{
    long long sum = 0;
    for (int i = 2007; i < n; i++)
    {
        sum += 365 + pdrn(i);
    }
    return sum;
}
int main()
{
    int n = 2023, y = 5;
    cin >> n >> y;
    if (n < 2007)
    {
        cout << "年度不能小于2007" << endl;
        return 1;
    }
    if (y < 1 || y > 12)
    {
        cout << "非法月份" << endl;
        return 2;
    }

    int day = m[y];

    if (pdrn(n) && y == 2)
        day += 1;

    int ks = (ms(y) + nts(n)) % 7 + 1;
    cout << "---------------------" << endl;
    cout << "Su Mo Tu We Th Fr Sa" << endl;
    cout << "---------------------" << endl;
    for (int i = 0; i < ks; i++)
    {
        cout << "   ";
    }
    int f = 7 - ks;
    for (int i = 1; i <= day; i++)
    {
        cout << setw(3) << i;
        if ((i - f) % 7 == 0)
        {
            cout << endl;
        }
    }
    return 0;
}



[此贴子已经被作者于2023-5-22 11:30编辑过]

2023-05-22 10:25
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
凑热闹,我用 C++ 写一个

程序代码:
#include <iostream>
#include <string>
#include <chrono>
using namespace std;

std::string foo( unsigned y, unsigned m )
{
    std::string s = "---------------------\n"
                    "Su Mo Tu We Th Fr Sa\n"
                    "---------------------\n";

    using namespace std::chrono;
    unsigned w = weekday{ year(y)/month(m)/day(1) }.c_encoding();
    unsigned q = unsigned( (year(y)/month(m)/last).day() );
    s.append( 3*w, ' ' );
    for( unsigned i=1; i!=q+1; ++i )
    {
        s += i/10 ? i/10+'0' : ' ';
        s += i%10+'0';
        s += (i+w)%7 ? ' ' : '\n';
    }
    return s;
}

int main( void )
{
    unsigned y, m;
    cin >> y >> m;
    cout << foo(y,m) << endl;
}
2023-05-22 12:42
快速回复:求为啥会错了!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017298 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved