一.自增和自减运算
++i:i自增1后再参与其它运算。
--i:i自减1后再参与其它运算。
i++:i参与运算后,i的值再自增1。
i--:i参与运算后,i的值再自减1。
二.C语言浮点数取整
两种方法:
1.float f = .....;
int i = (int)(f + 0.5);
i就是f四舍五入的结果。
2.使用库函数
floor:向下取整
floor(2.6)=2
floor(-2.6)=-3
ceil:向上取整
ceil(2.3)=3
ceil(-2.3)=-3
注意要包含头文件math.h
参考资料:
1.http://topic.youkuaiyun.com/t/20050203/14/3773229.html
2.http://zhidao.baidu.com/question/155536957.html
3.http://www.360doc.com/content/11/0429/17/3931678_113204596.shtml
三.C语言变量内存分配问题
参考资料:
1.http://blog.youkuaiyun.com/subo86/article/details/4814874
2.http://topic.youkuaiyun.com/t/20051110/09/4383464.html
3.http://zhidao.baidu.com/question/52843150.html
四.回文数问题
1.判断一个整数是不是回文数:
#include <stdio.h>
void main() {
int i,num,new_num = 0,temp = 0;
printf("请输入想要判断的数:");
scanf("%d", &num); temp = num; //将输入的数反过来
while(temp != 0) {
int gewei = temp % 10;
new_num = new_num * 10 + gewei;
temp = temp / 10;
}
//如果一个数是回文数那么取反后的数和原来的数是一样地
if (num == new_num) {
printf("%d", num);
printf("这个数是回文数\n");
} else {
printf("%d", num);
printf("这个数不是回文数\n");
}
}
参考资料:http://topic.youkuaiyun.com/u/20080331/22/d36bf390-944e-432b-a31b-d186dd4db4d0.html
2.判断一字符串是不是回文数:
#include <stdio.h>
#include <string>
void daozhi(char str[])
{
int i,n;
char c,*s=str;
for(n=0;s[n]!='\0';)
n++;
printf("n=%d\n",n);
for(i=0;i<=n/2;i++)
{
c=str[i];
str[i]=str[n-i-1];
str[n-i-1]=c;
}
}
void main()
{
char str1[100],str2[100];
int a;
gets(str1);
strcpy(str2,str1);
daozhi(str2);
a=strcmp(str1,str2);
if(a==0)
printf("%s是回文\n",str1);
else
printf("%s不是回文\n",str1);
}
参考资料:http://zhidao.baidu.com/question/123856313.html
五.链表逆序问题
#include <iostream>
using namespace std;
typedef struct node
{
int data;
node *next;
}*lkList;
//尾插法生成链表
lkList CreatList(int n){
lkList head,p,q;
int size=sizeof(node);
head=(lkList)malloc(size);
p=head;
int x;
for(int i=0;i<n;i++){
q=(lkList)malloc(size);
cout<<"input node "<<i<<" value:";
cin>>x;
q->data=x;
p->next=q;
p=q;
}
p->next=NULL;
return head;
}
//将一条链表按逆序输出
void printLK(node *L){
lkList p,q;
p=q=L;/*p,q为指向头结点的两个指针*/
while(p->next!=NULL)
p=p->next;/*让p指向键表的最后一个要访问结点*/
while(1)
{
while(q->next!=p)
q=q->next;/*让q向后找,找到最后一个要打印的结点*/
printf("%d\n",p->data);
p=q;/*p向前移动一个*/
q=L;/*q又指向头结点*/
if(p==L)/*访问完了退出*/
break;
}
}
//将链表元素位置前后交换
lkList reverse(lkList h) //h为链表的头指针
{
lkList p,v1,v2;
v2=h;
v1=NULL;
while(v2!=NULL){
p=v2->next;
v2->next=v1;
v1=v2;
v2=p;
}
return v1;
}
//将链表元素位置前后交换
lkList res(lkList h)
{
lkList s,s1;
s = h;
h = NULL;
while (s)
{
s1 = s;
s = s->next;
s1->next = h;
h = s1;
}
return h;
}
void main(){
lkList lk=CreatList(5);
//printLK(lk);
//lkList rk=reverse(lk);
lkList rk=res(lk);
while(rk->next!=NULL){
cout<<rk->data<<" ";
rk=rk->next;
}
}
参考资料:
1.http://zhidao.baidu.com/question/41056423.html
2.http://lixon2000.blog.163.com/blog/static/996498020061026113420769/
六.大整数相乘问题
void multiply(char* a,char* b,char* c)
{
int i,j,ca,cb,* s;
ca=strlen(a);
cb=strlen(b);
s=(int*)malloc(sizeof(int)*(ca+cb));
for (i=0;i<ca+cb;i++)
s[i]=0;
for (i=0;i<ca;i++)
for (j=0;j<cb;j++)
s[i+j+1]+=(a[i]-'0')*(b[j]-'0');
for (i=ca+cb-1;i>=0;i--)
if (s[i]>=10)
{
s[i-1]+=s[i]/10;
s[i]%=10;
}
i=0;
while (s[i]==0)
i++;
for (j=0;i<ca+cb;i++,j++)
c[j]=s[i]+'0';
c[j]='\0';
free(s);
}
参考资料:
1.http://topic.youkuaiyun.com/u/20080203/10/64d519c1-030c-4725-abe4-e4d1061e894c.html
2.http://wenku.baidu.com/view/8ac7c44ffe4733687e21aadd.html
3.http://wenku.baidu.com/view/968c4ba0b0717fd5360cdc71.html
七.牛顿迭代法求方程的根
#include<stdio.h>
float solution(float x)
{
float x1,y,k;
do
{
k=6*x*x-8*x+3;
y=2*x*x*x-4*x*x+3*x-6;
x1=x-y/k;
x=x1;
}
while(fabs(y)<0.001);
return x;
}
void main()
{
float x;
x=1.5;
x=solution(x);
printf("%f\n",x);
}
参考资料:
1.http://zhidao.baidu.com/question/73178644.html?fr=qrl&cid=866&index=4
2.http://zhidao.baidu.com/question/6326922.html?fr=qrl&cid=93&index=5&fr2=query
判断整数是否是回文数的程序和大整数相乘的程序,是很经典的程序,有时很难想到,所以记录下来,方便以后查阅
2162

被折叠的 条评论
为什么被折叠?



