您好,这里的初学者需要您的帮助。我的C程序很好,并且只执行了不应使用任何种类的if语句的操作。我认为这样写起来会更容易,因此可以替换if语句。我一直在尝试替换if语句,但是现在卡住了。我可以使用什么代替if语句来产生相同的输出。
该程序应生成一个由30个随机整数组成的序列(介于0和9之间),然后向前和向后打印该序列。然后打印出一个数字,显示序列中0到9之间的每个数字出现了多少次。
这是输出
Here is a sequence of 30 random numbers between 0 and 9:
3 6 7 5 3 5 6 2 9 1 2 7 0 9 3 6 0 6 2 6 1 8 7 9 2 0 2 3 7 5
Printing them backwards, that's:
5 7 3 2 0 2 9 7 8 1 6 2 6 0 6 3 9 0 7 2 1 9 2 6 5 3 5 7 6 3
There were 3 0's
There were 2 1's
There were 5 2's
There were 4 3's
There were no 4's
There were 3 5's
There were 5 6's
There were 4 7's
There was only 1 8
There were 3 9's
这是我的C程序
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, array[30]={0}, count=0,check;
srand(time(NULL));
for(i=0;i<30;i++)
array[i]=rand()%10;
for(i=0;i<30;i++)
printf("%d ",array[i]);
printf("\n\n");
for(i=29;i>=0;i--)
printf("%d ",array[i]);
printf("\n\n");
for(i=0;i<30;i++){
check=array[i];
if(array[i]!=-1)
array[i]=-1;
if(check == -1)
continue;
count =1;
for(j=0;j<30;j++){
if((i==j) || (array[j]==-1))
continue;
if(check==array[j]){
count++;
array[j]=-1;
}
}
printf("There were %d %d's\n",count,check);
}
return 0;
}
答案 0 :(得分:4)
您将从注释中了解算法:
#include <stdio.h>
#include <stdlib.h>
//time.h is needed for time()
#include <time.h>
int main()
{
int i, array[30] = {0};
srand(time(NULL));
//generate and print 30 random numbers
for(i = 0; i < 30; i++){
array[i] = rand() % 10;
printf("%d ", array[i]);
}
puts("\n\n");
//print these numbers backwards
for(i = 29; i >= 0; i--)
printf("%d ",array[i]);
puts("\n\n");
// print out a count of how many times each number
// between 0 and 9 appeared in the sequence.
int count[10] = {0};
for(i = 0; i < 30; i++)
count[array[i]]++;
//output the count for each number
for(i = 0; i < 10; i++)
printf("There were %d %d's\n",count[i], i);
return 0;
}
输出:
9 2 3 9 8 4 3 8 1 3 6 4 3 2 5 3 2 3 0 1 9 0 3 5 1 3 3 8 2 0
0 2 8 3 3 1 5 3 0 9 1 0 3 2 3 5 2 3 4 6 3 1 8 3 4 8 9 3 2 9
There were 3 0's
There were 3 1's
There were 4 2's
There were 9 3's
There were 2 4's
There were 2 5's
There were 1 6's
There were 0 7's
There were 3 8's
There were 3 9's
答案 1 :(得分:0)
以下是在删除if
语句时简化原始来源的信息。在逻辑位置用作源代码语句一部分的某些地方,隐含if
语句。
例如,for(j = 0; j < 30 && match >= 0; j++)
有几个逻辑表达式,但是此语句中没有if
出现。逻辑表达式为j < 30
和match >= 0
,以及j < 30 && match >= 0
的完整表达式。
此示例在语句array[j] == match && ++count && (array[j] = -1);
中使用逻辑表达式和C编译器的评估短路行为(请参阅Wikipedia中的Short-circuit evaluation),以便逻辑表达式array[j] == match
的计算结果为如果为false,则该语句的其余部分将不会执行。
我们还依靠带有++count
的preincrement运算符来增加计数,然后采用结果值来检查它是否为假(零)或真(非零)。由于变量count
初始化为零,并且递增时将始终为非零,因此该语句中的下一个逻辑表达式将被计算为(array[j] = -1)
。我们将赋值语句放在括号内以强制执行求值顺序。我们希望为变量array[j]
分配值-1,然后在逻辑语句中使用结果。因为这是整个逻辑语句的最后一个逻辑表达式,所以它求值为假(零)还是真(非零)都没有关系,因为我们想要的是将-1赋值给数组的副作用元素。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i, array[30] = {0};
srand(time(NULL));
for(i = 0; i < 30; i++)
array[i] = rand() % 10;
for(i = 0; i < 30; i++)
printf("%d ", array[i]);
printf("\n\n");
for(i = 29; i >= 0; i--)
printf("%d ",array[i]);
printf("\n\n");
for(i = 0; i < 30; i++){
int j;
int count = 0;
int match = array[i];
for(j = 0; j < 30 && match >= 0; j++){
array[j] == match && ++count && (array[j] = -1);
// replaces the following if as value of count is
// tested after it is incremented so will always be nonzero.
// if (array[j] == match) {
// count++; array[j] = -1;
// }
}
// if this is a valid array element value we are trying to match
// then print the count and the value being matched. printf()
// is a function that returns an int indicating number of character written.
match >= 0 && printf("There were %d %d's\n", count, match);
}
return 0;
}
示例输出。
8 2 4 0 8 0 8 1 1 4 6 9 3 9 7 6 3 9 0 1 0 7 1 2 4 0 3 0 2 3
3 2 0 3 0 4 2 1 7 0 1 0 9 3 6 7 9 3 9 6 4 1 1 8 0 8 0 4 2 8
There were 3 8's
There were 3 2's
There were 3 4's
There were 6 0's
There were 4 1's
There were 2 6's
There were 3 9's
There were 4 3's
There were 2 7's