赞
踩
两个月的坚持,终不负所望,希望我自己写的题库能为你们寻求一些帮助(针对湖北专升本C语言),希望你们也能不负所托。题的
排名不分先后
#include <stdio.h> int main() { int n, i; printf("请输入你要判断的数:"); scanf("%d", &n); for (i = 2; i < n; i++) if (n % i == 0) break; if (i < n) { printf("%d不是质数\n", n); } else { printf("%d是质数\n", n); } return 0; }
//暴力破解法 #include<stdio.h> int main() { int m, n, temp, i; printf("Input m & n:"); scanf("%d%d", &m, &n); if (m < n) /*比较大小,使得m中存储大数,n中存储小数*/ { /*交换m和n的值*/ temp = m; m = n; n = temp; } //或者三元运算 //n = m < n ? m , n; for (i = n; i > 0; i--) /*按照从大到小的顺序寻找满足条件的自然数*/ if (m % i == 0 && n % i == 0) { /*输出满足条件的自然数并结束循环*/ printf("最大公约数为%d\n", i); break; } return 0; } //辗转相除法 #include<stdio.h> int max(int m, int n) { if (m % n == 0) { return n; } else { return max(n, m % n); } } int main() { //辗转相除法 int m, n, temp; printf("请输入两个数 m & n:"); scanf("%d%d", &m, &n); temp = max(m, n); printf("最大公因数是%d", temp); return 0; } //更相减损术 #include<stdio.h> int max(int m, int n) { int tmp = 0; if (m == n) { return n; } else if (m > n) { tmp = m - n; max(tmp, n); } else { tmp = n - m; max(tmp, m); } } int main() { //更相减损术 int m, n, i; printf("请输入两个数 m & n:"); scanf("%d%d", &m, &n); printf("最大公因数是%d", max(m, n)); return 0; }
#include <stdio.h> int main() { int a, b; printf("请输入两个数:"); scanf("%d %d", &a, &b); int m = (a > b ? a : b); while (1) { if (m % a == 0 && m % b == 0) { break; } m++; } printf("最小公倍数是%d\n", m); return 0; } //求最大的公因数和最小公倍数 #include<stdio.h> void main() { int a, b, m, n;//a是最大公因数 b是最小公倍数 scanf("%d,%d", &m, &n); a = m > n ? m : n; while (a >= 1) { if (m % a == 0 && n % a == 0) { break; } a--; } b = m * n / a; printf("最大公因数是%d,最小公倍数是%d", a, b); }
#include <stdio.h> int main() { // 所谓回文数,就是说一个数字从左边读和从右边读的结果是一模一样的 12321 int num, s, y; printf("请输入你要判断的数:"); scanf("%d", &num); s = num; while (s > 0) { y = y * 10 + s % 10; s /= 10; } if (y == num) { printf("%d是一个回文数\n", num); } else { printf("%d不是一个回文数\n", num); } return 0; }
#include <stdio.h> int main() { int fac(int n); int n, y; printf("输入要求的阶乘:"); scanf("%d", &n); y = fac(n); printf("%d!=%d\n", n, y); return 0; } int fac(int n) { int f; if (n < 0) { printf("n不能小于0"); } else if (n == 0 || n == 1) { f = 1; } else { f = fac(n - 1) * n; } return (f); } //1!+2!+3!+...+20!的和 #include <stdio.h> int main() { int n, s = 0, t = 1, i; printf("输入要求的阶乘:"); scanf("%d", &n); for (i = 1; i <= n; i++) { t = t * n; s = s + t; } printf("1!+2!+3!+...+%d!=%d\n", n, s); return 0; }
#include<stdio.h> int main() { //前提是有序数组 int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }, key;// key是要查找的数字 int len = sizeof(arr) / sizeof(arr[0]); //数组长度 int left = 0; //最小下标 int right = len - 1; //最大下标 scanf("%d", &key); //输入要查找的正整数key while (left <= right) { //int mid = (left + right) / 2; int mid = left + (right - left) / 2;//防止数据过大,越界 if (arr[mid] < key) { left = mid + 1; } else if (arr[mid] > key) { right = mid - 1; } else { printf("找到了,下标是:%d\n", mid); break; } } if (left > right) printf("找不到\n"); return 0; }
int main() { int b, s, g, n; printf("result is:"); for (n = 100; n < 1000; n++) /*整数的取值范围*/ { b = n / 100; s = n / 10 % 10; g = n % 10; if (n == b * b * b + s * s * s + g * g * g) /*各位上的立方和是否与原数n相等*/ printf("%d ", n); } printf("\n"); return 0; }
#include<stdio.h> int Mystrcmp(char *p1,char *p2){ while(*p1==*p2){ if(*p1=='\0'){ return 0; } p1++; p2++; } return *p1-*p2; } int main(){ char a1[]="chb"; char a2[]="chd"; printf("%d",Mystrcmp(a1,a2)); return 0; }
#include<stdio.h> int Fibon(int n) { if (n == 1 || n == 2) { return 1; } else { return Fibon(n - 1) + Fibon(n - 2); } } int main() { for (int i = 1; i <= 12; i++) { printf("%d ", Fibon(i)); } return 0; }
#include<stdio.h> #include<string.h> int main() { int i; char str[80], min[80]; printf("请输入第一个学生名字:"); gets(str); strcpy(min, str); for (i = 1; i < 5; i++) { gets(str); if (strcmp(str, min) < 0) { strcpy(min, str); } } printf("\n字典排序最前面的学生姓名:"); puts(min); return 0; }
#include<stdio.h> #include<string.h> int main() { int i, leap, year, month, day = 0; int dayTab[2][13] = { {0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31} }; printf("请输入年月日:"); scanf("%d%d%d", &year, &month, &day); leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); for (i = 1; i < month; i++) { day = day + dayTab[leap][i]; } printf("今年的第%d天", day); return 0; }
#include<stdio.h> #include<string.h> void itoa(int num, char str[100]) { int i = 0; do { str[i] = num % 10 + '0'; num /= 10; i++; } while (num); str[i] = '\0'; } int main() { int num; char str[100]; printf("输入一个待转字符串整数:"); scanf_s("%d", &num); itoa(num, str); printf("转后的字符串为:"); for (int i = strlen(str) - 1; i >= 0; i--) printf("%c", str[i]); }
int fact(int n) { int i; if (n < 2) { return 0;//这里用0和1方便判断真假 } else if (n) { return 1; } else { for (i = 2; i <= n / 2; i++) { if (n % i) return 0; } return 1; } } #include<stdio.h> int main() { int n, i; printf("请输入一个大于六的整数,在这里我们要求哥德巴赫猜想:"); scanf("%d", &n); if (n < 6) { printf("error!\n"); } else { for (i = 3; i <= n / 2; i = i + 2) { if (fact(i) == 1 && fact(n - i) == 1) { printf("%d =%d + %d\n", n, i, n - i); } } } return 0; }
#include <stdio.h>
#include <string.h>
int main() {
char s1[80], s2[80];
int i;
printf("input s2:");
gets(s2);
for (i = 0; i <= strlen(s2); i++)
{
s1[i] = s2[i];
}
printf("s1:%s\n", s1);
return 0;
}
#include <stdio.h> #include <math.h> int main(void) { //while循环是先执行判断再执行循环体 //所以count用来计数一共累加多少个从1开始,n作为分母也从1开始 int n = 1, count = 1; //取最后的累加和使用类型为double类型 double pi = 0.0, sum = 1.0, term = 1.0, sign = 1; //1e-5就是 1*(10的-5次方)即0.00001 while (fabs(term) >= 1e-5) { count++; sign = -sign; n = n + 2; term = sign / n; sum = sum + term; } pi = 4 * sum; printf("π=%f \n 一共累加项count=%d\n", pi, count); return 0; }
#include <stdio.h>
int main()
{
char c;
scanf("%c",&c);
if(c>='A' && c<='Z')
{
if(c=='A') printf("没有前面的字母\n");
else if(c=='Z') printf("没有后面的字母\n");
else printf("前面字母:%c,后面字母:%c\n",c-1,c+1);
}
return 0;
}
#include<stdio.h>
int main() {
int a, n, i = 1, sn = 0, tn = 0;
printf("a,n=");
scanf("%d,%d", &a, &n);
while (i <= n)
{
tn = tn + a;
sn = sn + tn;
i++;
a = a * 10;
}
printf("a+aa+aaa.....=%d\n", sn);
return 0;
}
#include <stdio.h> int main() { double sn=100,hn=sn/2; int n; for(n=2;n<=10;n++) { sn=sn+2*hn; /*第n次落地时共经过的米数*/ hn=hn/2; /*第n次反跳高度*/ } printf("第10次落地时共经过%f米\n",sn); printf("第10次反弹%f米\n",hn); return 0; } #include <stdio.h> int main() { int i, n = 10; float sum = 0.0, num = 100.0; for (i = 0; i < 10; i++) { sum = sum + num + num / 2.0; //下降高度+弹起的高度 num = num / 2; } printf("第10次落地时共经过%f米\n", sum - num); //多了一次弹起的高度 printf("第10次反弹%f米\n", num); return 0; }
void fun(char *ss)
{
int i,n;
n=strlen(ss);
for(i=1;i<n;i+=2)
if(ss[i]>= 'a'&& ss[i]<='z')
ss[i]=ss[i]-32;
}
#include <stdio.h>
int main()
{
int x;
for (x=1; ;x++)
{
if (x%5==1 && x%6==5 && x%7==4 && x%11==10)
{
printf("x = %d\n", x);
break;
}
}
return 0;
}
#include <stdio.h> int main() { int profit; float d; float s = 800; printf("请输入工资:"); scanf("%d", &profit); if (profit < 0) { printf("输入的工资无效\n"); return 1; } switch (profit / 1000) { case 0: d = 0; break; case 1: d = 0.1; break; case 2: case 3: case 4: d = 0.15; break; case 5: case 6: case 7: case 8: case 9: d = 0.20; break; default: d = 0.25; } s = s + profit * d ; printf("工资s=%.2f", s); return 0; }
例如6=1+2+3.编程。找出1000以内的所有完数。
#include "stdio.h" int main() { int m, s, i; for (m = 2; m < 1000; m++) { s = 0; for (i = 1; i < m; i++) { if ((m % i) == 0) { s = s + i; } } if (s == m) { printf("\n%d is a wanshu\n", m); for (i = 1; i < m; i++) { if (m % i == 0) { printf("%d\t", i); } } } } return 0; }
#include <stdio.h> int main() { int n ; printf("请输入一个正整数:"); scanf("%d", &n); printf("%d = ", n); for (int i = 2; i <= n; i++) { while(n != i) { // 执行的条件必须是n与i不等,若相等则分解结束了 if(n % i == 0) { // 若能整除则i为n的因子之一 printf("%d * ", i); // 输出因子 n = n / i; // 找到了一个因子i,则n/i缩小n继续寻找 } else { break; // 不能整除则跳出本次循环,递增i进行下一轮 } } } printf("%d\n", n); // 最后剩下的n不能整除i,所以它也为因子之一,所以最后输出 return 0; }
#include<stdio.h> /*输入年份和月份,判断该年是不是闰年,并根据月份判断是什么季节和这个月有多少天*/ int main(){ int y,m,leap,season,days; printf("年份,月份"); scanf("%d,%d",&y,&m); if((y%4==0&&y%100!=0) ||(y%400==0)) leap==1; else leap=0; if(m>=3&&m<=5) season=1; else if(m>=6&&m<=8) season=2; else if(m>=9&&m<=12) season=3; else season=4; switch(m){ case 1:case 3:case 5:case 7:case 8:case 10:case 12:days=31;break; case 4:case 6:case 9:case 11:days=30;break; case 2:if(leap=1) days=29;else days=28; } printf("%d年%S闰年\n",y,(leap==1?"是":"不是")); printf("该季节是:"); switch(season){ case 1:printf("春季");break; case 2:printf("夏季");break; case 3:printf("秋季");break; case 4:printf("冬季");break; } printf("当月天数:%d\n",days); return 0; } #include <stdio.h> #define MONTHS 13 int main() { int days[MONTHS] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; int i, year, month, d, sum = 0; do { printf("Input a data:"); scanf("%d%d%d", &year, &month, &d); } while (month < 1 || month > 12); for (i = 1; i < month; i++) { sum += days[i]; } if ((year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && (month >= 3)) sum += d + 1; else sum += d; printf("The number of days is %d\n", sum); return 0; }
#include<stdio.h> #include<math.h> int main(){ float a,b,c; float s,area; printf("a,b,c="); scanf("%f,%f,%f",&a,&b,&c); if(a+b>c&&a+c>b&&b+c>a){ s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); printf("三角形的面积为area=%f\n",area); if(a==b&&b==c){ printf("等边三角形\n"); } if(a==b||b==c||a==c)printf("等腰三角形\n"); else if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(b*b+c*c==a*a))printf("直角三角形\n"); }else{ printf("不能组成三角形\n"); } return 0; }
#include<stdio.h>
#include<math.h>
int main(){
int n,i=0;
printf("n=");
scanf("%d",&n);
printf("对应的二进制数为:\n");
while(n){
printf("%d",n%2);
n=n/2;
}
return 0;
}
#include<stdio.h> #include<math.h> int main(){ int n,min,max,first=1; printf("输入数序\n"); while(1){ scanf("%d",&n); if(n==-1)break; if(first){ first=0; min=max=n; }else{ if(max<n)max=n; else if(n<min)min=n; } } if(!first)printf("max=%d,min=%d\n",max,min); else printf("没有输入任何有效数\n"); return 0; }
①方法1:
#include<stdio.h> #include<math.h> int main(){ char c; int i,j,k,n; printf("输入n:\n"); scanf("%d",&n); c='A'; for(k=1;k<=n;k++){ for(i=1;i<=n-k;i++)printf(" "); for(j=1;j<=k*2-1;j++)printf("%c",c); c++; printf("\n"); } for(k=1;k<n;k++){ for(i=0;i<k;i++)printf(" "); for(j=2*(n-k);j>0;j--)printf("%c",c); c++; printf("\n"); } return 0; }
②方法2:
#include<stdio.h> #include<math.h> int main(){ char c; int i,j,k,n; printf("输入n:\n"); scanf("%d",&n); c='A'; for(k=1-n;k<=n-1;k++){ i=n-abs(k); for(j=1;j<=n-i+1;j++) printf(" "); for(j=1;j<=2*i-1;j++) printf("%c",c); printf("\n"); c++; } return 0; } // * // *** // ***** //******* // ***** // *** // * #include<stdio.h> int main() { int i, j, k; for (i = 0; i <= 3; i++) { for (j = 0; j <= 2 - i; j++) printf(" "); for (k = 0; k <= 2 * i; k++) printf("*"); printf("\n"); } for (i = 0; i <= 2; i++) { for (j = 0; j <= i; j++) printf(" "); for (k = 0; k <= 4 - 2 * i; k++) printf("*"); printf("\n"); } return 0; }
#include<stdio.h> #include<math.h> int main(){ char c; int i,j,k,n,m; printf("输入n:\n"); scanf("%d",&n); m=pow(2,n); m=m-1; for(i=0;i<m;i++){ printf("{"); j=i; k=0; while(j){ if(j%2==1)printf("%d",k); k++; j=j/2; } printf("}"); } return 0; }
#include<stdio.h>
#include<math.h>
int main(){
int n,i,j,k,s,temp=1;
float e=0;
scanf("%d",&n);
for(i=1;i<=n;i++){
k=i;
temp=temp*k;
e+=1.0/temp;
}
printf("%f",e);
return 0;
}
#include<stdio.h> //铁0铜1锡2 int main(){ int a,a1,a2,b,b1,b2,c,c1,c2; int x; for(x=0;x<3;x++){ a1=(x!=0); a2=(x!=1); a=a1&&a2; b1=(x!=0); b2=(x==2); b=b1&&b2; c1=(x!=2); c2=(x==0); c=c1&&c2; if((a+b+c==1)&&a1+b1+c1+a2+b2+c2==3){ switch(x){ case 0:printf("矿石是铁矿\n");break; case 1:printf("矿石是铜矿\n");break; case 2:printf("矿石是锡矿\n");break; } if(a1==a2&&a1==1)printf("甲是老工人\n"); else if((a1==0&&a2==1)||(a2==0&&a1==1))printf("甲是普通工人\n"); else if(a1==a2&&a1==0)printf("甲是实习生\n"); if(b1==b2&&b1==1)printf("乙是老工人\n"); else if((b1==0&&b2==1)||(b2==0&&b1==1))printf("乙是普通工人\n"); else if(b1==b2&&b1==0)printf("乙是实习生\n"); if(c1==c2&&c1==1)printf("丙是老工人\n"); else if((c1==0&&c2==1)||(c2==0&&c1==1))printf("丙是普通工人\n"); else if(c1==c2&&c1==0)printf("丙是实习生\n"); } } return 0; }
#include<stdio.h> #include<math.h> void main() { int n ,x; double sum = 1, a = 1,b=1; int i = 1; scanf("%d%d", &n, &x); for ( ; i <=n; i++) { a = pow(x, i); b *= i; //阶乘 sum += a / b; } printf("%1f", sum); }
两条对角线上的各元素之和
#include<stdio.h> int f(int b[][4]) { int s = 0; for (int i = 0; i < 4; i++) { s += b[i][i]+b[i][3 - i]; } return s; } void main() { int a[4][4] = { {1 , 2 , 3 , 4 }, {5 , 6 , 7 , 8 }, {9 , 10 , 11 , 12}, {13 , 14 , 15 , 16} }; printf("%d", f(a)); } #include <stdio.h> #define N 5 int main() { int a[N][N]; int i, j; for (i = 0; i < N; i++) { for (j = 0; j < N; j++) scanf_s("%d", &a[i][j]); } for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { printf("%4d", a[i][j]); } printf("\n"); } int sum1 = 0, sum2 = 0;//sum1主对角线 sum2副对角线 for (i = 0; i < N; i++) { sum1 += a[i][i]; sum2 += a[i][N - 1 - i]; } printf("主对角线之和是%d\n副对角线之和是%d\n", sum1, sum2); return 0; }
#include<stdio.h> void main(){ int a,b,c,d,i,j, temp; scanf("%d%d%d%d", &a, &b, &c, &d); int arr[4] = { a,b,c,d }; for ( i = 0; i < 4; i++) { for (j = 0; j < 4-1-i; j++) { if (arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } for ( i = 0; i < 4; i++) { printf("%d ", arr[i]); } } //方法二 #include<stdio.h> void main() { int a, b, c, d, t; printf("请输入4个数(空格分隔):"); scanf("%d%d%d%d", &a, &b, &c, &d); if (a > b) { t = a; a = b; b = t; } if (a > c) { t = a; a = c; c = t; } if (a > d) { t = a; a = d; d = t; } if (b > c) { t = b; b = c; c = t; } if (b > d) { t = b; b = d; d = t; } if (c > d) { t = c; c = d; d = t; } printf("排序如下:\n"); printf("%d %d %d %d", a, b, c, d); }
#include<stdio.h> #include<math.h> void main() { float a, b, c, x1, x2, t; scanf("%f%f%f", &a, &b, &c); t = b * b - 4 * a * c; if (t >= 0) { x1 = (-b + sqrt(t)) / (2 * a); x2 = (-b - sqrt(t)) / (2 * a); printf("x1=%f,x2=%f", x1, x2); } else { printf("Error"); } }
#include<stdio.h> #define N 5 void main() { int a[N] = { 1,2, 3,4,5 }, i, t; for (i = 0; i < N / 2; i++) { t = a[i]; a[i] = a[N - 1 - i]; a[N - 1 - i] = t; } for (i = 0; i < N; i++) { printf("%4d", a[i]); } }
#include<stdio.h> #define N 10 void main() { int a[N] = { 0 }, i, n; while (1) { scanf("%d", &n); if (n == -1) break; a[n]++; } for (i = 0; i < N; i++) { printf("%d出现了%d\n", i, a[i]); } }
#include <stdio.h> int main() { char ss[] = "sTudyhaRdaazz"; //STUdYhARDaAzZ int i; for (i = 0; ss[i] != '\0'; i++) { if ((i + 1) % 2 != 0 && ss[i] <= 'z' && ss[i] >= 'a') { ss[i] -= 32; } } printf("%s", ss); return 0; }
#include <stdio.h> int main() { char a[] = "dhjakhfdaudgayugahashuadyhli"; int i, b = 0; for (i = 0; a[i] != '\0'; i++) { if (a[i] == 'a') { b++; } } printf("%d", b); return 0; }
字符’C’出现的次数存放到b[2]中, 字符’D’出现的次数存放到b[3]中, 字符’E’出现的次数存放到b[4]中,其它字符出现的次数存到b[5]中。例如, 当a中的字符串为: “VISUA-BASIC”,调用该函数后, b中存放数据应是:2、1、1、0、0、7。
#include <stdio.h> int main() { char a[] = "VISUA-BASIC"; int b[6] = { 0 }; int i, len; len = sizeof(b) / sizeof(int); for (i = 0; a[i] != '\0'; i++) { switch (a[i]) { case 'A':b[0]++; break; case 'B':b[1]++; break; case 'C':b[2]++; break; case 'D':b[3]++; break; case 'E':b[4]++; break; default: b[5]++; break; } } for (i = 0; i < len; i++) { printf("%d,", b[i]); } return 0; }
#include <stdio.h>
int main()
{
int i, k = 0;
char s[] = "china123df";
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] < '0' || s[i] > '9') {//当s[i]不是数字的时候
s[k++] = s[i]; //只有不是数字的时候进入循环,k的值一直保持在不是数字的是那个值,s[i]的值赋值给s[k]
}
}
s[k] = '\0';//给数组末尾放置一个结束符
puts(s);
return 0;
}
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d*%d=%d ", i, j, i * j);
}
printf("\n");
}
return 0;
}
2x + 1(x<0)
f(x) = 0 (x=0)
3x²-1 (x>0)
编程实现输入一个x值,输出y值。
#include<stdio.h>
int main() {
int x, y;
printf("请输入一个数:");
scanf_s("%d", &x);
if (x < 0)
y = 2 * x + 1;
else if (x == 0)
y = 0;
else
y = 3 * x * x - 1;
printf("\n%d", y);
return 0;
}
88<=score,等级为A;
78<=score<88,等级为B;
68<=score<78,等级为C:
60<= score<68,等级为D:
score<60,等级为E。用switch实现
#include<stdio.h> int main() { int score; char grade; printf("请输入一个数:"); scanf_s("%d", &score); if (score > 60) { score += 2; } switch (score / 10) { case 10: case 9: grade = 'A'; break; case 8: grade = 'B'; break; case 7: grade = 'C'; break; case 6: grade = 'D'; break; default: grade = 'E'; } printf("等级为:%c\n", grade); return 0; }
#include <stdio.h>
int main() {
int a, b, c, t; /*定义4个基本整型变量a、b、c、t*/
printf("Please input a,b,c:\n");
scanf_s("%d%d%d", &a, &b, &c); /*输入任意3个数*/
if (a > b)
t = a > c ? a : c;
else
t = b > c ? b : c;
printf("最大的数%d", t);
return 0;
}
#include<stdio.h> int main() { double judges[12]; int i = 0; while (i < 12) { printf("请输入第%d个评委分数\n", i + 1); scanf_s("%lf", &judges[i]); i++; } double maxscore = judges[0]; double minscore = judges[0]; for (i = 1; i < 12; i++) { if (maxscore < judges[i]) maxscore = judges[i]; if (minscore > judges[i]) minscore = judges[i]; } double all = 0, avg = 0; for (i = 0; i < 12; i++) { all += judges[i]; } all = all - maxscore - minscore; avg = all / 10.0; printf("最高分%.2f,最低分%.2f,最后得分是%.2f", maxscore, minscore, avg); return 0; }
#include<stdio.h>
int main() {
int i, s = 1; //s 是剩余的桃子数
for (i = 0; i < 9; i++)//因为到第十天还剩一个,所以循环9次
{
s = (s + 1) * 2;//倒序求出每天的数量
}
printf("The first day:%d\n", s);//输出结果
return 0;
}
#include<stdio.h> int main() { int i,n, y; printf("Input n:"); scanf_s("%d", &n); for (i = 100; i <= 999; i++) { if (n % i == 0) { y = i; } } printf("最大的三位数是:%d", y); return 0; }
#include<stdio.h> int main() { int i, x, y, z = 1; printf("请输入两个数,x和y(x^y):\n"); scanf_s("%d%d", &x, &y); /*输入底数和幂数*/ for (i = 1; i <= y; i++) z = z * x % 1000; /*计算一个数任意次方后的后三位*/ if (z >= 100) { printf("%d^%d的最后三位是:%d\n", x, y, z); /*输出最终结果*/ } else { printf("%d^%d的最后三位是:0%d\n", x, y, z); /*输出最终结果*/ } return 0; }
#include <stdio.h> #define N 10 int main() { int i, j, a[N][N] = { 0 }; for (i = 0; i < N; i++) a[i][0] = 1; for (i = 1; i < N; i++) for (j = 1; j <= i; j++) a[i][j] = a[i - 1][j - 1] + a[i - 1][j]; for (i = 0; i < N; i++) { for (j = 0; j < N - i; j++) printf(" "); for (j = 0; j <= i; j++) printf("%6d", a[i][j]); printf("\n"); } return 0; }
#include<stdio.h> int main() { int i, j = 1; float sum = 0.0; for (i = 1; i < 20; i++) { j *= i; if (i % 2 == 0) { continue; } sum += j; } printf("sum=%.2f\n", sum); return 0; }
#include<stdio.h> int main() { int i, n, max, min, a; float s; printf("请输入有多少个数和第一个数值:"); scanf_s("%d%d", &n, &a); s = max = min = a; for ( i = 1; i < n; i++) { scanf_s("%d", &a); s += a; if (max < a) max = a; if (min > a) min = a; } printf("max=%d,min=%d,avg=%f", max, min, s / n); return 0; }
#include<stdio.h> int main() { int i, n = 0; int a, b, c; for (i = 100; i < 1000; i += 2) { a = i / 100; b = i / 10 % 10; c = i % 10; if (a != b && a != c && b != c) n++; } printf("%d", n); return 0; }
#include<stdio.h> int main() { char s[20] = "baacda", c[20]; int i, k = 0; for (i = 0; s[i] != '\0'; i++) { c[k++] = s[i]; if (s[i] == 'a') { c[k++] = s[i]; } } c[k] = '\0'; puts(c); return 0; }
#include<stdio.h> int main() { char str1[20], str2[20]; int i, j = 0, m; printf("请输入字符串:"); gets(str1); printf("从第几个字符复制:"); scanf_s("%d", &m); i = m - 1; while (str1[i] != '\0') { str2[j++] = str1[i++]; } for (i = 0; i < j; i++) { printf("%c ", str2[i]); } return 0; }
#include<stdio.h>
int main()
{
int i, j = 1, n;
int sum = 0;
printf("请输入n的值:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
j *= i;
sum += j;
}
printf("结果为:%d\n", sum);
return 0;
}
#include <stdio.h> #define N 9 void main() { int a[N], i, t; for (i = 0; i < N; i++) scanf_s("%d", &a[i]); for (i = 0; i < N; i++) printf("%4d", a[i]); printf("\n"); for (i = 0; i < N / 2; i++) { t = a[i]; a[i] = a[N - i - 1]; a[N - i - 1] = t; } for (i = 0; i < N; i++) printf("%4d", a[i]); }
#include <stdio.h> #define N 10 void main() { int a[N] = { 1,9,8,6,45,78,3,2,5,98 }, i, j, t; //printf("请输入10个数:"); //for (i = 0; i < N; i++) // scanf_s("%d", &a[i]); for (i = 0; i < N; i++) printf("%4d", a[i]); printf("\n"); //冒泡排序 for (i = 0; i < N - 1; i++) { for (j = 0; j < N - 1 - i; j++) { //> 是升序 <是降序 if (a[j] > a[j + 1]) { t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } for (i = 0; i < N; i++) printf("%4d", a[i]); }
#include<stdio.h> #define N 10 int main() { int a[N + 1] = { 1,2,3,5,7,8,10,15,20,25 }, i, j, t, k, x; //printf("请输入10个数:"); //for (i = 0; i < N; i++) // scanf_s("%d", &a[i]); for (i = 0; i < N; i++) printf("%4d", a[i]); printf("\n"); //冒泡排序 for (i = 0; i < N - 1; i++) { for (j = 0; j < N - 1 - i; j++) { //> 是升序 <是降序 if (a[j] > a[j + 1]) { t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } for (i = 0; i < N; i++) printf("%4d", a[i]); printf("\n请输入你要插入的数"); scanf_s("%d", &x); k = 0; while (x > a[k]) k++; for (i = N - 1; i >= k; i--) a[i + 1] = a[i]; a[k] = x; for (i = 0; i <= N; i++) printf("%4d", a[i]); return 0; }
#include <stdio.h>
void main()
{
int a = 8, b = 4, c = 2, i, j, z, sum = 0;
for (i = 0; i < 200 / 8; i++)
for (j = 0; j < 200 / 4; j++)
for (z = 0; z < 200 / 2; z++)
if (((i * a) + (j * b) + (z * c)) == 200)
sum++;
printf("%d", sum);
}
#include <stdio.h> #include <stdlib.h> #include <time.h> void main() { int a[20], i, max, min, sum; srand((unsigned)time(NULL)); //防止随机数每次重复 使用这个函数要导入time.h头文件 a[0] = rand() % 41 + 10; max = min = sum = a[0]; for (i = 1; i < 20; i++) { a[i] = rand() % 41 + 10; if (max < a[i])max = a[i]; if (min > a[i])min = a[i]; sum += a[i]; } for (i = 0; i < 20; i++) printf("%d ", a[i]); printf("\n最大值max=%d\n最小值min=%d\n平均值avg=%.2f\n各元素之和sum==%d", max, min, sum / 20.0, sum); }
[1 2 5] [9 7 6]
a=|3 4 8| a = |8 4 3|
[6 7 9] [5 2 1]
100 200 300
400 500 600
700 800 900
转置矩阵是:
100 400 700
200 500 800
300 600 900
#include <stdio.h> int main() { int i, j, k, a[3][3] = { 1,2,5,3,4,8,6,7,9 }; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("%3d", a[i][j]); } printf("\n"); } printf("\n"); for (i = 0; i < 3; i++) { k = a[0][i]; a[0][i] = a[2][i]; a[2][i] = k; } for (i = 0; i < 3; i++) { k = a[i][0]; a[i][0] = a[i][2]; a[i][2] = k; } for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("%3d", a[i][j]); } printf("\n"); } return 0; } //第二题 #include <stdio.h> int fun(int array[3][3]) { int i, j, t; for (i = 0; i < 2; i++) { for (j = i + 1; j < 3; j++) { t = array[i][j]; array[i][j] = array[j][i]; array[j][i] = t; } } } int main() { int i, j, array[3][3] = { {100,200,300},{400,500,600},{700,800,900} }; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("%7d", array[i][j]); } printf("\n"); } fun(array); printf("转置矩阵是:\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("%7d", array[i][j]); } printf("\n"); } return 0; }
#include<stdio.h> int main() { int i, j, k; for (i = 0; i <= 20; i++) { for (j = 0; j <= 33; j++) { for (k = 0; k <= 99; k += 3) { if (5 * i + j * 3 + k / 3 == 100) { if (i + j + k == 100) { printf("公鸡:%d,母鸡:%d,小鸡:%d\n", i, j, k); } } } } } return 0; }
#include<stdio.h>
int main() {
int N, a;
scanf_s("%d", &N);
a = N % 5;
if (a <= 3 && a > 0)
printf("打鱼 day %d", N);
else
printf("晒网 in day %d", N);
return 0;
}
#include <stdio.h> #include <string.h> void move(char* a) { char* p = a; int n = strlen(a), i; if (n <= 3) { return; } for (i = 0; i < 3; i++) { *(a + n + i) = *(a + i); } p = a + 3; while (*p) { *a++ = *p++; } *a = '\0'; } int main() { char str[20] = "abcedf"; move(str); printf("%s", str); return 0; }
①求出它是几位数
2分别输出每一位数字
③按逆序输出各位数字。
测试输入;
1234
预期输出:
1234为4位数
顺序输出为:1,2,3,4
逆序输出为:4,3, 2, 1
#include<stdio.h> #include<string.h> int len(int num) { int count = 0; while (num > 0) { num /= 10; count++; } return count; } //顺序 void outputs1(int num) { if (num > 9) { outputs1(num / 10); } printf("%d", num % 10); } //逆序 void outputs2(int num) { int n = 0; while (num > 0) { n = num % 10; num = num / 10; printf("%d ", n); } } int main() { int num; printf("测试输入:\n"); scanf_s("%d", &num); printf("预期输出:\n"); printf("%d为%d位数\n", num, len(num)); printf("顺序输出为:"); outputs1(num); printf("\n逆序输出为:"); outputs2(num); }
#include<stdio.h> int main() { int i, j, temp, a[10] = { 1,3,2,10,5,8,7,6,9,4 }; int* p; p = a; /* for (i = 0; i < 10; i++) scanf_s("%d", &a[i]);*/ for (i = 0; i < 9; i++) { int min = i; for (j = i + 1; j < 10; j++) { if (a[j] < a[min]) min = j; } temp = a[min]; a[min] = a[i]; a[i] = temp; } for (i = 0; i < 10; i++) { printf("%d\t", a[i]); } return 0; } //方法二 #include<stdio.h> int main() { int i, j, temp, a[10] = { 1,3,2,10,5,8,7,6,9,4 }; for (i = 0; i < 9; i++)//循环9次是因为最后一个数不用排序 { int min = i; //把当前循环第一个数当成最小的的数 for (j = i + 1; j < 10; j++) { if (a[min] > a[j]) { min = j; //标记本次循环最小的数 } }//利用标记的数与本次循环的i进行交换 temp = a[min]; a[min] = a[i]; a[i] = temp; }//打印输出 for (i = 0; i < 10; i++) { printf("%d\t", a[i]); } return 0; }
#include<stdio.h> int main() { int a[15] = { 1,1,1,2,2,2,3,3,3,4,4,4,5,6,7 }, * s = a; int b = 1, i; for (int i = 1; i < 15; ++i) { if (*(s + i - 1) != *(s + i)) { // 当前元素与前一个元素进行比较,如果相同,则调到下一个,否则b+1 *(s + b) = *(s + i); b++; } } for (i = 0; i < b; i++) { printf("%d", a[i]); } return 0; }
#include<stdio.h> int gbs(int n, int m); int main() { printf("%d", 3 * 21 - gbs(5, 6) - gbs(6, 7) - gbs(5, 7)); return 0; } //n是几秒响一次,m是几秒响一次 int gbs(int n, int m) { int i, count = 0; for (i = 1; i <= 21; i++) { if (n * i % m == 0) count++; } return count; }
#include<stdio.h> void Insert(char *s,char *q) { while(*s){ *q++=*s; *q=' '; q++; s++; } *q='\0'; } int main() { char s[100]; char q[100]; printf("Please input s:"); gets(s); Insert(s,q); printf("the new string:%s", q); return 0; } //方法二 #include<stdio.h> #include<string.h> void Insert(char *p) { int i; /*结束为止应该在1位置,下标从0开始*/ for (i = strlen(p); i > 0; --i) { *(p + 2 * i) = *(p + i); *(p + 2 * i - 1) = ' '; } } int main() { char s[100]; printf("Please input s:"); gets(s); Insert(s); printf("the new string:%s", s); return 0; }
如输入“ABC”,应输出:A B C”
#include<stdio.h> #include<string.h> void Insert(char *p) { int i; /*结束为止应该在1位置,下标从0开始*/ for (i = strlen(p); i > 0; --i) { *(p + 2 * i) = *(p + i); *(p + 2 * i - 1) = ' '; } } int main() { char s[100]; printf("Please input s:"); gets(s); Insert(s); printf("the new string:%s", s); return 0; } //方法二 #include<stdio.h> void Insert(char *s,char *q) { while(*s){ *q++=*s; *q=' '; q++; s++; } *q='\0'; } int main() { char s[100]; char q[100]; printf("Please input s:"); gets(s); Insert(s,q); printf("the new string:%s", q); return 0; }
int fact(int n) { int i; if (n < 2) { return 0;//这里用0和1方便判断真假 } else if (n) { return 1; } else { for (i = 2; i <= n / 2; i++) { if (n % i) return 0; } return 1; } } #include<stdio.h> int main() { int n, i; printf("请输入一个大于六的整数,在这里我们要求哥德巴赫猜想:"); scanf("%d", &n); if (n < 6) { printf("error!\n"); } else { for (i = 3; i <= n / 2; i = i + 2) { if (fact(i) == 1 && fact(n - i) == 1) { printf("%d =%d + %d\n", n, i, n - i); } } } return 0; }
#include <stdio.h> int main() { char a, b, c; //a,b,c的对手是 a b c for (a = 'x'; a <= 'z'; a++) { for (b = 'x'; b <= 'z'; b++) { if (a != b) // a和b的对手不能相同 { for (c = 'x'; c <= 'z'; c++) { if ((a != c) && (b != c)) //a和c的对手不能相同,b和c的对手也不能相同 { if ((a != 'x') && (c != 'x') && (c != 'z')) //根据题目中a和c说的话进行排除 { printf("a——%c\nb——%c\nc——%c\n", a, b, c); } } } } } } return 0; }
#include<stdio.h> #include<math.h> int main() { //n被除数,k除数的最大值,i除数,m统计质数的个数 int n, k, i, m = 0, sum = 0; for (n = 2; n <= 500; n++) { k = sqrt(n); for (i = 2; i <= k; i++) { if (n % i == 0) { break; } } if (i > k) { printf("%4d", n); sum += n; m++; if (m % 10 == 0) //每10个换一行 { printf("\n"); } } } printf("\nsum=%d\n", sum); return 0; }
#include<stdio.h> #define ROW 3 #define COL 3 int main() { int a[][3] = { 1,2,3,4,5,6,7,8,9 }; int max = a[0][0]; int maxRow = 0, maxCol = 0; for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (a[i][j] > max) { max = a[i][j]; maxRow = i; maxCol = j; } } } printf("max=%d maxRow=%d maxCol=%d", max, maxRow, maxCol); return 0; }
A
BBB
CCCCC
DDDDDDD
EEEEEEEEE
FFFFFFF
GGGGG
HHH
I
#include<stdio.h> void main() { char c = 'A'; int i, j, k, n; scanf("%d", &n); printf("对应的菱形\n"); //正三角形 for (i = 1; i <= n; i++) { for (j = 0; j < n - i; j++) { printf(" "); } for (j = 0; j < 2 * i - 1; j++) { printf("%c", c); } c++; //单独出来避免c在循环里一直++ printf("\n"); } //反三角形 for (i = n - 1; i >= 1; i--) { for (j = n - i; j > 0; j--) { printf(" "); } for (j = 2 * i - 1; j > 0; j--) { printf("%c", c); } c++; printf("\n"); } } //方法二 #include<stdio.h> void main() { char c = 'A'; int i, j, k, n; scanf("%d", &n); printf("对应的菱形\n"); for (k = 1 - n; k <= n - 1; k++) { i = n - abs(k); for (j = 1; j <= n - i + 1; j++) { printf(" "); } for (j = 1; j <= 2 * i - 1; j++) { printf("%c", c); } c++; printf("\n"); } }
#include<stdio.h> void main() { double sum = 0; int x = 1, y = 2, i, temp; for (i = 1; i <= 50; i++) { sum += (double)y / x; temp = x; x = y; y = temp + y; } printf("%.2f", sum); } //方法二 #include<stdio.h>//头文件 int main()//主函数 { int i;//定义整型变量,用于for循环 double a=2,b=1,sum=0,temp;//定义双精度浮点型变量 for(i=1;i<=20;i++)//for循环 { sum=sum+a/b;//分母为前面的分子和分母之和 temp=a; a=a+b; b=temp;//分子为前面的分母 } printf("sum=%7.7f\n",sum);//输出和 return 0;//主函数返回值为0 }
例如:如果a数组中的元素为:1,5,2,5,6,8,7,4,3,0,当n=5时,程序的输出结果为:5isNo.3。当n=10时,程序的输出结果应为:10notfound!。
#include<stdio.h> void main() { int a[10] = { 1,5,2,5,6,8,7,4,3,0 }; int i, k = 0, n, f = 0; scanf("%d", &n); for (i = 0; i < 10; i++) { if (a[9 - i] == n) { f = 1; k = 9 - i; break; } } if (f) { printf("%disNo.%d", n, k); } else { printf("%dnotfound!", n); } }
#include<stdio.h> int main() { char str[50]; int i = 0, num = 0, flag = 0; printf("请输入一行字符:"); gets(str); for (i = 0; str[i] != '\0'; i++) { if (str[i] == ' ') //判断字符是否为空格,是赋值为0 flag = 0; else { if (flag == 0) //判断flag是否等于0 flag = 0就是遇到空格的时候 1是字母 { num++; //单词的个数加1 flag = 1; } } } printf("单词的个数是:%d", num); }
例如,当一维数组a中的元素为:1,4,2,7,3,12,5,34,5,9,程序的输出应为Theminis:1,posis:0。数组a可直接在代码中赋值。
#include<stdio.h>
void main() {
int a[] = { 1,4,2,7,3,12,5,34,5,9 };
int i, min = a[0], pos = 0;
for (i = 1; i < sizeof(a) / sizeof(int); i++)
{
if (min > a[i])
{
min = a[i];
pos = i;
}
}
//printf("最小的值是%d,下标是%d", min, pos);
printf("Theminis:%d,posis:%d。", min, pos);
}
#include<stdio.h>
#define PI 3.1415926
int main() {
float r, area, s;
printf("请输入圆的半径r=");
scanf("%f", &r);
area = PI * r * r;
s = 2 * PI * r;
printf("圆的面积是%f,周长是%f", s, area);
return 0;
}
例如当字符串中的内容为“abcdabfabcdx”,t中内容为“ab”时输出结果应是“abcdx”。当字符串中的内容为“abcdabfabcdx”,t中内容为“abd”时,则程序输出未找到信息“not be found!”。请改正程序中的错误.使它能得出正确的结果。
#include <stdio.h> #include <string.h> char* fun(char* s, char* t) { char* p, * r, * a; a = NULL; while (*s) { p = s; r = t; while (*r) if (*r == *p) { r++; p++; } else break; if (*r == '\0') a = s; s++; } return a; } int main() { char s[100], t[100], * p; printf("\nPlease enter string S :"); scanf("%s", s); printf("\nPlease enter substring t :"); scanf("%s", t); p = fun(s, t); if (p) printf("\nThe result is : %s\n", p); else printf("\nNot found !\n"); return 0; }
#include <stdio.h> int main() { int letter = 0, space = 0, digit = 0, other = 0; char c; while ((c = getchar()) != '\n') { if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) letter++; else if (c == ' ') space++; else if ('0' <= c && c <= '9') digit++; else other++; } printf("英文字母:%d\n空格:%d\n数字:%d\n其它字符:%d\n", letter, space, digit, other); return 0; }
例如s1为"abcaa63akdfk",s2为"ayk5",程序运行后输出:bc63df。
#include <stdio.h> int main() { int i, j, k; char s1[] = "abcaa63akdfk"; char s2[] = "ayk5"; for (int i = 0; s1[i] != '\0'; i++) { for (j = 0; s2[j] != '\0'; j++) { if (s1[i] == s2[j]) { for (int k = i; s1[k] != '\0'; k++) { //巧妙运用字符串’\0’ s1[k] = s1[k + 1]; //后面的值将前面的值进行覆盖掉 } } } } puts(s1); return 0; }
#include<stdio.h> int main() { int k, s, m, n; for (n = 1; n <= 100; n++) { k = 1, s = 0; m = n; while (m) { k = k * (m % 10); // 积 s = s + (m % 10); //和 m = m / 10; } if (k > s) // 比较 printf("%d ", n); } return 0; }
//方法一 #include<stdio.h> #define N 20 void main() { int i, j, a[N], max1, max2; for ( i = 0; i < N; i++) { scanf("%d", &a[i]); } //打擂台法求最大值,大的当擂主 max1 = a[0]; for ( i = 1; i < N; i++) { if (a[i] > max1) { max1 = a[i]; j = i;//提取擂主的下标 } } for ( i = j; i < N-1; i++) { a[i] = a[i + 1];//后面一个数覆盖擂主的数,去除擂主 } //继续打擂,求次大值 max2 = a[0]; for (i = 1; i < N-1; i++) { if (a[i] > max2) { max2 = a[i]; } } printf("最大值%d,次大值%d",max1, max2); } //方法二 #include<stdio.h> #define N 20 void main() { int i, j, a[N],t; for (i = 0; i < N; i++) { scanf("%d", &a[i]); } //使用冒泡排序但是只排序两次 就是把最大的和次大的放后面 for (i = 0; i < 2; i++) { for ( j = 0; j < N-1-i; j++) { if (a[j] > a[j+1]) { t = a[j]; a[j] = a[j + 1]; a[j+1] = t; } } } printf("最大值%d,次大值%d", a[N-1], a[N-2]); }
#include<stdio.h> int main() { int n, member[10], count = 0, temp; int i = 0; printf("Input number of person:n="); scanf("%d", &n); for (int i = 0; i < n; i++) member[i] = i + 1;//把人员编号存进去 temp = n; while (n > 1)//1234 { if (member[i] != 0) count++; if (count == 3) { member[i] = 0; count = 0; n--; //1234 1204 1004 1 } i++;// 不能放在member[] 里面 //printf("temp:%d\n",temp);temp的值并不会随着n的自减而自减 if (i == temp) i = 0;//temp = n 是为了让剩余的继续按着123 编号报数 , //编号4报了数1 是不是该让编号为1 的人报数 那是不是下标就要指向0 ,因为member [0]=1 } for (int i = 0; i < temp; i++) { if (member[i] != 0) printf("The last one is %d", member[i]); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。