C语言练习

来源:互联网 发布:2017java 笔试题 编辑:程序博客网 时间:2024/06/15 18:57

1.求平方根

#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]){  int n;  float a2,a1=1;  printf("please enter \n");  scanf("%d",&n);  while(1){        a2 = (a1+n/a1)/2;        if(a2 == a1){              printf("result = %f \n",a2);              system("PAUSE");              return 0;              }             else              a1 = a2;    } }


2输入字符串,将里边空格都变成一个空格


#include <stdio.h>#include <stdlib.h>void deblank(char string[]); int main(int argc, char *argv[]){   char st[]="ww dd  wddw   ww    abc";   deblank(st);     //system("PAUSE");   return 0;             }void deblank(char string[]){     int n,m;     //char *p=string;     for(n=0;;){         if(string[n]==' '&&string[n+1]==' ')              for(m=n+1;;m++){                  string[m] = string[m+1];                  if(string[m] == '\0')                       break;              } else//可能是多个空格,检测往前移后,继续检测原位置和后面的位置是不是空格,如果不是,才能n++ n++;         if(string[n] == '\0')               break;                                              }     printf("new st = %s",string);}


0 0
原创粉丝点击