2,0,0,0,3,0,0,9を長さエンコードして2 1 0 3 3 1 2 0 9 1にしないでください。
1)最初に間違っているのは、配列全体を見ていないということです。 <を使用して8の前に停止しますが、7で停止するので、配列項目0〜6のみを評価します。
2)ctがカウントを表す場合、リセットされません(宣言時にのみct = 0)。また、割り当てはこれです:a [flag] = ct;元のデータを上書きします。それは基本的にiの値を追跡します。
これは私がちょうど一緒に入れている私のバージョンです:
#define SZ 8
main()
{
int a[SZ]={2,0,0,0,3,0,0,9};
int i; //absolute position
int zero_count = 0; //target zeros specifically...
for (i = 0; i < SZ; i++) {
if (a[i] == 0)
zero_count++;
}
//now write it out in a bizarre, unparsable format again...
for (i = 0; i < SZ; i++) {
if (a[i] != 0) //write out all non zero values
printf("%d ", a[i]);
if (i == 0) { //this says put the zero count after the first number was printed
printf("%d 0 ", zero_count); //inserting it into a strange place in the array
}
}
return 0;
}
:出力はコメントに基づいて[OK]をあなたのアルゴリズムは、実際にこのようになり、下の左
2 1 0 3 3 1 0 2 9 1
ある
#define SZ 8
main()
{
int a[SZ]={2,0,0,0,3,0,0,9};
int i; //absolute position
int runningCount = 1; //because we start at array index 1 and not zero
for (i = 1; i <= SZ; i++) {
if (a[i - 1] == a[i]) //value same as one before it...
runningCount++;
else { // new value found. print last one, and the count of the last one.
printf("%d %d ", a[i - 1], runningCount);
runningCount = 1; //reset for next loop
}
}
return 0;
}
出力:2 5 0 3 9
これまでのところ、121の質問がありましたが、まだ*あなたのコードをフォーマットすることを学んだことはありませんか? –
私のコードをフォーマットするにはどうすればいいですか –
各行の前に4つのスペースを挿入してください。あなたはNotePad ++でこれを行うことができます。コードを貼り付けてすべてを選択し、タブをタップして貼り付けてSO –