2011年8月3日

在某个范围内寻找最大的f(n)=n 其中f(n)为计算小于等于n的数值中含有“1”的总和

摘要: #include<iostream>using namespace std;pair<int,int> mypair;int count_1(int n) //统计一个数值中含有“1”的个数{int sum=0;while(n!=0){if(n%10==1){sum++;}n/=10;}return sum;}int f(int n) //<=n内的所有数值含有“1”的个数的总和{int sum=0;while(n){sum+=count_1(n);n--;}return sum;}int main(){int maxcount=0;int count=0;int 阅读全文

posted @ 2011-08-03 21:32 原来... 阅读(526) 评论(0) 推荐(0) 编辑

求一个字符串中连续出现次数最多的子串

摘要: vector向量中的插入值如上图所示:#include <iostream>#include <vector>#include<string>using namespace std;pair<int ,string> fun(const string &str){vector<string> substrs;int maxcount=1,count=1;string substr;int i,len=str.length();for(i=0;i<len;i++){substrs.push_back(str.substr( 阅读全文

posted @ 2011-08-03 19:42 原来... 阅读(3888) 评论(0) 推荐(1) 编辑

约瑟夫问题

摘要: #include<iostream>using namespace std;typedef struct student{ int data; struct student *next;}node;void JOSEPHUS(int k,int m) //创建单链表{ node *head,*p,*s; int x,cycle=1; head=(node *)malloc(sizeof(node)); p=head; int n=0; while(cycle) { cout<<"请输入一直不为0的数,输入0则结束"<<endl; cin& 阅读全文

posted @ 2011-08-03 14:29 原来... 阅读(334) 评论(0) 推荐(0) 编辑

对 带头结点的单链表 的操作

摘要: //带头结点的单链表#include<iostream>using namespace std;typedef struct student{ int data; struct student *next;}node;node * creat() //创建单链表{ node *head,*p,*s; int x,cycle=1; head=(node *)malloc(sizeof(node)); p=head; while(cycle) { cout<<"请输入一直不为0的数,输入0则结束"<<endl; cin>>x; i 阅读全文

posted @ 2011-08-03 14:26 原来... 阅读(1044) 评论(1) 推荐(0) 编辑

malloc的内存分配之 malloc(0)的内存分配情况

摘要: #include<iostream>using namespace std;int main(){ char *p; if((p=(char *)malloc(0))==NULL) puts("got a null pointer"); else puts("got a valid pointer");}答案:got a valid pointer首先: 在标准的malloc实现中,并不检查输入值的大小,而是将输入值做对齐操作后直接从堆上分配空间。其次: 不论输入值的大小为多少,在malloc的内部最小的内存分配大小是一个定值(一般是8B), 阅读全文

posted @ 2011-08-03 14:24 原来... 阅读(9383) 评论(0) 推荐(1) 编辑

导航