【C语言】简单C编程题-同位相同的N项之和/标准输入花括号成对判断/行号行输出

来源:互联网 发布:苹果完美越狱软件 编辑:程序博客网 时间:2024/05/22 03:26
//求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字,例如:2+22+222+2222+22222#include <stdio.h>int main(){int a,n,sum;int i,j;sum = 0;printf("请输入项数和a的值");scanf("%d%d",&a,&n);j = a;for(i = 1;i <= n;i++){sum = sum+a;a = j + 10*a;}printf("Sn = %d",sum);}//2.编写一个程序,它从标准输入读取C源代码,并验证所有的花括号都正确的成对出现。0#include <stdio.h>int main(){int c;int count;count =0;while((c = getchar()) != EOF){if(c == '{')count++;if(c == '}')count--;}if( 0 == count)printf("正确");elseprintf("不正确");return 0;}//标准输入行数,然后标准输出,不限制行数的长度,然后在行数前面加上标号#include <stdio.h>#include <stdlib.h>int main(){int a,c;int count;a = 1;count = 0;while((c = getchar())!= EOF){if(a == 1){a = 0;count++;printf("%d",count);}putchar(c);if(c == '\n'){a = 1;}}return 0;}//递归的快速排序,并且输出最大值最小值#include <stdio.h>void swap(int v[],int a,int b){int temp;temp = v[a];v[a] = v[b];v[b] = temp;}void qsort(int v[],int left,int right){int i,last;    //last运用于下标转换if(left >= right)        //如果左边下标大于右边下标,则跳出递归return;swap(v,left,(left+right)/2);    //根据快速排序算法,先将第一个值与中间值转换last = left;for(i = left +1;i <= right;i++)    //v[left]为中间值,对余下值进行判断{if(v[i] < v[left])swap(v,++last,i);            //++last为余下进行进行swap()完成后才进行}swap(v,left,last);                    //将最左与last分区值转换同上面第一步qsort(v,left,last-1);                  //左边计算qsort(v,last+1,right);                  //右边计算}int main(){int arry[10];int i;i = 0;for(i = 0;i <sizeof(arry)/sizeof(arry[0]);i++){scanf("%d",&arry[i]);}qsort(arry,0,sizeof(arry)/sizeof(arry[0])-1);printf("最大数是%d\n",arry[0]);printf("最小数是%d",arry[9]);return 0;}//对1.2.3.4组合的3位数进行统计#include <stdio.h>int main(){int hundreds,tens,ones;hundreds = 0;tens = 0;ones = 0;for(hundreds = 1;hundreds <= 4;hundreds ++){for(tens = 1;tens <= 4;tens ++){for(ones = 1;ones <= 4;ones ++){if(ones != tens && tens != hundreds && hundreds != ones){printf("%d%d%d\n",hundreds,tens,ones);}}}}return 0;}


0 0
原创粉丝点击