zoukankan      html  css  js  c++  java
  • 《N诺机试指南》(八)日期、字符串、排序问题

    1.日期问题:

    输入:

    例题:

    代码:

    #include <stdio.h>
    #include <bits/stdc++.h>
    struct node{
    	int year, mouth, day;
    }p;
    int f[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    int main(){
    	while( scanf("%d%d%d", &p.year, &p.mouth, &p.day)!=EOF ){
    		//1.首先判断是不是闰年
    		if( (p.year%400==0)||(p.year%4==0 && p.year%100!=0) ){
    			f[2] = 29;//是闰年,二月为29天 
    		}else{
    			f[2] = 28;//不是闰年 
    		}
    		//2.判断输入的年月日是否合法
    		int flag = 0; 
    		//2.1.判断月份
    		if( p.mouth<1 || p.mouth>12 ){
    			flag = 1;
    		}
    		//2.2.判断日 
    		for( int i=1; i<=12; i++ ){
    			if( p.day<1 || p.day>f[i] ){
    				flag = 1;
    			}
    		}
    		if( flag ){
    			printf("Input Error
    ");
    		}
    		//3.计算当前日子是第多少天
    		int sum = 0;
    		int nowDay = p.day;
    		for( int i=1; i<p.mouth; i++ ){
    			sum += f[i];
    		}
    		sum += nowDay;
    		printf("%d
    ", sum); 	
    	}
    	return 0;
    } 

    注意点:

    怎么判断闰年: 

    (p.year%400==0)或者(p.year%4==0 && p.year%100!=0) 

    2.字符串问题:

    例题:

    代码:

    #include <stdio.h>
    #include <string.h>
    #include <bits/stdc++.h>
    
    int main(){
    	char s[105];
    	gets(s);//输入一行文本
    	int len = strlen(s); 
    	for(int i=0; i<len; i++){
    		//大写字母or小写字母 
    		if(s[i]>='A' && s[i]<='Z'){
    			s[i] += 3;
    		}else if(s[i]>='a' && s[i]<='z'){
    			s[i] += 3;
    		}else{
    		//其他的不处理 
    			continue;
    		}
    	}
    	puts(s);//输出一行文本
    	return 0;
    } 
    

    3.排序问题:

    sort函数:

    例题:

    解析:

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    /* 
    //问题一:稳定排序  
    struct Student{
    	string name;
    	int score, id;
    }stu[1005];
    
    //自定义:从小到大函数 
    bool FromShortToBig(Student a, Student b){
    	if( a.score==b.score ){
    		return a.id < b.id;//一样大,根据id判断 
    	}
    	return a.score < b.score;//哪个小哪个在前面 
    } 
    //自定义:从大到小函数 
    bool FromBigToShort(Student a, Student b){
    	if( a.score==b.score ){
    		return a.id < b.id;//一样大,根据id判断 
    	}
    	return a.score > b.score;//哪个大哪个在前面 
    } 
    
    int main(){
    	int n, model;
    	cin >> n;
    	cin >> model;
    	//输入数据 
    	for(int i=0; i<n; i++){
    		cin >> stu[i].name >> stu[i].score;
    		stu[i].id = i;//给id赋值 
    	}
    	//判断哪种模式 
    	if( model==0 ){
    		sort(stu, stu+n, FromBigToShort);
    	}else{
    		sort(stu, stu+n, FromShortToBig);
    	}
    	//打印 
    	for(int i=0; i<n; i++){
    		cout << stu[i].name << " " << stu[i].score << endl;
    	}
    	return 0;
    }
    */
    
    //问题二:先奇后偶、再按从小到大顺序排序
    bool cmp(int a, int b){
    	if( a%2==b%2 ){//同为奇数or偶数:按从小到大顺序排序 
    		return a < b;
    	}else{
    		return a%2 > b%2;//不同:按先奇后偶顺序排序 
    	}
    } 
    
    int main(){
    	int n;
    	cin >> n;
    	int a[n];
    	for(int i=0; i<n; i++){
    		cin >> a[i];
    	}
    	sort(a, a+n, cmp);
    	for(int i=0; i<n; i++){
    		cout << a[i] << " ";
    	}
    	cout << endl;
    	return 0;
    }
  • 相关阅读:
    mysql数据库优化课程---3、数据库设计是什么
    mysql数据库优化课程---2、命令其实也就是那几个单词
    mysql数据库优化课程---1、数据库的本质是什么
    php特级课---4、网站服务监控(常用网站服务监控软件有哪些)
    php特级课---5、网络数据转发原理
    php特级课---3、常用的网站加速技术有哪些
    php特级课---2、网站大数据如何存储
    php特级课---1、网站大访问量如何解决
    网络工程师课程---7、网络通信综合实验(做网络基础综合实验 用什么软件)
    Objective-C路成魔【2-Objective-C 规划】
  • 原文地址:https://www.cnblogs.com/Whgy/p/12389366.html
Copyright © 2011-2022 走看看