首页 新闻 会员 周边 捐助

算法设计问题,小白一个求大神带飞

0
悬赏园豆:50 [已解决问题] 解决于 2017-05-22 13:11

一、日期计算

语言:C

题目:输入一个日期,格式如:2010-10-24 ,判断这一天是这一年中的第几天。

输入:第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每行的输入数据都是一个按题目要求格式输入的日期。

输出:每组输入数据的输出占一行,输出判断出的天数n

样例输入:

3

2000-4-5

2001-5-4

2010-10-24

样例输出:

96

124

297

遥路漫漫的主页 遥路漫漫 | 初学一级 | 园豆:40
提问于:2017-05-17 10:58
< >
分享
最佳答案
0

正好之前给人做过这个题目.

重点是:一个每月天数数组.一个输入年份月份获取当月天数方法.然后就是循环加了.

int[] day4m=[31,28,31,30,31,30,31,31,30,31,30,31]

function int day4ym(int year,int m){if(m==2){判断闰年返回day4m[m-1]+1}else{return day4m[m-1];}}

然后就是循环月份的次数.把之前所有月份的天数加起来.再加上日数.就是结果.

收获园豆:50
吴瑞祥 | 高人七级 |园豆:29449 | 2017-05-17 11:02

大神,能不能给个完整代码,我好琢磨琢磨。

遥路漫漫 | 园豆:40 (初学一级) | 2017-05-17 11:10

@遥路漫漫: 

 function day4year( y, m, d ) {
        var day4m = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        if ( y < 0 || m < 0 || d < 0 ) {
            return -1;
        }
        if ( m > 12 ) {
            return -1;
        }
        if ( d > day4m[m] ) {
            return -1;
        }
        var rt = d;
        for ( var i = 0; i < m; i++ ) {
            rt += day4m[i];
        }
        if ( isLeapYear( y ) && m > 2 ) {
            rt += 1;
        }
        return rt;
    }
    function isLeapYear( y ) {
        return ( ( ( y % 4 ) == 0 ) && ( y % 100 ) != 0 ) || ( y % 400 == 0 );
    }

说实话.你问题大...

吴瑞祥 | 园豆:29449 (高人七级) | 2017-05-17 11:29

@吴瑞祥: 大神能在帮个忙吗? 

 

这里有java的代码 , 您能不能翻译成c代码?

package mec.lanqiao; import java.util.*; public class Main {                static char[] upper = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; // 大写数字                // 获取一个四位数的大写字符串,零的数目可以为多个               static String addZero(String t) {                               StringBuilder sb = new StringBuilder();                               int n1 = Integer.parseInt("" + t.charAt(0));                               int n2 = Integer.parseInt("" + t.charAt(1));                               int n3 = Integer.parseInt("" + t.charAt(2));                               int n4 = Integer.parseInt("" + t.charAt(3));                               if (n1 == 0) {                                              sb.append("零");                               } else {                                              sb.append(upper[n1] + "仟");                               }                               if (n2 == 0) {                                              sb.append("零");                               } else {                                              sb.append(upper[n2] + "佰");                               }                               if (n3 == 0) {                                              sb.append("零");                               } else {                                              sb.append(upper[n3] + "拾");                               }                               if (n4 != 0) {                                              sb.append(upper[n4]);                               }                               return sb.toString();               }                static void f(String num) {                               int len = num.length();                               if (len < 5) {                                              for (int i = 0; i < 4 - len; i++) {                                                             num = "0" + num;                                              }                                              String s = addZero(num);                                              // 去尾部的零                                              while (s.endsWith("零")) {                                                             s = s.substring(0, s.length() - 1);                                              }                                              // 去首部的零                                              while (s.startsWith("零")) {                                                             s = s.substring(1);                                              }                                              // 消除重复的零                                              s = s.replaceAll("零零零", "零");                                              s = s.replaceAll("零零", "零");                                              System.out.println(s);                               } else if (len < 9) {                                              String s, s1, s2;                                              s1 = num.substring(0, num.length() - 4);                                              int i = 4 - s1.length();                                              while (i-- > 0) {                                                             s1 = "0" + s1;                                              }                                              s1 = addZero(s1) + "万";                                              s2 = num.substring(num.length() - 4);                                              s2 = addZero(s2);                                              s = s1 + s2;                                              while (s.endsWith("零")) {                                                             s = s.substring(0, s.length() - 1);                                              }                                              while (s.startsWith("零")) {                                                             s = s.substring(1);                                              }                                              s = s.replaceAll("零零零", "零");                                              s = s.replaceAll("零零", "零");                                              System.out.println(s);                               } else {                                              String s, s1, s2, s3;                                              s1 = num.substring(0, num.length() - 8);                                              int i = 4 - s1.length();                                              while (i-- > 0) {                                                             s1 = "0" + s1;                                              }                                              s2 = num.substring(num.length() - 8, num.length() - 4);                                              s3 = num.substring(num.length() - 4);                                              s1 = addZero(s1) + "亿";                                              s2 = addZero(s2) + "万";                                              s3 = addZero(s3);                                              s = s1 + s2 + s3;                                              while (s.endsWith("零")) {                                                             s = s.substring(0, s.length() - 1);                                              }                                              while (s.startsWith("零")) {                                                             s = s.substring(1);                                              }                                              s = s.replaceAll("零零零", "零");                                              s = s.replaceAll("零零", "零");                                              System.out.println(s);                               }               }                public static void main(String[] args) {                               Scanner scan = new Scanner(System.in);                               String num = scan.next();                               scan.close();                               f(num);               }}
遥路漫漫 | 园豆:40 (初学一级) | 2017-05-21 22:35
其他回答(1)
0

#include <conio.h>
#include<stdio.h>
void main()
{
int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,j,k,y,m,d,b,n,sum;
int N=0;
int year[100];
int month[100];
int day[100];
scanf("%d",&N);
printf("日期输入格式:2010-6-9 \n");
for(i=0;i<N;i++)
scanf("%d-%d-%d",&year[i],&month[i],&day[i]);
for(j=0;j<N;j++)
{
sum=0;
for(i=1;i<month[j];i++)
sum=sum+days[i];
sum=sum+day[j];
if((year[j]%4==0||(year[j]%400==0&&year[j]%100!=0)) &&month[j]>2)
sum=sum+1;
printf("%d\n",sum);
}


getch();
}

遥路漫漫 | 园豆:40 (初学一级) | 2017-05-22 13:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册