笔记:(虽然自己做得麻烦了,但是!还是学到了一些知识)
1. 类似于split的函数:strtok
(https://blog.youkuaiyun.com/SweeNeil/article/details/84787945 )
char *strtok(char s[], const char *delim);
strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delim中包含的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。
实例:(https://www.runoob.com/cprogramming/c-function-strtok.html)
#include <string.h>
#include <stdio.h>
int main () {
char str[80] = "This is - www.runoob.com - website";
const char s[2] = "-";
char *token;
/* 获取第一个子字符串 */
token = strtok(str, s);
/* 继续获取其他的子字符串 */
while( token != NULL ) {
printf( "%s\n", token );
token = strtok(NULL, s);
}
return(0);
}
2. 字符串与整型互转
(https://www.runoob.com/w3cnote/c-int2str.html)
2.1 整型转字符串 <stdlib.h> 头文件
//value: 要转换的整数,string: 转换后的字符串,radix: 转换进制数,如2,8,10,16 进制等。
char* itoa(int value,char*string,int radix);
实例:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int number = 123456;
char string[16];
itoa(number1,string,10);
return 0;
}
2.2 字符串转整型 <stdlib.h> 头文件
int atoi(const char *nptr); //nptr: 要转换的字符串
实例:
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("%d\n",atoi("123456"));
return 0;
}
2005
Problem Description 给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output 对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20
2006/3/12
Sample Output
20
71
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char input[10];
int daynum[12] = {31,0,31,30,31,30,31,31,30,31,30,31};
while(scanf("%s",&input)!=EOF){
char *token;
int year,month,day;
int daysum = 0;
token = strtok(input,"/");
year = atoi(token);
token = strtok(NULL,"/");
month = atoi(token);
token = strtok(NULL,"/");
day = atoi(token);
if((year%4==0&&year%100!=0)||(year%400==0)){
daynum[1]=29;
}
else{
daynum[1]=28;
}
int i;
for(i=0;i!=month-1;i++){
daysum = daysum+daynum[i];
}
daysum = daysum+day;
printf("%d\n",daysum);
}
return 0;
}
简便做法就是利用scanf函数的特性....
scanf("%d/%d/%d", &year, &month, &date)