#include <iostream>#include <cstring>using namespace std;int * count (const char * const s){ int *count = new int [10]; for(int j = 0;j < 10;j++) { count[j] = 0; for (int k = 0;k < strlen(s);k++) { if (s[k]-48 == j) { count[j]++; } } }return count;delete [] count;}int main(){ int * counts = count("0000"); for (int k = 0;k < 10;k++) { cout << counts[k] << " "; }return 0;}上面这段代码中包含int * count (const char * const s)重新设计函数,讲统计结果数组作为参数COUNTS传递给函数void count(const char * const s,int * counts,int size)
2 回答
眼眸繁星
TA贡献1873条经验 获得超9个赞
void count(const char* s, int* cnts,int size)
{
int i;
memset(cnts,0,sizeof(int)*10);
for (i=0;i<size;i++){if (s[i] >= '0' && s[i]<= '9'){cnts[s[i]-'0']++;}}
}
这样就可以了
当年话下
TA贡献1890条经验 获得超9个赞
void count( __in const char * const s,__out int * counts,__in int size) //size是const的大小
{
for(int j = 0;j < 10;j++)
{
counts[j] = 0;
for (int k = 0;k < size;k++)
{
if (s[k]-48 == j) counts[j]++;
}
}
}
添加回答
举报
0/150
提交
取消