方法一
#include<iostream>
#include<string>
using namespace std;
int main() {
int time,y,m,d;
cin >> time;
y=time/365;
m=time%365/30;
d=time%365%30;
printf("%d ano(s)\n",y);
printf("%d mes(es)\n",m);
printf("%d dia(s)\n",d);
return 0;
}
下面这个是根据钞票那一题大佬的循环思路写的,当然这个并没有简化,
虽然通过了,但是自己在写的过程中遇到了问题,所以求问
为啥这个输出如果用注释的那行内容的话会是乱码呢?
但是这样用cout输出字符串就没有问题
C++ 代码(正常输出)
#include<iostream>
#include<string>
using namespace std;
int main() {
int time, a[3] = { 365,30,1 };
string b[3] = { {"ano(s)"},{"mes(es)"},{"dia(s)"} };
cin >> time;
for (int i = 0; i < 3; i++) {
printf("%d ", time / a[i]);//printf("%d %s", time / a[i],b[i]);
cout << b[i] << endl;
time %= a[i];
}
return 0;
}
C++ 代码(英文乱码)
#include<iostream>
#include<string>
using namespace std;
int main() {
int time, a[3] = { 365,30,1 };
string b[3] = { {"ano(s)"},{"mes(es)"},{"dia(s)"} };
cin >> time;
for (int i = 0; i < 3; i++) {
printf("%d %s", time / a[i],b[i]);
time %= a[i];
}
return 0;
}