int tree::count2(treenode* a,char c ,int i){ if(a==NULL){ return 0; } int t,y; t=count2(a->left,c,i); if(a->person.getGender() == c){ t++; } y=count2(a->right,c,i); i = i+y+t; if(c=='F') a->num_woman = i; else if(c=='M') a->num_man = i; return i; }
int count(int n){ int res = 0; for(int i = 0; i <= n; i++){ res += count2(i); } return res; }
void TreeCtrlTestCase::CollapseExpandEvents() { wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(), wxTestableFrame); m_tree->CollapseAll(); EventCounter count(m_tree, wxEVT_COMMAND_TREE_ITEM_COLLAPSED); EventCounter count1(m_tree, wxEVT_COMMAND_TREE_ITEM_COLLAPSING); EventCounter count2(m_tree, wxEVT_COMMAND_TREE_ITEM_EXPANDED); EventCounter count3(m_tree, wxEVT_COMMAND_TREE_ITEM_EXPANDING); wxUIActionSimulator sim; wxRect pos; m_tree->GetBoundingRect(m_root, pos, true); // We move in slightly so we are not on the edge wxPoint point = m_tree->ClientToScreen(pos.GetPosition()) + wxPoint(4, 4); sim.MouseMove(point); wxYield(); sim.MouseDblClick(); wxYield(); CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_TREE_ITEM_EXPANDING)); CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_TREE_ITEM_EXPANDED)); sim.MouseDblClick(); wxYield(); CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_TREE_ITEM_COLLAPSING)); CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_TREE_ITEM_COLLAPSED)); }
// Driver program to test above function int main() { int i, j; int arr[] = {2,5,3,6}; int m = sizeof(arr)/sizeof(arr[0]); printf("%d \n", count(arr, m, 10)); printf("%d \n", count2(arr, m, 10)); //getchar(); return 0; }
void count1(int array[]) { int x=0;//承载整数输出 int i=7; int t; for(;i>=0;i--) { t=count2(i); x=x+t*array[i]; } printf("%d",x); }
bool IsAnagram(const std::string &s1, const std::string &s2) { if (s1.length() != s2.length()) return false; std::vector<int> count1(26, 0), count2(26, 0); for (int i = 0; i < s1.length(); i++) { count1[s1[i] - 'a']++; count2[s2[i] - 'a']++; } for (int i = 0; i < 26; i++) if (count1[i] != count2[i]) return false; return true; }
//在main()函数中调用count()函数 main() { int i; //调用count1()10次 cout<<"count1():"<<endl; for (i=1;i<=12;i++) cout<<count1()<<" "; cout<<endl; //调用count2()10次 cout<<"count2():"<<endl; for (i=1;i<=12;i++) cout<<count2()<<" "; cout<<endl; }
TEST(Pointer, SPtr) { static int count = 0; struct counter { counter() { ++count; } ~counter() { --count; } void nop() {} }; struct counter_inherit : counter {}; { util::sptr<counter> count0(new counter); ASSERT_TRUE(count0.not_nul()); ASSERT_TRUE((*count0).not_nul()); ASSERT_EQ(1, count); util::sref<counter> refcount0 = *count0; util::sptr<counter> count1(std::move(count0)); ASSERT_EQ(1, count); util::sref<counter> refcount1 = *count1; ASSERT_EQ(refcount0.id().str(), refcount1.id().str()); ASSERT_EQ(refcount0.id().str(), count1.id().str()); ASSERT_TRUE(count0.nul()); ASSERT_TRUE((*count0).nul()); util::sptr<counter_inherit> count2(new counter_inherit); count1 = std::move(count2); ASSERT_EQ(1, count); util::sptr<counter_inherit> count3(new counter_inherit); ASSERT_EQ(2, count); util::sptr<counter> count4(std::move(count3)); count4->nop(); } ASSERT_EQ(0, count); }
void ScopeGuardTestCase::BlockExitObj() { Counter count0(1), count1(2), count2(3); { wxON_BLOCK_EXIT_OBJ0(count0, Counter::Zero); wxON_BLOCK_EXIT_OBJ1(count1, Counter::Set, 17); wxON_BLOCK_EXIT_OBJ2(count2, Counter::Sum, 2, 3); CPPUNIT_ASSERT_EQUAL( 1, count0.GetCount() ); CPPUNIT_ASSERT_EQUAL( 2, count1.GetCount() ); CPPUNIT_ASSERT_EQUAL( 3, count2.GetCount() ); } CPPUNIT_ASSERT_EQUAL( 0, count0.GetCount() ); CPPUNIT_ASSERT_EQUAL( 17, count1.GetCount() ); CPPUNIT_ASSERT_EQUAL( 5, count2.GetCount() ); }
bool IsScramble1(const std::string &s1, const std::string &s2) { if (s1 == s2) return true; if (s1.length() != s2.length()) return false; std::vector<int> count1(26, 0), count2(26, 0); for (int i = 0; i < s1.length(); i++) { count1[s1[i] - 'a']++; count2[s2[i] - 'a']++; } for (int i = 0; i < 26; i++) if (count1[i] != count2[i]) return false; for (int i = 1; i < s1.length(); i++) { if (IsScramble1(s1.substr(0, i), s2.substr(0, i)) && IsScramble1(s1.substr(i), s2.substr(i))) return true; if (IsScramble1(s1.substr(0, i), s2.substr(s1.length() - i)) && IsScramble1(s1.substr(i), s2.substr(0, s1.length() - i))) return true; } return false; }
void ListCtrlTestCase::ColumnDrag() { wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(), wxTestableFrame); EventCounter count(m_list, wxEVT_COMMAND_LIST_COL_BEGIN_DRAG); EventCounter count1(m_list, wxEVT_COMMAND_LIST_COL_DRAGGING); EventCounter count2(m_list, wxEVT_COMMAND_LIST_COL_END_DRAG); m_list->InsertColumn(0, "Column 0"); m_list->InsertColumn(1, "Column 1"); m_list->InsertColumn(2, "Column 2"); m_list->Update(); m_list->SetFocus(); wxUIActionSimulator sim; wxPoint pt = m_list->ClientToScreen(wxPoint(m_list->GetColumnWidth(0), 5)); sim.MouseMove(pt); wxYield(); sim.MouseDown(); wxYield(); sim.MouseMove(pt.x + 50, pt.y); wxYield(); sim.MouseUp(); wxYield(); CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG)); CPPUNIT_ASSERT(frame->GetEventCount(wxEVT_COMMAND_LIST_COL_DRAGGING) > 0); CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_LIST_COL_END_DRAG)); m_list->ClearAll(); }
int main() { printf("7 has %d, should be 3\n", count(7)); printf("307(100110011) and 277(100010101) diff is %d, should be 3\n", count2(307, 277)); return 0; }
int main() { scanf("%d%d", &n, &m); for(int i = 1; i <= n; ++i) scanf("%d", a + i); for(sqn = 1; sqn * sqn < n; ++sqn); sqn = std::max(sqn, (n - 1) / 200 + 1); for(int i = 1, j = 1; i <= n; i += sqn, ++j) { int iLim = std::min(i + sqn - 1, n); fir[j] = i; for(int k = i; k <= iLim; ++k) pos[k] = j; } blk = pos[n]; fir[blk + 1] = n + 1; for(int i = 1; i <= blk; ++i) { memset(b[i] + 1, 0, blk * sizeof(int)); memset(c[i] + 1, 0, n * sizeof(int)); } for(int i = 1; i <= n; ++i) { int x = pos[i], v = a[i], y = pos[v]; ++b[x][y]; ++c[x][v]; p[1][i] = q[1][i] = i; } for(int i = 2; i <= blk; ++i) { for(int j = 1; j <= blk; ++j) b[i][j] += b[i - 1][j]; for(int j = 1; j <= n; ++j) c[i][j] += c[i - 1][j]; memcpy(p[i] + 1, p[1] + 1, n * sizeof(int)); memcpy(q[i] + 1, q[1] + 1, n * sizeof(int)); } while(m--) { int typ, L, R, A, B, pL, pR; scanf("%d%d%d%d", &typ, &L, &R, &A); pL = pos[L]; pR = pos[R]; if(typ == 1) { scanf("%d", &B); if(A == B) continue; int pA = pos[A], pB = pos[B]; if(pL == pR) { int dt = modify(L, R, pL, A, B); if(dt) for(int i = pL; i <= blk; ++i) { b[i][pA] -= dt; b[i][pB] += dt; c[i][A] -= dt; c[i][B] += dt; } } else { int dt = 0; for(int i = pL; i <= blk; ++i) { if(dt) { b[i][pA] -= dt; b[i][pB] += dt; c[i][A] -= dt; c[i][B] += dt; } if(i > pR) continue; int res = modify(std::max(L, fir[i]), std::min(R, fir[i + 1] - 1), i, A, B); if(res) { b[i][pA] -= res; b[i][pB] += res; c[i][A] -= res; c[i][B] += res; dt += res; } } } } else { int y, ans; memset(cnt + 1, 0, blk * sizeof(int)); if(fir[pL] < L || R < fir[pR + 1] - 1) { if(pL == pR) { count1(L, R, pL); ++pL; } else { if(fir[pL] < L) { count1(L, fir[pL + 1] - 1, pL); ++pL; } if(R < fir[pR + 1] - 1) { count1(fir[pR], R, pR); --pR; } } } for(y = 1; y <= blk; ++y) { int ctr = cnt[y] + b[pR][y] - b[pL - 1][y]; if(A <= ctr) break; A -= ctr; } pL = pos[L]; pR = pos[R]; _cnt = cnt - fir[y] + 1; memset(cnt + 1, 0, sqn * sizeof(int)); if(fir[pL] < L || R < fir[pR + 1] - 1) { if(pL == pR) { count2(L, R, pL, y); ++pL; } else { if(fir[pL] < L) { count2(L, fir[pL + 1] - 1, pL, y); ++pL; } if(R < fir[pR + 1] - 1) { count2(fir[pR], R, pR, y); --pR; } } } for(ans = fir[y]; ans < fir[y + 1]; ++ans) { int ctr = _cnt[ans] + c[pR][ans] - c[pL - 1][ans]; if(A <= ctr) break; A -= ctr; } printf("%d\n", ans); } } return 0; }
void calculatePath(long long **mat, int n, int mark, int x, int y){ long long dp[n][n][2]; char path[n][n][2],res[n+n+2]; int i,j,k; i=0; dp[0][i][0]=count2(mat[0][i]); dp[0][i][1]=count5(mat[0][i]); for(i=0;i<n;i++){ for(j=0;j<n;j++){ if(j==0 && i==0) continue; else if(j==0){ dp[i][0][0]=dp[i-1][0][0]+count2(mat[i][0]); dp[i][0][1]=dp[i-1][0][1]+count5(mat[i][0]); path[i][0][0]='D'; path[i][0][1]='D'; } else if(i==0){ dp[0][j][0]=dp[0][j-1][0]+count2(mat[0][j]); dp[0][j][1]=dp[0][j-1][1]+count5(mat[0][j]); path[0][j][0]='R'; path[0][j][1]='R'; } else{ dp[i][j][0]=(dp[i][j-1][0]>=dp[i-1][j][0]?dp[i-1][j][0]:dp[i][j-1][0]); path[i][j][0]=(dp[i][j-1][0]>=dp[i-1][j][0]?'D':'R'); dp[i][j][0]+=count2(mat[i][j]); dp[i][j][1]=(dp[i][j-1][1]>=dp[i-1][j][1]?dp[i-1][j][1]:dp[i][j-1][1]); path[i][j][1]=(dp[i][j-1][1]>=dp[i-1][j][1]?'D':'R'); dp[i][j][1]+=count5(mat[i][j]); } } } if(mark && min(dp[n-1][n-1][0],dp[n-1][n-1][1])>0){ printf("1\n"); for(k=0;k<x;k++){ printf("D"); } for(k=0;k<y;k++){ printf("R"); } for(k=0;k<n-y-1;k++){ printf("R"); } for(k=0;k<n-x-1;k++){ printf("D"); } } else{ printf("%lld\n",min(dp[n-1][n-1][0],dp[n-1][n-1][1])); int p=0; if(dp[n-1][n-1][0]>dp[n-1][n-1][1]){ p=1; } i=n-1; j=n-1; k=0; while(i!=0 && j!=0){ res[k]=path[i][j][p]; k++; if(path[i][j][p]=='D'){ i--; } else{ j--; } } while(i!=0){ res[k]='D'; k++; i--; } while(j!=0){ res[k]='R'; k++; j--; } res[k]='\0'; printf("%s\n",strrev(res)); } }
void ListBaseTestCase::ItemClick() { // FIXME: This test fail under wxGTK because we get 3 FOCUSED events and // 2 SELECTED ones instead of the one of each we expect for some // reason, this needs to be debugged as it may indicate a bug in the // generic wxListCtrl implementation. #if wxUSE_UIACTIONSIMULATOR && !defined(__WXGTK__) // FIXME: This test fails on MSW buildbot slaves although works fine on // development machine, no idea why. It seems to be a problem with // wxUIActionSimulator rather the wxListCtrl control itself however. if ( wxGetUserId().Lower().Matches("buildslave*") ) return; wxTestableFrame* frame = wxStaticCast(wxTheApp->GetTopWindow(), wxTestableFrame); wxListCtrl* const list = GetList(); list->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT, 60); list->InsertColumn(1, "Column 1", wxLIST_FORMAT_LEFT, 50); list->InsertColumn(2, "Column 2", wxLIST_FORMAT_LEFT, 40); list->InsertItem(0, "Item 0"); list->SetItem(0, 1, "first column"); list->SetItem(0, 2, "second column"); EventCounter count(list, wxEVT_COMMAND_LIST_ITEM_SELECTED); EventCounter count1(list, wxEVT_COMMAND_LIST_ITEM_FOCUSED); EventCounter count2(list, wxEVT_COMMAND_LIST_ITEM_ACTIVATED); EventCounter count3(list, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK); wxUIActionSimulator sim; wxRect pos; list->GetItemRect(0, pos); //We move in slightly so we are not on the edge wxPoint point = list->ClientToScreen(pos.GetPosition()) + wxPoint(2, 2); sim.MouseMove(point); wxYield(); sim.MouseClick(); wxYield(); sim.MouseDblClick(); wxYield(); sim.MouseClick(wxMOUSE_BTN_RIGHT); wxYield(); // when the first item was selected the focus changes to it, but not // on subsequent clicks CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_LIST_ITEM_FOCUSED)); CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_LIST_ITEM_SELECTED)); CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_LIST_ITEM_ACTIVATED)); CPPUNIT_ASSERT_EQUAL(1, frame->GetEventCount(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK)); //tidy up when we are finished list->ClearAll(); #endif // wxUSE_UIACTIONSIMULATOR }
double pulse_measure(unsigned BaseAddress) { #define TIMEOUTVAL 150000 unsigned firstcount = 0; unsigned secondcount = 0; unsigned previouscount = 0; unsigned time = 0; unsigned long timeout; unsigned one, two; OUTPORTB(BaseAddress + 3, 0xB4); // 1011 0100 ctr2 mode2 OUTPORTB(BaseAddress + 2, 0xFF); // write 65535 to counters OUTPORTB(BaseAddress + 2, 0xFF); OUTPORTB(BaseAddress + 3, 0x72); // 0111 0010 ctr1 mode2 DELAY(1); OUTPORTB(BaseAddress + 3, 0x70); // 0111 0000 ctr1 mode2 DELAY(1); OUTPORTB(BaseAddress + 3, 0x74); // 0111 0100 ctr1 mode2 OUTPORTB(BaseAddress + 1, 0xFF); // write 65535 to counters OUTPORTB(BaseAddress + 1, 0xFF); firstcount = total_count(BaseAddress); // read the counter timeout = 0; do { previouscount = firstcount; firstcount = total_count(BaseAddress); timeout++; } while ((firstcount != previouscount) && (timeout < TIMEOUTVAL)); // check to see if the counts are changing one = count2(BaseAddress); if (timeout < TIMEOUTVAL) timeout = 0; do { secondcount = total_count(BaseAddress); timeout++; } while ((firstcount == secondcount) && (timeout < TIMEOUTVAL)); // check to see if counts are stable if (timeout < TIMEOUTVAL) timeout = 0; do { previouscount = secondcount; DELAY(1); secondcount = total_count(BaseAddress); timeout++; } while ((secondcount != previouscount) && (timeout < TIMEOUTVAL)); // check to see if counts are changing two = count2(BaseAddress); if (timeout < TIMEOUTVAL) { time = firstcount - secondcount; return(time); // return the counts for the pulse } else return 0; }