首页 > 试题广场 >

出生日期输入输出

[编程题]出生日期输入输出
  • 热度指数:92910 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入一个人的出生日期(包括年月日),将该生日中的年、月、日分别输出。

数据范围:年份满足 ,月份满足 ,日满足

输入描述:
输入只有一行,出生日期,包括年月日,年月日之间的数字没有分隔符。


输出描述:
三行,第一行为出生年份,第二行为出生月份,第三行为出生日期。输出时如果月份或天数为1位数,需要在1位数前面补0。
示例1

输入

20130225 

输出

year=2013
month=02
date=25

备注:

通过scanf函数的%m格式控制可以指定输入域宽,输入数据域宽(列数),按此宽度截取所需数据;通过printf函数的%0格式控制符,输出数值时指定左面不使用的空位置自动填0。

import java.util.Scanner;

public class Main{

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        String year = s.substring(0, 4);
        String month = s.substring(4, 6);
        String days = s.substring(6, 8);
        System.out.println("year="+year);
        System.out.println("month="+month);
        System.out.println("date="+days);

    }


}
发表于 2020-03-22 13:47:37 回复(0)
import java.util.Scanner;

public class Main
{
    public static void main(String[] agrs)
    {
        Scanner in = new Scanner(System.in);
        String birth = in.nextLine();
        int year = Integer.parseInt(birth.substring(0,4));
        int month = Integer.parseInt(birth.substring(4,6));
        int day = Integer.parseInt(birth.substring(6,8));
        
        System.out.printf("year=%d\n", year);
        System.out.printf("month=%02d\n", month);
        System.out.printf("date=%02d\n", day);
    }
}
发表于 2021-01-07 20:31:52 回复(0)
s = input()
year = s[0:4]
month = s[4:6]
date = s[6:8]
print('year={}'.format(year))
print('month={}'.format(month))
print('date={}'.format(date))
发表于 2020-04-23 07:46:12 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String [] args)
    {
        Scanner sc = new Scanner(System.in);
        String birth = sc.nextLine();
        System.out.println("year="+birth.substring(0,4));
        System.out.println("month="+birth.substring(4,6));
        System.out.println("date="+birth.substring(6,8));
    }
}
发表于 2021-07-14 21:06:11 回复(0)
%m(所有类型),其中m为常数,限定输入范围,如scanf(“%4d”,&a)时输入123456,只把1234赋值给a
#include<iostream>
using namespace std;

int main()
{
    int date,year,month;
    scanf("%4d%2d%2d",&year,&month,&date);
    printf("year=%d\nmonth=%.2d\ndate=%.2d\n",year,month,date);
    return 0;
}


发表于 2020-07-27 17:16:26 回复(1)
#include <stdio.h>
int main()
{
    int birth=0;
    scanf("%d",&birth);
    int year=birth/10000;
    int date=birth%100;
    int month=birth%10000/100;
    printf("year=%d\n",year);
     printf("month=%02d\n",month);
     printf("date=%02d\n",date);
    return 0;
}

发表于 2021-02-10 20:48:49 回复(0)
#include<stdio.h>

int main()
{
    int i = 0;
    scanf("%d", &i);
    printf("year=%d\n", i/10000);
    printf("month=%02d\n", i/100%100);
    printf("date=%02d\n", i%100);
    return 0;
}
发表于 2021-10-06 21:18:54 回复(0)
a = input()
print('year='+a[:4])
print('month='+a[4:6])
print('date='+a[6:8])

单单解决题目的话没问题,可是感觉题目有bug,如果输入2021111
怎么判断后面的111是1月11 还是11月1,仅仅靠补0也不准确
发表于 2021-08-27 15:51:05 回复(2)
#include <stdio.h>
int main ()
{
    int  y,m,d;
    scanf("%4d%2d%2d",&y,&m,&d);
    printf("year=%04d\n",y);
    printf("month=%02d\n",m);
    printf("date=%02d",d);
}

发表于 2020-05-30 11:39:24 回复(3)
#include <stdio.h>

int main()
{
    int date;
    scanf("%d", &date);
    printf("year=%04d\nmonth=%02d\ndate=%02d\n", date / 10000, (date / 100) % 100, date % 100);
}

发表于 2020-04-05 13:27:13 回复(0)
#include<iostream>
(720)#include<string>
using namespace std;

int main(){
    string str;
    while(cin>>str){
        cout<<"year="<<str.substr(0,4)<<endl;
        cout<<"month="<<str.substr(4,2)<<endl;
        if(str.length()<8){
            cout<<"date=0"<<str.substr(6,1)<<endl;
        }else{
            cout<<"date="<<str.substr(6,2)<<endl;
        }
    }
    return 0;
}

发表于 2020-03-15 15:29:59 回复(1)
也可以通过截取字符串解决问题
#include <iostream>
#include <string>
using namespace std;

int main() {
    string n;
    cin>>n;
    cout<<"year="<<n.substr(0,4)
    <<"\nmonth="<<n.substr(4,2)
    <<"\ndate="<<n.substr(6,2);
    return 0;
}


发表于 2023-06-15 17:02:01 回复(0)
#include <stdio.h>

int main() {
    int a=0;
    scanf("%d",&a);
    printf("year=%d\n",a/10000);
    printf("month=%02d\n",(a/100)%100);
    printf("date=%02d\n",a%100);

    return 0;
}
编辑于 2024-01-09 20:16:02 回复(0)
#include<stdio.h>
int main(void){
    char year[10], month[5], date[5];
    scanf("%4s%2s%2s", year, month, date);
    
    printf("year=%s\nmonth=%s\ndate=%s", year, month, date);
    return 0;
}
这一题主要考察的地方在于月份为1-9的时候如何加上前置的0,天数也存在这样的问题,我这里采用的是将整个生日日期的输入切分为代表年、月、日的三个字符串,然后分别打印符合题目格式的这三个字符串就行了

如果不用字符串怎么办,使用数字很难直接达到year=2022  month=03   date=06这种效果,需要多层判断,目前技术水平不够,对于使用数字处理这一题【使用int类型去解此题】,只想到这种解决方案
发表于 2022-01-27 22:14:53 回复(0)
有没有大佬可以用字符串写啊,为啥我用gets()不对呢
发表于 2021-12-23 20:27:58 回复(1)
#include <iostream>
using namespace std;

int main() {
	int num;
	cin >> num;
	cout << "year=" << num / 10000 << endl;
	if (num / 100 % 100 < 10) {
		cout << "month=" << 0 << num / 100 % 100 << endl;
	}
	else {
		cout << "month=" << num / 100 % 100 << endl;
	}
	if (num % 100 < 10) {
		cout << "date=" << 0 << num % 100 << endl;
	}
	else {
		cout << "date=" << num % 100 << endl;
	}

	return 0;
}

发表于 2021-09-15 21:39:56 回复(0)
#include <stdio.h>
int main(){
    int num;
    scanf("%8d",&num);
    int year,month,date;
    year=num/10000;
    month=num/100%100000%10000%1000%100;
    date=num%1000000%100000%10000%1000%100;
    printf("year=%04d\n",year);
    printf("month=%02d\n",month);
    printf("date=%02d",date);   
}
发表于 2020-04-08 14:21:37 回复(0)
#include <stdio.h>
int main()
{
	int year = 0;
	int month = 0;
	int date = 0;
	//按照格式输入
	scanf("%4d%2d%2d", &year, &month, &date);
	printf("year=%4d\n", year);
	printf("month=%02d\n", month);
	printf("date=%02d\n", date);
	return 0;
}

编辑于 2024-04-12 15:28:01 回复(0)
#include <stdio.h>

int main() {
    int a;
    int b[4]={0};
    scanf("%d",&a);
    for(int i=0;i<4;i++){
        b[i]=a%10;
        a/=10;
    }
    printf("year=%d\nmonth=%d%d\ndate=%d%d",a,b[3],b[2],b[1],b[0]);
    return 0;
}
发表于 2022-10-26 14:52:13 回复(2)
#include<stdio.h>
int main()
{
    int year,month,date;
    scanf("%4d%2d%2d",&year,&month,&date);
    printf("year=%d\nmonth=%02d\ndate=%02d\n",year,month,date);
    return 0;
}

发表于 2022-09-03 08:27:22 回复(0)