上一页 1 2 3 4 5 6 ··· 17 下一页
思路分析:intisdigit(charc)函数用来判断字符c是否为数字,当c为数字0~9时,返回非零值,否则返回零。使用时需要包含头文件。如果一个字符为数字,那输出它减去48之后的整数形式,即为这个数的值。代码如下:#include "stdafx.h"#include #include int main(){ int c; while ((c = getchar()) != EOF) { getchar(); if (isdigit(c)) printf("是:%d\n", c-48); else ... Read More
posted @ 2014-03-25 11:41 源子陌 Views(430) Comments(0) Diggs(0) Edit
方法一:递(sangxin)归(bingkuang)法,遍历字符串,每个字符只能取或不取。取该字符的话,就把该字符放到结果字符串中,遍历完毕后,输出结果字符串。代码如下:#include "stdafx.h"#include #include void CombineRecursiveImpl(const char* str, char* begin, char* end){ if (*str == 0) { *end = 0; if (begin != end) printf("%s", begin); return... Read More
posted @ 2014-03-25 10:23 源子陌 Views(1931) Comments(0) Diggs(0) Edit
思路分析:采用hash法来实现,首先申请一个长度为256的表,对每个字符hash计数即可。因为C/C++中的字符有3种类型:char、signedchar和unsignedchar。char类型的符号是由编译器指定的,一般是有符号的。在对字符进行hash时,应该先将字符转为无符号类型;否则当下标为负值时,就会出现越界访问。此外,还需要一个数组记录当前找到的只出现一次的字符,避免对原字符串进行第二次遍历。代码如下:#include "stdafx.h"#include char GetChar(char str[]){ if (str == NULL) return 0; . Read More
posted @ 2014-03-24 22:30 源子陌 Views(563) Comments(0) Diggs(0) Edit
方法一:先求出字符串长度,然后反向遍历。代码如下:#include "stdafx.h"#include void ReversePrint(const char* s){ int len = strlen(s); for (int i = len - 1; i >= 0; i--) printf("%c", s[i]); }int main(){ char a[] = "abcd"; ReversePrint(a); printf("\n"); getchar(); return 0;} 效果如图: 方法二 Read More
posted @ 2014-03-24 16:34 源子陌 Views(534) Comments(0) Diggs(0) Edit
思路分析:一共分两个步骤,第一步先按单词逆序,第二步将整个句子逆序。代码如下:#include "stdafx.h"#include void ReverseWord(char* p, char* q){ while (p < q) { char t = *p; *p = *q; *q = t; p++; q--; }}char* Reverse(char *s){ char *p = s; char *q = s; while (*q != '\0') { if (*... Read More
posted @ 2014-03-24 16:26 源子陌 Views(417) Comments(0) Diggs(0) Edit
方法一:普通逆序。直接分配一个与愿字符串登场的字符数组,然后反向拷贝即可。代码如下:#include "stdafx.h"#include char *Reverse(char *s){ char *q = s; while (*q)q++; q=q-1; char *p = new char[sizeof(char)*(q - s + 2)]; char *r = p; //逆序存储 while (q >= s) { *p = *q; p++; q--; } *p = '\0'; re... Read More
posted @ 2014-03-24 11:24 源子陌 Views(1196) Comments(0) Diggs(0) Edit
思路分析:单词的数目可以由空格出现的次数决定。连续的若干空格作为出现一次空格,一行开头的空格不统计在内。如果测出一个字符是非空格,而它前面的字符是空格,则表示新的单词开始了,此时将单词计数器累加1。如果当前字符是非空格而其前面的字符也是非空格,则意味着仍然是原来那个单词的继续,计数器不应再累加1。设置一个标识判断前面一个字符是否为空格,将其初始化为0,表示前一个字符是空格。若遇到非空格字符,则将该标识设为1,表示前一个字符是非空格。代码如下:#include "stdafx.h"#define BUFFERSIZE 1024int main(){ char string[B Read More
posted @ 2014-03-19 22:29 源子陌 Views(997) Comments(0) Diggs(0) Edit
思路分析: 方法一:涉及到两个数,就要用到异或定理了:若a^b=x,则a=b^x,b=x^a。对于这道题,假设这两个数分别为a、b,将数组中所有元素异或之后结果为x,因为a!=b,所以x=a^b,且x!=0,判断x中位为1的位数,只需要知道某一个位为1的位数k,如00101100,k可以取2或者3,或者5.因为x中第k位为1表示a或b中有一个数的第k位也为1,假设为a,将x与数组中第k位为1的数进行异或时,也即将x与a以及其他第k位为1的出现过偶数次的数进行异或,化简即为x与a异或,最终结果即为b。程序示例如下:#include "stdafx.h"#include voi Read More
posted @ 2014-03-14 11:04 源子陌 Views(1440) Comments(0) Diggs(0) Edit
思路分析:任何一个数字异或它自己都等于0,根据这一特性,如果从头到尾依次异或数组中的每一个数字,因为那些出现两次的数字全部在异或中抵消掉了,所以最终的结果刚好是那些只出现一次的数字。代码如下:#include "stdafx.h"#include int findNotDouble(int a[], int n){ int result = a[0]; int i; for (i = 1; i < n; ++i) result ^= a[i]; return result;}int main(){ int array[] = { 1, 2, 3, 2... Read More
posted @ 2014-03-14 10:18 源子陌 Views(806) Comments(0) Diggs(0) Edit
思路分析:可以与归并排序联系起来,给定两个变量A、B,变量A轮着存放:a*1,a*2,a*3,……变量组B轮着存放:b*1,b*2,b*3,……有两个整数i、j,分别代表A、B第i、j次存放的值,每次取A、B中的较小值,并将较小值的次数加一,然后继续比较。代码如下:#include "stdafx.h"#includevoid Generate(int a, int b, int N, int *Q){ int tmpA, tmpB; int i = 1; int j = 1; for (int k = 0; k < N; k++) { tmpA =... Read More
posted @ 2014-03-14 09:41 源子陌 Views(340) Comments(0) Diggs(0) Edit
上一页 1 2 3 4 5 6 ··· 17 下一页