我具有以以下格式打印时间的代码:00 01 02 03 04 05 06 07 08 09 10 11 ...仅是第一个数字(0)必须高于第二个数字(0)。下面是我有的
#include <iostream>
using namespace std;
void printArray (int arg[], int length) {
for (int n=0; n<length; ++n)
cout << arg[n] << ' ';
cout << '\n';
}
int main()
{
int ftime[99] = {0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,2};
int ttime[99] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0};
cout << "Time: ";printArray(ftime,21);
cout << " ";printArray(ttime, 21);
return 0;
}
现在打印出以下内容:
Time: 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
这正是我想要的,但是我需要一直执行到99,并且想知道是否有比我更简单的方法来初始化ftime和ttime数组。任何投入将不胜感激!
简单的循环就可以做到。
int j = -1;
for(unsigned int i = 0; i < 99; ++i) {
ttime[i] = i % 10;
if(i % 10 == 0) {
++j;
}
ftime[i] = j;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句