先不说计数器
if(4x+3y+z/2==36&&x+y+z==100)
x+y+z不会超过9+12+36那么x+y+z==100不会成立,所以正确答案是0个,不用计数器了
改了下条件+了个计数器
#include<stdioh>
int main()
{
int x,y,z;
int cnt=0;
printf("Work\n");
for(x=1;x<9;x++)
for(y=1;y<12;y++)
for(z=2;z<36;z+=2)
if(4x+3y+z/2==36&&x+y+z<100)
{
cnt++;
printf("%2d: men:%d,women:%d,children:%d\n",cnt,x,y,z);
}
}
example-btn{color:#fff;background-color:#5cb85c;border-color:#4cae4c}example-btn:hover{color:#fff;background-color:#47a447;border-color:#398439}example-btn:active{background-image:none}divexample{width:98%;color:#000;background-color:#f6f4f0;background-color:#d0e69c;background-color:#dcecb5;background-color:#e5eecc;margin:0 0 5px 0;padding:5px;border:1px solid #d4d4d4;background-image:-webkit-linear-gradient(#fff,#e5eecc 100px);background-image:linear-gradient(#fff,#e5eecc 100px)}divexample_code{line-height:14em;width:98%;background-color:#fff;padding:5px;border:1px solid #d4d4d4;font-size:110%;font-family:Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;word-break:break-all;word-wrap:break-word}divexample_result{background-color:#fff;padding:4px;border:1px solid #d4d4d4;width:98%}divcode{width:98%;border:1px solid #d4d4d4;background-color:#f6f4f0;color:#444;padding:5px;margin:0}divcode div{font-size:110%}divcode div,divcode p,divexample_code p{font-family:"courier new"}pre{margin:15px auto;font:12px/20px Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;white-space:pre-wrap;word-break:break-all;word-wrap:break-word;border:1px solid #ddd;border-left-width:4px;padding:10px 15px} 排序算法是《数据结构与算法》中最基本的算法之一。排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存。常见的内部排序算法有:插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。以下是计数排序算法:
计数排序的核心在于将输入的数据值转化为键存储在额外开辟的数组空间中。作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范围的整数。
1 计数排序的特征
当输入的元素是 n 个 0 到 k 之间的整数时,它的运行时间是 Θ(n + k)。计数排序不是比较排序,排序的速度快于任何比较排序算法。
由于用来计数的数组C的长度取决于待排序数组中数据的范围(等于待排序数组的最大值与最小值的差加上1),这使得计数排序对于数据范围很大的数组,需要大量时间和内存。例如:计数排序是用来排序0到100之间的数字的最好的算法,但是它不适合按字母顺序排序人名。但是,计数排序可以用在基数排序中的算法来排序数据范围很大的数组。
通俗地理解,例如有 10 个年龄不同的人,统计出有 8 个人的年龄比 A 小,那 A 的年龄就排在第 9 位,用这个方法可以得到其他每个人的位置,也就排好了序。当然,年龄有重复时需要特殊处理(保证稳定性),这就是为什么最后要反向填充目标数组,以及将每个数字的统计减去 1 的原因。
算法的步骤如下:
(1)找出待排序的数组中最大和最小的元素 (2)统计数组中每个值为i的元素出现的次数,存入数组C的第i项 (3)对所有的计数累加(从C中的第一个元素开始,每一项和前一项相加) (4)反向填充目标数组:将每个元素i放在新数组的第C(i)项,每放一个元素就将C(i)减去1 2 动图演示
代码实现 JavaScript 实例 function countingSort ( arr , maxValue ) {
var bucket = new Array ( maxValue + 1 ) ,
sortedIndex = 0 ;
arrLen = arr length ,
bucketLen = maxValue + 1 ;
for ( var i = 0 ; i 0 :
arr [ sortedIndex ] = j
sortedIndex+ = 1
bucket [ j ] - = 1
return arr
Go 实例 func countingSort ( arr [] int , maxValue int ) [] int {
bucketLen := maxValue + 1
bucket := make ([] int , bucketLen ) // 初始为0的数组
sortedIndex := 0
length := len ( arr )
for i := 0 ; i < length ; i ++ {
bucket [ arr [ i ]] += 1
}
for j := 0 ; j < bucketLen ; j ++ {
for bucket [ j ] > 0 {
arr [ sortedIndex ] = j
sortedIndex += 1
bucket [ j ] -= 1
}
}
return arr
}
Java 实例 public class CountingSort implements IArraySort {
@Override
public int [ ] sort ( int [ ] sourceArray ) throws Exception {
// 对 arr 进行拷贝,不改变参数内容
int [ ] arr = Arrays copyOf ( sourceArray, sourceArray length ) ;
int maxValue = getMaxValue ( arr ) ;
return countingSort ( arr, maxValue ) ;
}
private int [ ] countingSort ( int [ ] arr, int maxValue ) {
int bucketLen = maxValue + 1 ;
int [ ] bucket = new int [ bucketLen ] ;
for ( int value : arr ) {
bucket [ value ] ++;
}
int sortedIndex = 0 ;
for ( int j = 0 ; j 0 ) {
arr [ sortedIndex ++ ] = j ;
bucket [ j ] --;
}
}
return arr ;
}
private int getMaxValue ( int [ ] arr ) {
int maxValue = arr [ 0 ] ;
for ( int value : arr ) {
if ( maxValue
#include<reg51h>
#define uchar unsigned char
#define uint unsigned int
uchar code ledtab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//0-9
uchar scanled;
uchar disdat[4];
uint ss,time;
sbit led=P1^0;
void dischg()
{
disdat[3]=ss%10;
disdat[2]=(ss/10)%10;
disdat[1]=(ss/100)%10;
disdat[0]=(ss/1000)%10;
}
void t0isr() interrupt 1
{
ss++;
dischg();
}
void t1isr() interrupt 3 //显示
{
TH1=(65536-5000)/256;
TL1=(65536-5000)%256;
P2=1<<scanled;
P0=~ledtab[disdat[scanled]];
scanled++;
scanled%=4;
time++;
if(time>100){time=0;led=~led;}
}
main()
{
TMOD=0x16;
TH0=0xff;
TL0=0xff;
TH1=(65536-5000)/256;
TL1=(65536-5000)%256;
TR1=1;
TR0=1;
ET0=1;
ET1=1;
EA=1;
led=0;
scanled=0;
ss=0;
dischg();
while(1);
}
以上就是关于c语言帮忙做个计数器全部的内容,包括:c语言帮忙做个计数器、c语言计数排序算法、基于51单片机的简易计数器设计c程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)