Cunit6
- 大数加法
#include<cstring>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
using namespace std;int main()
{int tmp,up,i;char buffer[100000], a[100000],b[100000];scanf("%s",buffer);for(tmp = 0, i = strlen(buffer)-1;i>=0;i--)a[tmp++] = buffer[i] - '0';scanf("%s",buffer);for(tmp = 0, i = strlen(buffer)-1;i>=0;i--)b[tmp++] = buffer[i] - '0';for(up = 0,i = 0;i<100000;i++){tmp = a[i] + b[i] + up;a[i] = tmp %10;up = tmp/10;}for(i = 10000;i>=0;i--){if(a[i] != 0 ){//for(i ; i>=0;i--)printf("%d",a[i]);}}system("pause");return 0;
}
- 大数乘法
#include<cstring>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(){int i ,j,up,tmp,n;char arr[100000] = {1};scanf("%d",&n);for(i = 2;i<=n;i++){for(up = 0,j = 0;j<=100000;j++){tmp = arr[j]*i+up;// printf("%d %d",tmp,arr[j]);arr[j] = tmp % 10;up = tmp/10;}}
// printf("%d%d",arr[2],arr[3]);for(i = 100000;i>=0;i--)if(arr[i] != 0){for(i;i>=0;i--)printf("%d",arr[i]);}system("pause"); return 0;
}
- 1!+2!+3!+… n!
#include<cstring>
#include <stdlib.h>
#include <stdio.h>
#include <math.h> int main(){int i,j,n ,sum1=1,sum = 0;scanf("%d",&n);for(i = 1;i<=n;i++){for(j = 1;j<=i;j++){sum1 *= j;}sum+=sum1;sum1 = 1;
}printf("%d",sum);return 0;
}
- a + aa + aaa + aaaa … n个a
#include<cstring>
#include <stdlib.h>
#include <stdio.h>
#include <math.h> int main(){int n,a,i,j,s=0;long sum = 0;scanf("%d%d",&a,&n);for(i=0;i<n;i++){ s = s*10 + a;sum += s;}printf("%ld ",sum);return 0;
}
计算 Π 2 2 4 4 6 6 — = —* —* —* —* —* — ... 2 1 3 3 5 5 7note: 两两一组 2 41 3 3 5分母 = (分子 + 1) * (分子 -1)code :s = s * ( i * i) / ( (i-1) * (i+1) )
int main(){int i ;float s = 1;for(i = 2;i<=100;i=i+2){s = s * ( i * i)/((i-1)*(i+1));}printf("%f",s*2);return 0;
}
- 利用泰勒级数计算sin(x)的值
x^3 x^5 x^7 x^9 sin(x) = x - — + — - — + — - ... 3! 5! 7! 9!要求最后一项的绝对值 fabs() 小于 1e-5 并统计出此时累计了多少项 note:初始化第一项为 x 分子: x的n次方,从x开始 保留结果 每项 * x * x (每次加两个x) 分母: 1*2*3 *4*5 *6*7 保留结果code:tmp = xtmp = -tmp * x * x / ((n+1) * (n+2))
int main(){double sum,tmp;float x;int i,n = 1;scanf("%f",&x);for( i = 1 ,sum = x,tmp = x; fabs(tmp) >= 1e-5 ;i++){tmp = -tmp*x*x/((n+1)*(n+2));n = n + 2;sum += tmp;} printf("sin( %f ) = %f, count = %d",x,sum,i+1);return 0;
}
- 打印三位水仙花数
例如: 135 = 1^3 + 3^3 + 5^3 135是水仙花数
#include<cstring>
#include <stdlib.h>
#include <stdio.h>
#include <math.h> int main(){int i,a,b,c;for(i = 100;i<1000;i++){a = i/100;c = i%10;b = i/10%10;if(i == (a*a*a + b*b*b + c*c*c)){printf("%d\n",i);}}return 0;
}
- 输入 : 1234
分离出 1 2 3 4 四个数
输出 1 + 2 + 3 + 4 = 10
code :input : ntmp = n % 10;sum += tmp;n /= 10;
#include<cstring>
#include <stdlib.h>
#include <stdio.h>
#include <math.h> int main(){int n,i,tmp,sum = 0;scanf("%d",&n);for(i = 0;n>0 ;i++){tmp = n%10;sum += tmp;n /=10; }printf("%d",sum);return 0;
}
- 回文数
12321 4004从左到右读 == 从右到左读note:将数 从左到右重新组合,若与原数相等 即为回文数12341 ——> 14321 12321 ——> 12321code:input :sr = s % 10;m = 10 * m + r;s = s / 10;
#include<cstring>
#include <stdlib.h>
#include <stdio.h>
#include <math.h> int main(){int n , m = 0,s,r;scanf("%d",&n);s = n;while(s != 0){r = s%10;m = 10*m+r;s = s / 10;}if(m == n){printf("yes");}else{printf("no");}return 0;
}
- 150分 = x5分 + y2分 + z*1分
寻找上下界: 每种硬币必须有所有硬币一共100枚换购150分 150 - 1 -2 = 147/5 = 29150-5-1 = 144 / 2 = 72
#include<cstring>
#include <stdlib.h>
#include <stdio.h>
#include <math.h> int main(){int x,y,z,count = 0;for(x= 1;x<=29;x++)for(y = 1;y<=72;y++){z = 100 -x -y;if(5*x + 2*y + z == 150){count++;printf("%02d,%02d,%02d ",x,y,z);if(count % 6 == 0){printf("\n");}}}printf("count = %d\n",count);return 0;
}
四个学生 A B C D中一个孬 看谁是 孬 校长问 谁孬 ABCD一人一个理由四个理由 三个真 一个假枚举 A B C D孬 当满足三个理由为真 一个假 揪出孬娃 code: thisman = 'A' + k;sum = (条件1) + (条件2) + (条件3) + (条件4)sum = 3 true
#include<cstring>
#include <stdlib.h>
#include <stdio.h>
#include <math.h> int main(){int k = 0,sum = 0,g = 0;char thisman = ' ';for(k = 0;k<=3;k++){thisman = 'A' + k;sum = (thisman!='A') + (thisman == 'C') + (thisman == 'D') + (thisman != 'D');if(sum == 3){printf("This man is %c\n",thisman);g = 1;}}if(g!=1) printf("NONONO");return 0;
}
int main(){int i ,j;for(i = 1;i<=3;i++){for( j = 1;j<i;j++)printf(" ");for(j=1;j<=5-2*(i-1);j++)printf("*");printf("\n");}for(i = 1;i<=2;i++){for(j = 1;j<=1 - (i-1)*2;j++)printf(" ");for(j = 1;j<=1+i*2;j++)printf("*");printf("\n"); }printf("\n"); printf("\n"); printf("\n"); for(i = 1;i<=5;i++){for(j = 1;j<=5-i;j++)printf(" ");printf("*****\n");}printf("\n"); printf("\n"); printf("\n"); for(i = 1;i<=5;i++){for(j = 1;j<=i+(i-1);j++)printf("*");printf("\n");} }
- 输入一行字符 统计 A~Z 出现的个数
note:用一个数组存储每个字母出现的次数数组下标 第一个表示A出现的个数第二个表示B出现的个数第三个表示C出现的个数即:数组下标 = 输入字符 - A的ASCII码值
code:num(ch-'A')++;
#include<cstring>
#include <stdlib.h>
#include <stdio.h>
#include <math.h> int main(){char ch;int num[26],i;memset(num,0,26*sizeof(int));while((ch = getchar())!= '\n'){if(ch >= 'A' && ch <= 'Z')num[ch-'A']++;}for(i = 0;i<26;i++){if(i%9 == 0)printf("\n");printf("%c(%d) ",'A' + i,num[i]);}printf("\n");return 0;
}
- 冒泡排序
两两依次比较,将最大的带着往前比较,直到最后一个一次循环结束时最后一项是最大值循环次数依次减去已经排好序的末尾code for(i = 1;i<NUM;i++) for(j = 0;j<NUM-i;j++)
#include<cstring>
#include <stdlib.h>
#include <stdio.h>
#include <math.h> #define NUM 10
int main(){int flag = 0,a[NUM] = {1,29,3,4,5,6,7,8,9,10},i,j,t;for(i = 1;i<NUM;i++){for(j = 0;j<NUM-i;j++)if(a[j] > a[j+1]){t = a[j];a[j] = a[j+1];a[j+1] = t;flag = 1;}if(flag == 0){break;}flag = 0;}for(i = 0;i<NUM;i++)printf("%d ",a[i]);return 0;
}
- 选择排序
找出每次排序中最小的数放在最前面第二次排序 排除首位,向前移 code:for(i = 0;i<num-1;i++){k = i;for(j = i+1;j<10;j++){if(a[j] < a[k]){k = j; } }if(k != i){t = a[k];a[k] = a[i];a[i] = t;}}
#include<cstring>
#include <stdlib.h>
#include <stdio.h>
#include <math.h> int main(){int min,i,j,k,t;int a[10] = {898,456,23,65,1896,35,4285,685,3,856};for(i = 0;i<9;i++){k = i;for(j = i+1;j<10;j++){if(a[j] < a[k]){k = j; } }if(k != i){t = a[k];a[k] = a[i];a[i] = t;}}for(i = 0;i<10;i++) printf("%d ",a[i]);return 0;
}
- 输出如下:
please input the mark of 1thcourseif 1th student:96
please input the mark of 2thcourseif 1th student:89
please input the mark of 3thcourseif 1th student:95
please input the mark of 4thcourseif 1th student:96
please input the mark of 1thcourseif 2th student:98
please input the mark of 2thcourseif 2th student:86
please input the mark of 3thcourseif 2th student:85
please input the mark of 4thcourseif 2th student:84
please input the mark of 1thcourseif 3th student:82
please input the mark of 2thcourseif 3th student:75
please input the mark of 3thcourseif 3th student:76
please input the mark of 4thcourseif 3th student:74
please input the mark of 1thcourseif 4th student:72
please input the mark of 2thcourseif 4th student:95
please input the mark of 3thcourseif 4th student:83
please input the mark of 4thcourseif 4th student:69
please input the mark of 1thcourseif 5th student:94
please input the mark of 2thcourseif 5th student:75
please input the mark of 3thcourseif 5th student:62
please input the mark of 4thcourseif 5th student:71NO. C1 C2 C3 C4 AVER
STD1 96.0 89.0 95.0 96.0 94.0
STD2 98.0 86.0 85.0 84.0 88.2
STD3 82.0 75.0 76.0 74.0 76.8
STD4 72.0 95.0 83.0 69.0 79.8
STD5 94.0 75.0 62.0 71.0 75.5
------------------------------------------------------
AVER_C 88.4 84.0 80.2 78.8
#define NUM_std 5
#define NUM_course 4int main(){int i,j;float score[NUM_std+1][NUM_course+1] = {0};for(i = 0;i<NUM_std;i++)for(j = 0;j<NUM_course;j++){printf("please input the mark of %dthcourseif %dth student:",j+1,i+1);scanf("%f",&score[i][j]);}for(i = 0;i<NUM_std;i++){for(j = 0;j<NUM_course;j++){score[i][NUM_course] += score[i][j];score[NUM_std][j] +=score[i][j];}score[i][NUM_course] /=NUM_course; //个人平均成绩} for(j=0;j<NUM_course;j++) score[NUM_std][j] /= NUM_std; //课程平均成绩 printf(" NO. C1 C2 C3 C4 AVER\n");for(i=0;i<NUM_std;i++){printf("STD%d\t",i+1);for(j = 0;j<NUM_course+1;j++){printf("%6.1lf\t\t",score[i][j]);}printf("\n");}printf("------------------------------------------------------"); printf("\nAVER_C ");for(i = 0;i<NUM_course;i++){printf("%6.1lf\t\t",score[NUM_std][i]);} printf("\n");return 0;
}
- 统计单词的个数
i am shabby Y.
there are 4 words
--------------------------------
Process exited after 13.28 seconds with return value 0
请按任意键继续. . .
#define IN 1
#define OUT 0int main(){int word = OUT, num = 0,i;char a[80],c; gets(a); //输入字符 包含空格 遇到回车停止for(i = 0;(c = a[i])!='\0';i++)if(c==' ')word = OUT; elseif(word == OUT){word = IN;num++;} printf("there are %d words",num); return 0;
}
- 三阶幻方
#define MAX 15
int main(){int m,mm,i,j,k,ni,nj;int mangic[MAX][MAX];printf("INPUT:");scanf("%d",&m);if((m<=0)||(m%2==0)){printf("Error in input data..\n");return 0;}mm = m*m;i = 0;j = m/2;for(k=1;k<=mm;k++){mangic[i][j] = k;if(i==0)ni = m-1;else ni = i -1;if(j==m-1)nj=0;elsenj=j+1;if(mangic[ni][nj]==0){i = ni;j = nj;}else{i++;}for(i = 0;i<m;i++){for(j = 0;j<m;j++)printf("%4d",mangic[i][j]);printf("\n"); }return 0;
}
int main(){long k,min,max,count[10]={0};char str[9];int i;//输入最小最大数scanf("%d%d",&min,&max);if(min > max){printf("\n INPUT ERROR!");return 0;}for(k = min;k<max;k++){sprintf(str,"%8d",k);for(i = 7;i>=0 && str[i] !=' ';i--)count[str[i]-'0']++;}for(i = 0;i<10;i++){printf("%d -- (%ld) ",i,count[i]);if(i==4)printf("\n"); }printf("\n");return 0;
}
更多推荐
Cunit6
发布评论