Exemplo n.º 1
0
int main(void) {
    int n, Sum;
    char AddChoice;
    printf("1+2+...+n=?請輸入n=");
    scanf("%d", &n);
    fflush(stdin);
    printf("請問要做奇數和(O),偶數和(E),還是整數和(I)?請選擇:");
    scanf("%c", &AddChoice);

    switch (AddChoice) {
    case 'O':
        Sum = Odd(n);
        break;

    case 'E':
        Sum = Even(n);
        break;

    case 'I':
        Sum = TotalSum(n);
        break;

    default:
        printf("選擇錯誤\n");
        return -1;
    }

    printf("總和為%d\n", Sum);
    /*  system("pause");  */
}
Exemplo n.º 2
0
void partitionEven(int value)
{
  printf("partitionEven %d\n", value);
  int * arr = malloc(value * sizeof(int));
  int ind = 0;
  Even(value,arr,ind);
  free(arr);
}
void Twinkle::step(uint8_t delta)
{
  //A fast flasher which alternates LEDs
  //TODO: base this on the time delta
  //This should flash at a fast rate to work
  
  uint8_t i;
  for(i=0;i<group.length;++i)
  {
    if(even && Even(i))
    {
      set_led(group.leds[i],true);
    }
  }
  even = !even;
}
Exemplo n.º 4
0
/*
 * =================================================================
 * This function prints even number only partitions of a positive integer value
 * For example, if value is 8
 * 2 + 2 + 2 + 2and
 * 2 + 4 + 2 are valid partitions
 *
 * 8 is a valid partition
 *
 * 2 + 1 + 1 + 2 + 2and
 * 2 + 1 + 2 + 3and
 * 5 + 3 are invalid partitions.
 *
 * if the value is 5, there will be no result generated
 * 
 * The program should generate only valid partitions.  Do not
 * generates invalid partitions and checks validity before printing.
 */
void Even(int n,int * arr,int ind)
{
  int val;
  if(n == 0)
  {
  partprint(arr,ind);
  return;
  }

  for(val=1;val<=n;val++)
  {
    arr[ind]=val;
    if(arr[ind]%2==0)
      {
    Even(n-val,arr,ind+1);
      }
  }
}
Exemplo n.º 5
0
int main() 
{
	int ia[] = {0,1,1,2,3,5,8,13,21,34};
	list< int,allocator > ilist( ia, ia+10 );
		
        /*
         * unsupported in current implementation
         *****************************************************
	 typedef 
	    iterator_traits<InputIterator>::distance_type
	    distance_type;
	
	    distance_type ia_count, list_count;
		
	    // count even elements: 4 
	    ia_count = count_if( &ia[0], &ia[10], Even() );
	    list_count = count_if( ilist.begin(), ilist_end(),
			           bind2nd(less<int>(),10) );
	  ******************************************************
	  */

	int ia_count = 0;
        count_if( &ia[0], &ia[10], Even(), ia_count );

	// generates: 
	//   count_if(): there are 4 elements that are even.

	cout << "count_if(): there are "
	     << ia_count << " elements that are even.\n";

	int list_count = 0;
        count_if( ilist.begin(), ilist.end(),
	          bind2nd(less<int>(),10), list_count );


	// generates: 
	//   count_if(): there are 7 elements that are less than 10.

	cout << "count_if(): there are "
	     << list_count 
	     << " elements that are less than 10.\n";
		
	return 0;
}
Exemplo n.º 6
0
int TotalSum(int U) {
    return Odd(U) + Even(U);
}