[1]反转字符串
#include "stdafx.h"
void reverse(char *str) {
char *end =str;
char tmp;
if(str){
while(*end){
end++;
}
end--;
while(str<end){
tmp= *str;
*str++=*end;
*end--=tmp;
}
}
}
int main(int argc, char* argv[])
{
char testStr[10] = "abcdefg";
reverse(testStr);
printf("%s\n",testStr);
return 0;
}
[2]将字符数组相同的字符去掉
public class CharDemo1{
public static void main(String args[]){
String str1 = "abcdabfh";
char testStr[] = str1.toCharArray();
removeDuplicates(testStr);
for(int i=0;i<testStr.length;i++){
System.out.print(testStr[i]);
}
}
public static void removeDuplicates(char[] str) {
if (str == null) return;
int len = str.length;
if (len < 2) return;
int tail = 1;
for (int i = 1; i < len; i++) {
int j;
for (j = 0; j < tail; j++) {
if (str[i] == str[j]) break;
}
if (j == tail) {
str[tail] = str[i];
tail++;
}
}
for(int i= tail;i<len;i++){
str[i] = 0;
}
}
}
输出:abcdfh
[3]获取当前时间
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include <string.h>
#include <time.h>
#include <windows.h>
int main(int argc, char* argv[])
{
FILE *fp;
int line = 0;
char buf[Num];
if((fp=fopen("d:/time.txt","a"))==NULL){
fprintf(stderr,"don't fopen %s=\n",strerror(errno));
return -1;
}
while(fgets(buf,Num,fp)!=NULL){
if(strlen(buf)<Num-1 ||buf[Num-2]!='\n')
line++;
}
while(1)
{
time_t t;
time(&t);
struct tm *t1;
t1 = localtime(&t);
sprintf(buf,"%d,%d,%d,%d,%d,%d,%d\n",++line,t1->tm_year+1900,
t1->tm_mon,t1->tm_mday,t1->tm_hour,t1->tm_min,t1->tm_sec);
printf("the current time is =%s\n",buf);
fputs(buf,fp);
fflush(fp);
Sleep(1);
}
fclose(fp);
return 0;
}
[4]
int a=10;
int b;
int main(int argc, char* argv[])
{
int i;
printf("i=%d\n",i);
printf("&i=%d\n",&i);
for(i=0;i<a;i++)
b+=3*i;
for(i=0;i<10;i++,a++)
{
;
}
printf("a=%d\n",a);
printf("b=%d\n",b);
return 0;
}
[5]
int foo()
{
int a =0;
a=strlen("aaabbbccc")-10;
return a;
}
int main(int argc, char* argv[])
{
unsigned char a =foo();
printf("a =%d\n",a);
return 0;
}
a =255
[6]
int main(int argc, char* argv[])
{
unsigned char a = -1;
unsigned char b = 257;
unsigned char c = (a*b+4)/2;
printf("c =%d\n",c);
return 0;
}
c=129
[7]
#define FREE(p) free(p);\
p = NULL;
void freeFunc(char *p)
{
free(p);
p=NULL;
}
int main(int argc, char* argv[])
{
char * buf = (char *)malloc(100);
freeFunc(buf);
//FREE(buf);
return 0;
}
freeFunc(char *p)这种方法只能释放内存,但buf并不会=NULL;并不能做到无残留
而FREE(p)则可以。
[8]
#define FREE(p) free(p);\
p = NULL;
int main(int argc, char* argv[])
{
int tag=2;
int i;
char * buf = (char *)malloc(100);
if(tag==3)
FREE(buf);
for(i=0;i<10;i++)
{
buf[i]=2*i+'a';
}
buf[i]=0;
printf("buf is %s\n",buf);
return 0;
}
这段程序会崩溃
[9]
#include "stdafx.h"
#include<iostream.h>
#include<string.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define F_MM(f, m, n) \
{ \
(f), \
(m), \
~((n)-(m)) * !!(n), \
~(n),\
}
struct clk_freq_tbl {
unsigned long freq_hz;
int m_val;
int n_val;
int d_val;
};
static struct clk_freq_tbl ftbl_csi0_1_clk[] = {
F_MM(100000000, 1, 5),
//F_END,
};
int main(int argc, char* argv[])
{
printf("freq is =%d,m_val is =%d,n_val is =%d,d_val is =%d\n",ftbl_csi0_1_clk[0].freq_hz,ftbl_csi0_1_clk[0].m_val,\
ftbl_csi0_1_clk[0].n_val,ftbl_csi0_1_clk[0].d_val);
return 0;
}
[10]
转载请注明出处:http://blog.csdn.net/ns_code/article/details/22091663
题目:
You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.
EXAMPLE
Input: (3 -> 1 -> 5), (5 -> 9 -> 2)
Output: 8 -> 0 -> 8
翻译:
用单链表表示一个正整数,每个结点代表其中的一位数字,逆序排列,个位位于表头,最高位位于表尾,将两个数相加并返回结果,结果也由链表表示。
例如:
输入:(3 -> 1 -> 5)+(5 -> 9 -> 2)
输出:8 -> 0 -> 8
思路:
这道题目不难,也没有太大的技巧性,就按照最常规的思路来,只是要注意将所有的情况全部考虑到。
1、两个链表中有一个为NULL,这时直接返回另一个链表就行了;
2、如果两个链表都为空,这本身也包含在第1种情况中包;
3、如果两个链表长度不等,我们需要将二者相加后的结果保存在较长的链表上;
4、如果某位相加大于等于10,需要进位;
5、如果相加后的链表长度大于另两个链表中最长的那个链表的长度,则需要开辟新的节点,将其挂在长度较长的那个链表的表尾。
实际上该题目也为我们提供了一种实现大数相加的思路。
实现代码: