|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
const int YEAR_PLUS = 2014;
int main(){
double pm[3][12] = {
{-1,-1, -1, -1, -1, -1, -1, 31.3, 35.5 , 58.7, 49.6, 55.5},//2014
{59.8, 54.9, 33.1, 38.2, 26.6, 20.5, 27.8, 38.5, 41.5, 44.7, 38.1, 41.5},//2015
{34.9, 36.4, 47.5, 37.9, 30.6, 23.4, 26.6, 34.3, -1, -1, -1, -1}//2016
}; //-1表示未收入
int year,month,maxYear,maxMonth,minYear,minMonth;
double max = 0,min = 100000;
double (*ptr)[12] = pm;
// 找出PM最值和对应月份
for (int i = 0; i <= 2; i++) {
for (int j = 0; j <= 12; j++) {
if(ptr[i][j] != -1){
max = ptr[i][j] > max ? ptr[maxYear = i][maxMonth = j] : max;
min = ptr[i][j] < min ? ptr[minYear = i][minMonth = j] : min;
}
}
}
//2015-1 max
//2015-6 min
printf("请输入年份和月份:");
scanf("%d-%d",&year,&month);
if(ptr[year - YEAR_PLUS][month - 1] != -1)
printf("该月的PM值为%2.1f\n", ptr[year - YEAR_PLUS][month - 1]);
else
printf("未收入!\n");
printf("PM2.5最大的月份是:%d年%d月,PM:%2.1f\nPM2.5最小的月份是:%d年%d月,PM:%2.1f", \
maxYear + YEAR_PLUS, maxMonth + 1, max, minYear + YEAR_PLUS, minMonth + 1, min);
return 0;
}
请输入年份和月份:2015-8
该月的PM值为38.5
PM2.5最大的月份是:2014年13月,PM:59.8
PM2.5最小的月份是:2016年13月,PM:0.0
Process finished with exit code 0
找出PM最值和对应月份那一块代码出问题了,但我找不出问题所在,拜托鱼油们了!
#include <stdio.h>
const int YEAR_PLUS = 2014;
int main() {
double pm[3][12] = {
{-1, -1, -1, -1, -1, -1, -1, 31.3, 35.5, 58.7, 49.6, 55.5}, // 2014
{59.8, 54.9, 33.1, 38.2, 26.6, 20.5, 27.8, 38.5, 41.5, 44.7, 38.1, 41.5}, // 2015
{34.9, 36.4, 47.5, 37.9, 30.6, 23.4, 26.6, 34.3, -1, -1, -1, -1} // 2016
}; // -1 表示未收入
int year, month, maxYear, maxMonth, minYear, minMonth;
double max = 0, min = 100000;
double(*ptr)[12] = pm;
// 找出PM最值和对应月份
//for(int i = 0; i <= 2; i++) { // 不能说错,但是不推荐这样写
for(int i = 0; i < 3; i++) {
//for(int j = 0; j <= 12; j++) { // 这是什么?
for(int j = 0; j < 12; j++) {
if(ptr[i][j] != -1) {
max = ptr[i][j] > max ? ptr[maxYear = i][maxMonth = j] : max;
min = ptr[i][j] < min ? ptr[minYear = i][minMonth = j] : min;
}
}
}
// 2015-1 max
// 2015-6 min
printf("请输入年份和月份:");
scanf("%d-%d", &year, &month);
if(ptr[year - YEAR_PLUS][month - 1] != -1)
printf("该月的PM值为%2.1f\n", ptr[year - YEAR_PLUS][month - 1]);
else
printf("未收入!\n");
printf("PM2.5最大的月份是:%d年%d月,PM:%2.1f\nPM2.5最小的月份是:%d年%d月,PM:%2.1f",
maxYear + YEAR_PLUS, maxMonth + 1, max, minYear + YEAR_PLUS, minMonth + 1, min);
puts("");
return 0;
}
|
|