记录大一学习C语言和C++时的课堂作业
一、循环结构程序设计
二、数组
三、自定义函数
四、指针
五、结构体
六、数据读取和数据存储
七、基础算法题
#include
int main(void)
{
int s = 0, i = 0;
for (i = 1; i <= 100; i++)
{
s += i;
}
printf("%d\n", s);
return 0;
}
#include
int main(void)
{
int s = 0, i = 0;
for (i = 1; i <= 99; i = i + 2)
{
s += i;
}
printf("%d\n", s);
return 0;
}
#include
#include
int main(void)
{
int s = 0, i = 0;
for (i = 1; i <= 50; i++)
{
s += (2 * i - 1) * pow(-1, i - 1);
}
printf("%d\n", s);
return 0;
}
#include
int main(void)
{
double s = 0, j = 1, i = 0;
for (i = 1; 1 / (2 * i - 1) >= 1E-6; i++)
{
s += j / (2 * i - 1);
j = -j;
}
printf("π的值约为%lf\n", 4 * s);
return 0;
}
#include
int main(void)
{
double k = 1, sum = 0;
//如果对精度有要求可以将double换成long long int,相应的输出数据类型改为%lld
int i = 0;
for (i = 1; i <= 20; i++)
{
k *= i;
sum += k;
}
printf("%.0lf\n", sum);
return 0;
}
#include
int main(void)
{
double s = 1, sum = 0;
int i = 0;
for (i = 1; i <= 19; i++)
{
s *= i;
if (i % 2 == 1) //保留奇数的阶乘进行累加
sum += s;
}
printf("%.0lf\n", sum);
return 0;
}
#include
#include
int main(void)
{
double x = 0, y = 0, i = 0, j = 1, jc = 1;//其中jc表示阶乘的值
printf("输入x的值【角度】:");
scanf("%lf", &x);
x = x * 3.1415926 / 180;
while (fabs(pow(-1, i) * pow(x, j) / jc) >= 0.000001)
{
y += pow(-1, i) * pow(x, j) / jc;
i++;
j = j + 2;
jc *= (j - 1) * j;
}
printf("sinx=%lf\n", y);
return 0;
}
#include
#include
int main(void)
{
double i = 1, jc = 1, sum = 0, x = 0;
printf("输入x的值【弧度】:");
scanf("%lf", &x);
while (pow(x, i) / jc >= 0.000001)
{
sum += pow(-1, (i - 1) / 2) * pow(x, i) / jc;
i = i + 2;
jc *= i * (i - 1);
}
printf("则sinx=%lf\n", sum);
return 0;
}
#include
int main(void)
{
double a[41] = {
1,1 };
int i = 0;
double sum = 0;
for (i = 2; i <= 40; i++)
{
a[i] = a[i - 2] + a[i - 1];
}
for (i = 0; i <= 39; i++)
{
sum += a[i + 1] / a[i];
}
printf("%lf\n", sum);
return 0;
}
#include
#include
int main(void)
{
double sum = 0, k = 0;
int i = 0, j = 0, n = 0;
printf("输入n的值:");
scanf("%d", &n);
for (i = 0; i <= n - 1; i++)
{
k = 0;
for (j = 0; j <= i; j++)
{
k += pow(10, j);
}
sum += k;
}
printf("%.0lf\n", sum);
return 0;
}
#include
int main(void)
{
double k = 0, s = 0;
int i = 0, n = 0, m = 0;
printf("输入n的值:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (m = 1; m <= 1000000; m = 10 * m)
{
if (i >= m && i <= 10 * m - 1)
s = 10 * m * s + i;
}
k += s;
}
printf("%.0lf\n", k);
return 0;
}
#include
int main(void)
{
int i = 0;
double t = 0, ave, sum = 0, max = 0;
printf("输入各个学生的C语言成绩(输入负数时结束):\n");
while (scanf("%lf", &t)) //scanf返回参数的个数,也就是1
{
if (t < 0) break;
if (max < t) max = t;
sum += t;
i++;
}
ave = sum / i;
printf("平均成绩为:%lf,最高成绩为:%lf\n", ave, max);
return 0;
}
#include
int main(void)
{
int n = 0, i = 0, flag = 1;
printf("输入一个整数:");
scanf("%d", &n);
if (n <= 1) flag = 0;
for (i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
flag = 0;
break;
}
}
if (flag == 0) printf("它不是质数\n");
else printf("它是质数\n");
return 0;
}
#include
int main(void)
{
int n = 0, i = 0;
printf("输入一个整数:");
scanf("%d", &n);
for (i = 2; i <= n - 1; i++)
{
if (n % i == 0)
break;
}
/*通过判断以上循环是否完成来确定输入的数是否质数*/
if (i == n) printf("它是质数\n");
else printf("它不是质数\n");
return 0;
}
#include
int main(void)
{
int x = 0, i = 0, n = 0;
printf("输入一个整数:");
scanf("%d", &x);
for (i = 1; i <= x; i++)
{
if (x % i == 0)
n++;
}
if (n == 2) printf("它是质数\n");
else printf("它不是质数\n");
return 0;
}
#include
int main(void)
{
int x = 0, i = 0;
for (x = 2; x <= 1000; x++)
{
int flag = 1;
for (i = 2; i * i <= x; i++)
{
if (x % i == 0)
{
flag = 0;
break;
}
}
if (flag == 1)//此条件值为真,证明了x是素数
printf("%d\n", x);
}
return 0;
}
#include
int main(void)
{
int x = 0, i = 0, flag = 1, num = 0, z = 0;
printf("输入一个正整数:");
scanf("%d", &z);
printf("比这个数小的最大的十个质数为:\n");
for (x = z; x >= 2; x--)
{
flag = 1;
for (i = 2; i * i <= x; i++)
{
if (x % i == 0)
{
flag = 0;
break;
}
}
if (flag == 1)//此条件值为真,证明了x是素数
{
num++;//先计算个数
if (num <= 10)//再判断是否在10个以内,如果是,则输出
printf("%d\n", x);
else
break;//输出10个素数就终止循环!
}
}
return 0;
}
#include
int main(void)
{
int x = 0, y = 0, i = 0;
printf("输入两个正整数:");
scanf("%d%d", &x, &y);
//求最大公约数
for (i = x; i >= 1; i--)
{
if (x % i == 0 && y % i == 0)
break;
}
printf("最大公约数:%d\n", i);
printf("最小公倍数:%d\n", x * y / i);
return 0;
}
#include
int main(void)
{
int n = 1;
while (!(n % 2 == 1 && n % 3 == 2 && n % 5 == 4 && n % 6 == 5 && n % 7 == 0))
{
n++;//求出的值为满足条件的最小值
}
printf("%d\n", n);
return 0;
}
#include
int main(void)
{
int x = 0, a[32] = {
0 }, i = 0, y = 0;
printf("Input decimal number:");
scanf("%d", &x); //带符号的32位二进制数转换为十进制的范围为[-2147483647,2147483647]
if (x < 0)
a[31] = 1;
y = x > 0 ? x : -x; //将x的绝对值赋值给y
while (y > 0)
{
a[i] = y % 2;
i++;
y = y / 2;
}
if (x < 0) //对于负数,这里计算的是负数的二进制补码
{
for (i = 30; i >= 0; i--)
{
a[i] = !a[i]; //除开符号位以外其余各位按位求反 1->0 0->1
}
a[0]++; //末位+1
for (i = 1; i <= 30; i++) //处理进位
{
a[i] += a[i - 1] / 2;
a[i - 1] = a[i - 1] % 2;
}
a[30] = a[30] % 2;
}
printf("Output binary number:");
for (i = 31; i >= 0; i--)
{
printf("%d", a[i]);
}
return 0;
}
#include
int main(void)
{
int i = 2010;
double m = 14.536, z = 5.845;
while (m >= z)
{
z = z * 1.101;
m = m * 1.033;
i++;
}
printf("到%d年中国的GDP有望超过美国\n", i);
return 0;
}
#include
int main(void)
{
int a = 0, b = 0, t = 0, i = 0, sum = 0;
printf("输入a和b的值(以空格分开):");
scanf("%d%d", &a, &b);
if (a > b) //保证a不大于b
{
t = a;
a = b;
b = t;
}
for (i = a; i <= b; i++)
sum += i;
printf("a到b的累加和为:%d\n", sum);
return 0;
}
#include
int main(void)
{
double s = 150, m = 1, i = 1;
while (i <= 10)
{
m = m * 1.1;
s = s * 1.06;
i++;
}
printf("张三的存款现在可以购买%lf平米的房子\n", s / m);
return 0;
}
#include
int main(void)
{
int n = 1, s = 0;
while (s != 300)
{
n++;
s = s + (n - 1); //握手定理
}
printf("%d\n", n);
return 0;
}
#include
int main(void)
{
int x = 0, i = 0, s = 0;
for (x = 1; x <= 1000; x++)
{
s = 0;
for (i = 1; i <= x / 2; i++)
{
if (x % i == 0)
s = s + i;
}
if (x == s) printf("%d\n", x);
}
return 0;
}
C n m = n ! m ! ( n − m ) ! C n m = C n − 1 m + C n − 1 m − 1 \begin{aligned} &{C}_{n}^{m}=\frac{n!}{m!(n-m)!}& \\\\ &{C}_{n}^{m}={C}_{n-1}^{m}+{C}_{n-1}^{m-1}& \end{aligned} Cnm=m!(n−m)!n!Cnm=Cn−1m+Cn−1m−1
#include
double C(int n, int m) //通过递归求解组合C(n,m)的值
{
if (m == 0 || m == n)
return 1;
else
return C(n - 1, m) + C(n - 1, m - 1);
}
int main(void)
{
int white = 0, red = 0, black = 0;
double p = 0;
for (white = 1; white <= 5; white++) //要求至少有一个白球,所以white的值从1开始
{
for (red = 0; red <= 3; red++)
{
for (black = 0; black <= 6; black++)
{
p = C(5, white) * C(3, red) * C(6, black) / C(14, 8); //通过组合求概率
if (white + red + black == 8)
printf("%d个白球,%d个红球,%d个黑球的概率为:%lf\n", white, red, black, p);
}
}
}
return 0;
}
#include
int main(void)
{
int c = 0, r = 0, i = 0;
for (c = 0; c <= 36; c++)
{
for (r = 0; r <= 36; r++)
{
if (2 * c + 4 * r == 100 && c + r == 36)
printf("鸡:%d只 兔:%d只\n", c, r);
}
}
return 0;
}
#include
int main(void)
{
int i = 0, sum = 0, a1 = 0, a2 = 0, a3 = 0;
for (i = 100; i <= 999; i++)
{
a1 = i % 10; //个位
a2 = i / 10 % 10; //十位
a3 = i / 100 % 10; //百位
if (i % 2 == 0 && a1 != a2 && a1 != a3 && a2 != a3)
sum++;
}
printf("%d\n", sum);
return 0;
}
#include
int main(void)
{
int i = 0, j = 0;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d*%d=%d\t", j, i, i * j);
}
printf("\n");
}
return 0;
}
#include
int main(void)
{
int a = 0, b = 0, c = 0, sum = 0;
for (a = 0; a <= 100; a++)
{
for (b = 0; b <= 50; b++)
{
for (c = 0; c <= 20; c++)
{
if (a + b + c == 70 && a + 2 * b + 5 * c == 100)
{
sum++;
printf("第%d种方案:1分%d枚,2分%d枚,5分%d枚\n", sum, a, b, c);
}
}
}
}
return 0;
}
#include
int narcissistic(int number); //判断是否是水仙花数
void PrintN(int m, int n); //打印m和n之间的水仙花数
int main(void)
{
int m = 0, n = 0;
scanf("%d %d", &m, &n);
PrintN(m, n);
return 0;
}
int narcissistic(int number)
{
int a[50] = {
0 }, i = 0, j = 0, sum = 0, k = 0, t = 1;
int n = number;
while (number > 0)
{
a[i++] = number % 10;
number /= 10;
}
for (j = 0; j < i; j++)
{
t = 1;
for (k = 0; k < i; k++)
t *= a[j];
sum += t;
}
if (sum == n) return 1;
else return 0;
}
void PrintN(int m, int n)
{
int i = 0;
for (i = m + 1; i <= n - 1; i++)
{
if (narcissistic(i) == 1)
printf("%d\n", i);
}
}
#include
int main(void)
{
int k = 0, x = 0, i = 0;
for (i = 1; i <= 4; i++) //第1至4行
{
for (k = 1; k <= 6 - i; k++)
printf(" ");
for (x = 1; x <= 2 * i - 1; x++)
printf("*");
printf("\n");
}
for (i = 3; i >= 1; i--) //第5至7行
{
for (k = 1; k <= 6 - i; k++)
printf(" ");
for (x = 1; x <= 2 * i - 1; x++)
printf("*");
printf("\n");
}
return 0;
}
#include
int main(void)
{
int y = 0, sum = 0, i = 0;
for (y = 1; y <= 236; y++)
{
sum = 0; //表示吹熄的蜡烛总数
for (i = y; i <= 236; i++) //从第y岁开始吹
{
sum += i;
if (sum == 236)
printf("从%d岁开始过生日\n", y);
else if (sum > 236)
break;
}
} //运行结束后可以得到26和236两个答案,理论都可行,但236实际不可能,故答案为26
return 0;
}
#include
int main(void)
{
int year = 0, month = 0, day = 0, s = 0, i = 0;
printf("输入年月日(中间用空格隔开): ");
scanf("%d%d%d", &year, &month, &day);
i = 1;
while (i <= month - 1) //计算所有整月的总天数
{
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10)
s = s + 31;
else if (i == 4 || i == 6 || i == 9 || i == 11)
s = s + 30;
else if (i == 2)
s = s + 28;
i++;
}
s = s + day; //加上该非整月的day天
if ((year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month >= 3)
s++; //如果为闰年,并且月份在2月之后,则s+1
printf("%d\n", s);
}
#include
int DateNumber(int year, int month, int day)
{
//函数DateNumber的作用是计算日期year-month-day是该年的第几天
int i = 1, s = 0;
while (i <= month - 1) //计算所有整月的总天数
{
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10)
s = s + 31;
else if (i == 4 || i == 6 || i == 9 || i == 11)
s = s + 30;
else if (i == 2)
s = s + 28;
i++;
}
s = s + day; //加上该日期所处非整月的day天
if ((year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month >= 3)
s++; //如果为闰年,并且月份在2月之后,则s+1
return s;
}
int main(void)
{
int y1 = 0, m1 = 0, d1 = 0, y2 = 0, m2 = 0, d2 = 0, s1 = 0, s2 = 0, i = 0, result = 0;
printf("输入起始年月日(用空格隔开): ");
scanf("%d%d%d", &y1, &m1, &d1);
printf("输入终止年月日(用空格隔开): ");
scanf("%d%d%d", &y2, &m2, &d2);
s1 = DateNumber(y1, m1, d1); //调用函数DateNumber计算日期y1-m1-d1处于y1年的第几天
s2 = DateNumber(y2, m2, d2); //调用函数DateNumber计算日期y2-m2-d2处于y2年的第几天
//总天数=起始年剩的天数+中间所有年的天数+终止年已经过了的天数
result = 365 - s1 + 365 * (y2 - y1 - 1) + s2;//先默认都是平年,后面再纠正由于闰年带来的误差
for (i = y1 + 1; i <= y2 - 1; i++)
{
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
result++;
}
if ((y1 % 4 == 0 && y1 % 100 != 0 || y1 % 400 == 0) && (y1 != y2))
result++;
printf("从起始日至终止日共经过%d天\n", result);
return 0;
}
{ w = [ y e a r − 1 ] + [ ( y e a r − 1 ) / 4 ] − [ ( y e a r − 1 ) / 100 ] + [ ( y e a r − 1 ) / 400 ] + S S = S ( y e a r , m o n t h , d a y ) \left\{ \begin{aligned} w&=[year-1]+[(year-1)/4]-[(year-1)/100]+[(year-1)/400]+S& \\\\ S&=S(year,month,day)& \end{aligned} \right. ⎩⎪⎨⎪⎧wS=[year−1]+[(year−1)/4]−[(year−1)/100]+[(year−1)/400]+S=S(year,month,day)
#include
int DateToWeek(int year, int month, int day)
{
int i = 1, s = 0, week = 0;
while (i <= month - 1) //计算所有整月的总天数
{
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10)
s = s + 31;
else if (i == 4 || i == 6 || i == 9 || i == 11)
s = s + 30;
else if (i == 2)
s = s + 28;
i++;
}
s = s + day; //加上该日期所处非整月的day天
if ((year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month >= 3)
s++;
week = ((year - 1) + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + s) % 7;
return week;
}
int main(void)
{
int y = 0, m = 0, d = 0, w = 0, i = 0;
printf(" 输入年份:");
scanf("%d", &y);
printf("\n");
int Days[12] = {
31,28,31,30,31,30,31,31,30,31,30,31 };
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
Days[1]++; //闰年2月加一天
for (m = 1; m <= 12; m++)
{
printf(" %4d年%2d月\n", y, m);
printf(" --------------------------\n");
w = DateToWeek(y, m, 1); //该年每个月1号为星期w
printf(" 一 二 三 四 五 六 日\n"); //中式日历排版
for (i = 1; i <= (w - 1 + 7) % 7; i++)
printf(" "); //每次循环打印4个空格,循环次数由w决定
for (i = 1; i <= Days[m - 1]; i++)
{
printf("%4d", i);
if ((w + i - 1 + 7) % 7 == 0) //换行依据和w有关
printf("\n");
}
printf("\n\n");
}
return 0;
}
{ w = ( [ p 4 ] − 2 p + q + [ q 4 ] + [ 13 × ( m o n t h + 1 ) 5 ] + d a y − 1 ) m o d 7 p = [ y e a r 100 ] q = y e a r m o d 100 \left\{ \begin{aligned} w&=([\frac{p}{4}]-2p+q+[\frac{q}{4}]+[\frac{13×(month+1)}{5}]+day-1)\bmod 7& \\\\ p&=[\frac{year}{100}]& \\\\ q &=year\bmod 100& \end{aligned} \right. ⎩⎪⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎧wpq=([4p]−2p+q+[4q]+[513×(month+1)]+day−1)mod7=[100year]=yearmod100
#include
int DateToWeek(int year, int month, int day)
{
if (month == 1 || month == 2) //判断month是否为1或2
{
year--;
month += 12; //蔡勒公式需要把当年的1月和2月视作前一年的13月和14月
}
int ye = year / 100;
int ar = year % 100;
int week = ye / 4 - 2 * ye + ar + ar / 4 + 26 * (month + 1) / 10 + day - 1;
while (week < 0)
week += 7;
week %= 7;
return week;
}
int main(void)
{
int y = 0, m = 0, d = 0, w = 0, i = 0;
printf(" 输入年份:");
scanf("%d", &y);
printf("\n");
int Days[12] = {
31,28,31,30,31,30,31,31,30,31,30,31 };
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
Days[1]++; //闰年2月加一天
for (m = 1; m <= 12; m++)
{
printf(" %4d年%2d月\n", y, m);
printf(" --------------------------\n");
w = DateToWeek(y, m, 1); //该年每个月1号为星期w
printf(" 日 一 二 三 四 五 六\n"); //西式日历排版
for (i = 1; i <= w; i++)
printf(" "); //每次循环打印4个空格,循环次数由w决定
for (i = 1; i <= Days[m - 1]; i++)
{
printf("%4d", i);
if ((w + i) % 7 == 0)
printf("\n");
}
printf("\n\n");
}
return 0;
}
#include
int main(void)
{
double h[6] = {
0 }, t = 0;
int i = 0, j = 0;
for (i = 0; i <= 5; i++)
{
printf("输入第%d个同学的身高:", i + 1);
scanf("%lf", &h[i]);
}
for (j = 0; j <= 4; j++)
{
for (i = 0; i <= 4 - j; i++)
{
if (h[i] > h[i + 1])
{
t = h[i];
h[i] = h[i + 1];
h[i + 1] = t;
}
}
} //冒泡排序法,考试重点
printf("从小到大排列为:\n");
for (i = 0; i <= 5; i++)
printf("%.2lf ", h[i]);
return 0;
}
#include
int main(void)
{
double h[33] = {
0 }, t = 0;
int i = 0, j = 0;
for (i = 0; i <= 32; i++)
{
printf("输入第%d个同学的C语言成绩:", i + 1);
scanf("%lf", &h[i]);
}
for (j = 0; j <= 31; j++)
{
for (i = 0; i <= 31 - j; i++)
{
if (h[i] > h[i + 1])
{
t = h[i];
h[i] = h[i + 1];
h[i + 1] = t;
}
}
}
printf("两个最好成绩为:%lf和%lf\n", h[31], h[32]);
return 0;
}
#include
#include
int main(void)
{
double t = 0, ave = 0, sum = 0;
int i = 0, j = 0, n = 0;
printf("输入裁判数量:");
scanf("%d", &n);
double* a = (double*)malloc(8 * n); //动态申请内存;
for (i = 0; i <= n - 1; i++)
{
printf("请第%d个裁判打分:", i + 1);
scanf("%lf", &a[i]);
}
for (j = 0; j <= n - 2; j++)
{
for (i = 0; i <= n - 2 - j; i++)
{
if (a[i] > a[i + 1])
{
t = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
}
}
}
for (i = 1; i < n - 1; i++)
sum = sum + a[i];
ave = sum / (n - 2); //计算平均分
printf("去掉最高分和最低分,选手最后平均得分为%lf\n", ave);
free(a);
return 0;
}
#include
#include
int main(void)
{
double h[10] = {
0 }, k[10] = {
0 };
double ave = 0, sum = 0, min = 0;
int i = 0;
for (i = 0; i <= 9; i++)
{
printf("输入第%d个同学的身高:", i + 1);
scanf("%lf", &h[i]);
sum = sum + h[i];
}
ave = sum / 10;
printf("平均身高为%lf\n", ave);
for (i = 0; i <= 9; i++)
k[i] = fabs(h[i] - ave); //求出各身高与平均值的差的绝对值
min = k[0];
for (i = 1; i <= 9; i++)
{
if (min > k[i])
min = k[i]; //求十个差值中的最小值
}
for (i = 0; i <= 9; i++)
{
if (fabs(ave - h[i]) == min) //找出满足要求的那个身高
printf("与平均值最接近的身高为%lf\n", h[i]);
}
return 0;
}
#include
int main(void)
{
int x[33] = {
0 }, i = 0, a = 0, b = 0, c = 0, d = 0, e = 0;
for (i = 0; i <= 32; i++)
{
printf("第%d个同学的成绩【0-100】为:", i + 1);
scanf("%d", &x[i]);
while (x[i] > 100 || x[i] < 0) //纠错功能
{
printf("你输入的分数不正确,请你重新输入第%d个同学的成绩[0,100]:", i + 1);
scanf("%d", &x[i]);
}
}
for (i = 0; i <= 32; i++)
{
if (90 <= x[i] && x[i] <= 100)
a++;
else if (80 <= x[i])
b++;
else if (70 <= x[i])
c++;
else if (60 <= x[i])
d++;
else if (0 <= x[i])
e++;
}
printf("优秀%d人,占比%lf%%\n", a, (double)a / 33 * 100);
printf("良好%d人,占比%lf%%\n", b, (double)b / 33 * 100);
printf("中等%d人,占比%lf%%\n", c, (double)c / 33 * 100);
printf("及格%d人,占比%lf%%\n", d, (double)d / 33 * 100);
printf("不及格%d人,占比%lf%%\n", e, (double)e / 33 * 100);
return 0;
}
#include
int main(void)
{
int x = 0, k = 0, a = 0, i = 0;
printf("输入一个整数:");
scanf("%d", &k);
x = k > 0 ? k : -k;
printf("逆序排列后为:");
if (k < 0)
printf("-");
if (k == 0)
printf("0");
while (x > 0)
{
a = x % 10;
printf("%d", a);
x = x / 10;
}
printf("\n");
return 0;
}
#include
int main(void)
{
int a[40] = {
0 }, i;
for (i = 0; i <= 39; i++)
{
if (i == 0 || i == 1)
a[i] = 1;
else if (i >= 2)
a[i] = a[i - 1] + a[i - 2];
printf("%d\n", a[i]);
}
return 0;
}
7 4 8 9 1 5
5 7 4 8 9 1
1 5 7 4 8 9
9 1 5 7 4 8
8 9 1 5 7 4
4 8 9 1 5 7
7 4 8 9 1 5
#include
int main(void)
{
int a[6][6] = {
0 };
int i = 0, j = 0, m = 0;
printf("任意输入六个数值:");
for (i = 0; i <= 5; i++)
scanf("%d", &a[5][i]);
for (i = 0; i <= 5; i++)
{
for (j = 0; j <= 5; j++)
{
for (m = 0; m <= 5; m++)
{
if (i - j == 5 - m || i - j == -1 - m) //计算二维数组各个变量的值
a[i][j] = a[5][m];
}
}
}
for (i = 0; i <= 5; i++)
{
for (j = 0; j <= 5; j++)
{
printf("%d\t", a[i][j]); //二维数组的矩阵形式输出
}
printf("\n");
}
return 0;
}
#include
int main(void)
{
double s[6][7] = {
0 };
int i = 0, j = 0;
for (i = 0; i < 6; i++)
{
printf("输入第%d位同学的5门成绩(以空格分开):", i + 1);
for (j = 0; j < 5; j++)
{
scanf("%lf", &s[i][j]);
s[i][5] += s[i][j];
}
s[i][6] = s[i][5] / 5;
}
printf("\n");
printf("%-10s", "科目");
printf("%-10s", "高数");
printf("%-10s", "思修");
printf("%-10s", "英语");
printf("%-10s", "离散");
printf("%-10s", "C语言");
printf("%-10s", "总分");
printf("%-10s", "平均分");
printf("\n");
for (i = 0; i < 6; i++)
{
printf("%-10s", "成绩");
for (j = 0; j < 7; j++)
{
printf("%-10.0lf", s[i][j]);
}
printf("\n");
}
return 0;
}
#include
int main(void)
{
int a[3][4] = {
0 }, b[4][3] = {
0 }, i = 0, j = 0;
printf("输入3行4列的矩阵的值:\n");
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= 3; j++)
{
scanf("%d", &a[i][j]);
}
}
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= 3; j++)
{
b[j][i] = a[i][j];
}
}
printf("其转置矩阵为:\n");
for (j = 0; j <= 3; j++)
{
for (i = 0; i <= 2; i++)
{
printf("%d\t", b[j][i]);
}
printf("\n");
}
return 0;
}
#include
int main(void)
{
int a[4][5] = {
0 }, i = 0, j = 0, t = 0, max = 0;
printf("输入4行5列矩阵的值:\n");
for (i = 0; i <= 3; i++)
{
for (j = 0; j <= 4; j++)
{
scanf("%d", &a[i][j]);
}
}
max = a[0][0];
for (i = 0; i <= 3; i++)
{
for (j = 0; j <= 4; j++)
{
if (a[i][j] > max)
{
max = a[i][j];
}
}
}
for (i = 0; i <= 3; i++)
{
for (j = 0; j <= 4; j++)
{
if (a[i][j] == max)
{
printf("最大值为a[%d][%d]=%d\n", i, j, max);
}
}
}
return 0;
}
5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
#include
int main(void)
{
int a[100][100] = {
0 }, i = 0, j = 0, n = 0;
printf("输入n的值:");
scanf("%d", &n);
printf("%d行%d列的杨辉三角为:\n", n, n);
for (i = 0; i <= n - 1; i++)
{
for (j = 0; j <= i; j++)
{
if (j == 0 || i == j)
a[i][j] = 1;
else
a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
}
}
for (i = 0; i <= n - 1; i++)
{
for (j = 0; j <= i; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
return 0;
}
#include
#include
int main(void)
{
int i = 0, j = 0, n = 0, m = 0, x = 0, y = 0, min = 0, max = 0, flag = 0;
printf("输入矩阵的行数m和列数n:");
scanf("%d%d", &m, &n);
int** a;
a = (int**)malloc(sizeof(int*) * m);
for (i = 0; i < m; i++)
a[i] = (int*)malloc(sizeof(int) * n); //为m行n列的二维数组动态开辟空间
printf("输入矩阵各个元素的值(以空格分隔):\n");
for (i = 0; i <= m - 1; i++)
{
for (j = 0; j <= n - 1; j++)
scanf("%d", &a[i][j]);
}
for (i = 0; i <= m - 1; i++)
{
for (j = 0; j <= n - 1; j++)
{
max = a[i][0];
for (y = 0; y <= n - 2; y++)
{
if (a[i][y] < a[i][y + 1])
max = a[i][y + 1];
}
min = a[0][j];
for (x = 0; x <= m - 2; x++)
{
if (a[x][j] > a[x + 1][j])
min = a[x + 1][j];
}
if (max == a[i][j] && min == a[i][j])
{
printf("YES,鞍点值为%d,坐标为第%d行第%d列\n", a[i][j], i, j);
flag = 1;
}
}
}
if (flag == 0)
printf("NO\n");
for (i = 0; i < m; i++)
free(a[i]);
free(a);
return 0;
}
#include
#include
int main(void)
{
char E[5000];
int sum = 0, i = 0;
printf("输入一段英文:");
gets(E);
for (i = 0; i <= strlen(E) - 1; i++)
{
if (E[i] == 'A' || E[i] == 'a')
sum++;
}
printf("英文字符a(不区分大小写)的个数是%d\n", sum);
return 0;
}
#include
#include
int main(void)
{
char E[5000];
int sum = 0, i = 0, j = 0;
printf("输入一段英文:");
gets(E);
for (j = 65; j <= 90; j++) //这里是A到Z在ASCII码表中的范围
{
sum = 0;
for (i = 0; i <= strlen(E) - 1; i++)
{
if (E[i] == j || E[i] == j + 32) //对数组E中每个位置上的字符进行依次判断属于A 或者 a
sum++;
}
printf("英文字符%c的个数是%d\n", j, sum);
}
return 0;
}
#include
#include
int main(void)
{
char E[5000];
int a = 0, b = 0, c = 0, d = 0, i = 0;
printf("输入一段英文:");
gets(E);
for (i = 0; i <= strlen(E) - 1; i++)
{
if (E[i] >= 'A' && E[i] <= 'Z' || E[i] >= 'a' && E[i] <= 'z')
a++;
else if (E[i] >= '0' && E[i] <= '9')
b++;
else if (E[i] == ' ')
c++;
else
d++;
}
printf("英文字符个数是%d\n", a);
printf("数字字符个数是%d\n", b);
printf("空格字符个数是%d\n", c);
printf("其他字符个数是%d\n", d);
return 0;
}
#include
#include
int main(void)
{
char E[5000];
int sum = 0, i = 0;
printf("输入一段英文:");
gets(E);
sum = 0;
for (i = 0; i <= strlen(E) - 1; i++)
{
if (E[i] >= 65 && E[i] <= 90 || E[i] >= 97 && E[i] <= 122) //左边是英文字母
if (!(E[i + 1] >= 65 && E[i + 1] <= 90 || E[i + 1] >= 97 && E[i + 1] <= 122)) //右边不是英文字母
if (E[i + 1] != '-') //右边也不是符号"-"
sum++;
}
printf("单词个数是%d\n", sum);
return 0;
}
#include
#include
void delete_zf(char zfc[])
{
int i = 0, j = 0, n = 0;
n = strlen(zfc); //计算字符串长度
for (i = 0; i < n; i++)
{
if (zfc[i] >= '0' && zfc[i] <= '9')
{
for (j = i + 1; j <= n; j++)
{
zfc[j - 1] = zfc[j]; //从i+1位置开始到n位置的字符整体往前覆盖
}
i--; //删除之后i不能变,因为它这个位置放的是后面覆盖过来的且没有判断过的字符
}
}
}
int main(void)
{
char zfc[1000], del;
printf("输入一段英文:");
gets(zfc);
delete_zf(zfc);
printf("输出删除数字字符之后的英文:");
puts(zfc);
return 0;
}
#include
#include
int main(void)
{
int n = 0, i = 0, j = 0, t = 0;
char a[1000];
printf("输入一个字符串:\n");
gets(a);
n = strlen(a);
for (j = 0; j <= n - 2; j++)
{
for (i = 0; i <= n - 2 - j; i++)
{
if (a[i] > a[i + 1])
{
t = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
}
}
}
printf("该字符串按照ASCII码的顺序从小到大的顺序排序为:\n");
puts(a);
return 0;
}
#include
#include
int main(void)
{
int n = 0, i = 0, j = 0, k = 0;
char a[1000];
printf("输入一个字符串:\n");
gets(a);
n = strlen(a);
for (i = 1; i <= n - 1; i++)
{
for (j = 0; j <= i - 1; j++)
{
if (a[i] == a[j])
{
for (k = i + 1; k <= n - 1; k++)
{
a[k - 1] = a[k];
}
n--; //删除某个重复字母后,更新当前的字符数组长度
i--; //防止遇到连续相同字符时漏删
}
}
}
printf("该字符串删除重复元素后为:\n");
for (k = 0; k <= n - 1; k++)
{
printf("%c", a[k]);
}
return 0;
}
#include
int isPrimeNumber(int s)
{
int i = 0;
if (s <= 1) return 0;
for (i = 2; i * i <= s; i++)
{
if (s % i == 0) return 0;
}
return 1;
}
int main(void)
{
int n = 0;
printf("输入一个整数:");
scanf("%d", &n);
if (isPrimeNumber(n) == 1)
printf("这个数是素数\n");
else
printf("这个数不是素数\n");
return 0;
}
#include
double fib(int n)
{
if (n == 1 || n == 2)
return 1;
else if (n >= 3)
return fib(n - 1) + fib(n - 2);
}
int main(void)
{
int n = 0;
printf("输入一个正整数:");
scanf("%d", &n);
printf("斐波那契数列第%d项为:%.0lf\n", n, fib(n));
return 0;
}
或者:
#include
double fib(int n)
{
return (n == 1 || n == 2) ? 1 : fib(n - 1) + fib(n - 2);
}
int main(void)
{
int n = 0;
printf("输入一个正整数:");
scanf("%d", &n);
printf("斐波那契数列第%d项为:%.0lf\n", n, fib(n));
return 0;
}
#include
int main(void)
{
double f1 = 0, f2 = 1, fn = 0;
int i = 0, n = 0;
printf("输入一个正整数:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
fn = f1 + f2;
f1 = f2, f2 = fn;
} //迭代法求斐波那契数列
printf("%.0lf", f1);
return 0;
}
#include
#include
int* sort(int* a, int n)
{
int i = 0, j = 0, t = 0;
for (j = 0; j <= n - 2; j++)
{
for (i = 0; i <= n - 2 - j; i++)
{
if (a[i] > a[i + 1])
{
t = a[i];
a[i] = a[i + 1];
a[i + 1] = t;
}
}
}
return a;
}
int main(void)
{
int n = 0, i = 0;
printf("输入要排序的数据个数:");
scanf("%d", &n);
int* p = (int*)malloc(4 * n); //为数组申请空间;
printf("请输入%d个数据:", n);
for (i = 0; i <= n - 1; i++)
{
scanf("%d", &p[i]);
}
sort(p, n); //调用排序函数
printf("按从小到大顺序排序为:");
for (i = 0; i <= n - 1; i++)
{
printf("%d ", p[i]);
}
free(p);
return 0;
}
#include
double fact(int n)
{
double s = 1;
while (n--) s *= (n + 1);
return s;
}
int main(void)
{
int n = 0;
printf("输入一个正整数:");
scanf("%d", &n);
printf("%d的阶乘为:%.0lf\n", n, fact(n));
return 0;
}
或者:
#include
double fact(int n)
{
double s = 1;
for (; n >= 1; n--)
s *= n;
return s;
}
int main(void)
{
int n = 0;
printf("输入一个正整数:");
scanf("%d", &n);
printf("%d的阶乘为:%.0lf\n", n, fact(n));
return 0;
}
#include
double fact(int n)
{
if (n == 0 || n == 1)
return 1;
else if (n > 1)
return n * fact(n - 1);
}
int main(void)
{
int n = 0;
printf("输入一个正整数:");
scanf("%d", &n);
printf("%d的阶乘为:%.0lf\n", n, fact(n));
return 0;
}
或者:
#include
#include
using namespace std;
double fact(int n)
{
return (n == 0 || n == 1) ? 1 : (n * fact(n - 1));
}
int main(void)
{
int n = 0;
cin >> n;
cout << fixed << setprecision(0) << fact(n) << endl; //不以科学记数法输出,保留0位小数
return 0;
}
解题思路:
(1)把A柱子上的前N-1个盘子借助C柱子,全部移动到B柱子上(过程暂不考虑),再把第N个盘子由A柱子移动到C柱子上,那么剩下要移动的盘子在B柱子上了。
(2)把B柱子上的前N-2个盘子借助C柱子,全部移动到A柱子上(过程暂不考虑),再把第N-1个盘子由B柱子移动到C柱子上。
(3)重复上面的两个步骤即可把A柱子上的盘子全部移动到C柱子上。
#include
void loveyou(int n, char start, char help, char end)
{
if (n >= 2)
{
loveyou(n - 1, start, end, help);
printf("%c------>%c\n", start, end);
loveyou(n - 1, help, start, end);
}
else if (n == 1)
{
printf("%c------>%c\n", start, end);
}
}
int main(void)
{
int n = 0;
printf("输入圆盘的数量:");
scanf("%d", &n);
loveyou(n, 'A', 'B', 'C');
return 0;
}
#include
#include
int a = 0, b = 0, c = 0, d = 0;
void tjzf(char E[])
{
int i = 0;
for (i = 0; i <= strlen(E) - 1; i++)
{
if (E[i] >= 'A' && E[i] <= 'Z' || E[i] >= 'a' && E[i] <= 'z')
a++;
else if (E[i] >= '0' && E[i] <= '9')
b++;
else if (E[i] == ' ')
c++;
else
d++;
}
}
int main(void)
{
char F[1000];
printf("输入一段英文:");
gets(F);
tjzf(F);
printf("英文字符个数是%d\n", a);
printf("数字字符个数是%d\n", b);
printf("空格字符个数是%d\n", c);
printf("其他字符个数是%d\n", d);
return 0;
}
#include
#include
double getmax(double a[], int n)
{
double max = a[0];
int i = 0;
for (i = 1; i < n; i++)
{
if (max < a[i])
max = a[i];
}
return max;
}
double getmin(double a[], int n)
{
double min = a[0];
int i = 0;
for (i = 1; i < n; i++)
{
if (min > a[i])
min = a[i];
}
return min;
}
double getave(double a[], int n)
{
double sum = 0, ave = 0;
int i = 0;
for (i = 0; i < n; i++)
sum += a[i];
ave = sum / n;
return ave;
}
int main(void)
{
int n = 0, i = 0;
printf("输入班级人数:");
scanf("%d", &n);
double* a = (double*)malloc(8 * n);
printf("输入所有同学的成绩:\n");
for (i = 0; i < n; i++)
scanf("%lf", &a[i]);
printf("最高分为%.2lf,最低分为%.2lf,平均分为%.2lf\n", getmax(a, n), getmin(a, n), getave(a, n));
return 0;
}
#include
int gys(int x, int y)
{
int i = 0;
for (i = x; i >= 1; i--)
{
if (x % i == 0 && y % i == 0)
return i;
}
}
int gbs(int x, int y)
{
int i = 0;
for (i = x; i <= x * y; i++)
{
if (i % x == 0 && i % y == 0)
return i;;
}
}
int main(void)
{
int a = 0, b = 0;
printf("输入两个正整数:");
scanf("%d%d", &a, &b);
printf("它们的最大公约数为%d,最小公倍数为%d\n", gys(a, b), gbs(a, b));
return 0;
}
#include
#include
int isPrimeNumber(int s)
{
int i = 0;
if (s <= 1)
return 0;
for (i = 2; i <= sqrt(s); i++)
{
if (s % i == 0)
return 0;
}
return 1;
}
int main(void)
{
int i = 0, k = 0;
printf("输入一个不小于4的偶数:");
scanf("%d", &k);
while (k % 2 != 0 || k < 4)
{
printf("输入错误,请重新输入:");
scanf("%d", &k);
}
for (i = 2; i <= k - 2; i++)
{
if (isPrimeNumber(i) == 1 && isPrimeNumber(k - i) == 1)
{
printf("%d+%d=%d\n", i, k - i, k);
break;
}
}
return 0;
}
#include
int sf = 0;
void f(int n, int step)
{
if (n > 39)
return;
if (n == 39 && step % 2 == 0)
sf++;
f(n + 1, step + 1);
f(n + 2, step + 1);
}
int main(void)
{
f(0, 0);
printf("共有%d种上法\n", sf);
return 0;
}
#include
int pd(int s, int sum) //sum表示次数
{
if (s == 1)
return sum;
else if (s != 1 && s % 2 == 0)
{
sum++;
pd(s / 2, sum);
}
else if (s != 1 && s % 2 == 1)
{
sum++;
pd(3 * s + 1, sum);
}
}
int main(void)
{
int a;
printf("输入一个自然数:");
scanf("%d", &a);
printf("至少需经过%d次运算可以得到自然数1\n", pd(a, 0));
return 0;
}
#include
#include
#include
int main(void)
{
int a = 0, b = 0, c = 0, d = 0, i = 0;
char* E = (char*)malloc(5000);
printf("输入一段英文:");
gets(E);
for (i = 0; i <= strlen(E) - 1; i++)
{
if (*(E + i) >= 'A' && *(E + i) <= 'Z' || *(E + i) >= 'a' && *(E + i) <= 'z')
a++;
else if (*(E + i) >= '0' && *(E + i) <= '9')
b++;
else if (*(E + i) == ' ')
c++;
else
d++;
}
printf("英文字符个数是%d\n", a);
printf("数字字符个数是%d\n", b);
printf("空格字符个数是%d\n", c);
printf("其他字符个数是%d\n", d);
return 0;
}
#include
#include
int main(void)
{
double t = 0;
int i = 0, j = 0, n = 0;
printf("输入数据个数:");
scanf("%d", &n);
double* p = (double*)malloc(8 * n);
printf("输入这%d个数据:", n);
for (i = 0; i < n; i++)
scanf("%lf", p + i);
for (j = n - 2; j >= 0; j--)
{
for (i = 0; i <= j; i++)
{
if (*(p + i) < *(p + i + 1))
{
t = *(p + i);
*(p + i) = *(p + i + 1);
*(p + i + 1) = t;
}
}
}
printf("从高到低排序后输出:\n");
for (i = 0; i < n; i++)
printf("%lf\t", *(p + i));
free(p);
return 0;
}
#include
#include
void input(double* p, int n) //输入函数
{
int i = 0;
printf("输入%d个数据:", n);
for (i = 0; i <= n - 1; i++)
scanf("%lf", p + i);
}
void output(double* p, int n) //输出函数
{
int i = 0;
printf("按从高到低排序并输出:\n");
for (i = 0; i < n; i++)
printf("%lf\n", *(p + i));
}
void sort(double* p, int n) //排序函数
{
int i = 0, j = 0;
double t = 0;
for (j = n - 2; j >= 0; j--)
{
for (i = 0; i <= j; i++)
{
if (*(p + i) < *(p + i + 1))
{
t = *(p + i);
*(p + i) = *(p + i + 1);
*(p + i + 1) = t;
}
}
}
}
int main(void) //主函数
{
int n = 0;
printf("输入数据的个数:");
scanf("%d", &n);
double* p = (double*)malloc(sizeof(double) * n);
input(p, n);
sort(p, n);
output(p, n);
free(p);
return 0;
}
#include
int main(void)
{
int m = 0, i = 0, j = 0, t = 0;
int a[10] = {
0 };
printf("输入10个整数:");
for (i = 0; i < 10; i++)
scanf("%d", a + i);
printf("输入需要移动的数据个数:");
scanf("%d", &m);
for (j = 1; j <= m; j++)
{
t = *(a + 9);
for (i = 8; i >= 0; i--)
{
*(a + i + 1) = *(a + i);
}
*(a + 0) = t;
}
for (i = 0; i < 10; i++)
{
printf("%d ", *(a + i));
}
return 0;
}
#include
void DecToBin(int x) //正负数都可以转换,这里转换成对应的二进制补码
{
int a[32] = {
0 }, i = 0, y = 0;
if (x < 0)
a[31] = 1;
y = x > 0 ? x : -x; //将x的绝对值赋值给y
while (y > 0)
{
a[i] = y % 2;
i++;
y = y / 2;
}
if (x < 0) //对于负数,这里计算的是负数的二进制补码
{
for (i = 30; i >= 0; i--)
{
a[i] = !a[i]; //除开符号位以外其余各位按位求反 1->0 0->1
}
a[0]++; //末位+1
for (i = 1; i <= 30; i++) //处理进位
{
a[i] += a[i - 1] / 2;
a[i - 1] = a[i - 1] % 2;
}
a[30] = a[30] % 2;
}
for (i = 31; i >= 0; i--)
{
printf("%d", a[i]);
}
}
int main(void)
{
int k = 0;
printf("Input decimal number:");
scanf("%d", &k); //带符号的32位二进制数转换为十进制的范围为[-2147483647,2147483647]
printf("Output binary number:");
DecToBin(k);
return 0;
}
#include
#include
void DecToBin(int x) //正负数都可以转换,这里转换成对应的二进制原码
{
int i = 0, y = 0;
int* a = (int*)malloc(4 * 32);
for (i = 0; i <= 31; i++)
{
*(a + i) = 0;
}
if (x < 0)
*(a + 31) = 1; //为负数定义符号位
y = x > 0 ? x : -x; //y取x的绝对值
i = 0; //给i重新赋值为0
while (y > 0)
{
*(a + i) = y % 2;
i++;
y = y / 2;
}
for (i = 31; i >= 0; i--)
{
printf("%d", *(a + i));
}
free(a);
}
int main(void)
{
int k = 0;
printf("Input decimal number:");
scanf("%d", &k);
printf("Output binary number:");
DecToBin(k);
return 0;
}
#include
#include
void delete_zf(char* zf, int n, int m)
{
int i = 0, c;
c = strlen(zf);
for (i = 0; i <= c - n - m; i++)
{
*(zf + n + i) = *(zf + n + i + m);
}
}
int main(void)
{
char zfc[1000];
int m = 0, n = 0;
printf("输入一段字符串:");
gets(zfc);
printf("从第多少个字符开始删除:");
scanf("%d", &n);
printf("删除多少个字符:");
scanf("%d", &m);
delete_zf(zfc, n - 1, m);
printf("输出结果:");
puts(zfc);
return 0;
}
#include
#include
int main(void)
{
int n, i = 0, k = 3, t = 0, num = 0; //总人数n,跳出数字k
printf("输入总人数:");
scanf("%d", &n);
int* ar = (int*)malloc(4 * n);
for (i = 0; i <= n - 1; i++) //给n个人编号
{
ar[i] = i + 1;
}
num = n; //num用来临时存放总人数
for (i = 1;; i++) //终止条件置空实现无限循环
{
if (i > n) //回溯,转完一圈了
{
i = 1;
}
if (ar[i - 1] != 0) //不为跳出数字k的同学跳过
{
t++;
}
if (t == k)
{
ar[i - 1] = 0; //出列的同学赋值为0
num--; //同时当前总人数减1
t = 0; //又重新开始计数
}
if (num == 1) //只剩一人时跳出循环
{
break;
}
}
for (i = 0; i < n; i++) //打印目标值,最后编号不为0的同学就是剩下的那个
{
if (ar[i] != 0)
{
printf("这个同学原本是第%d个同学\n", ar[i]);
break;
}
}
return 0;
}
#include
#include
int main(void)
{
int n, k = 3, i = 0, t = 0, num = 0; //总人数n,跳出数字k
printf("输入总人数:");
scanf("%d", &n);
int* ar = (int*)malloc(4 * n);
for (i = 0; i < n; i++) //给n个人编号
{
*(ar + i) = i + 1;
}
num = n; //num用来临时存放总人数
for (i = 1;; i++) //终止条件置空实现无限循环
{
if (i > n) //回溯,转完一圈了
i = 1;
if (*(ar + i - 1) != 0) //不为跳出数字k的同学跳过
t++;
if (t == k)
{
*(ar + i - 1) = 0; //出列的同学赋值为0
num--; //同时当前总人数减1
t = 0; //又重新开始计数
}
if (num == 1) //只剩一人时跳出循环
{
break;
}
}
for (i = 0; i < n; i++) //打印目标值,最后编号不为0的同学就是剩下的那个
{
if (*(ar + i) != 0)
{
printf("这个同学原本是第%d个同学\n", ar[i]);
break;
}
}
return 0;
}
#include
#include
int main(void)
{
int n, k, i = 0, t = 0, num = 0; //总人数n,跳出数字k
printf("输入总人数和跳出数字:");
scanf("%d%d", &n, &k);
int* ar = (int*)malloc(4 * n);
for (i = 0; i <= n - 1; i++) //给n个人编号
{
ar[i] = i + 1;
}
num = n; //num用来临时存放总人数
for (i = 1;; i++) //终止条件置空实现无限循环
{
if (i > n) //回溯,转完一圈了
{
i = 1;
}
if (ar[i - 1] != 0) //不为跳出数字k的同学跳过
{
t++;
}
if (t == k) //出列的同学赋值为0
{
ar[i - 1] = 0;
num--; //同时总人数减1
t = 0; //又重新开始计数
}
if (num == 1) //只剩一人时跳出循环
{
break;
}
}
for (i = 0; i < n; i++) //打印目标值,最后编号不为0的同学就是剩下的那个
{
if (ar[i] != 0)
{
printf("这个同学原本是第%d个同学\n", ar[i]);
break;
}
}
return 0;
}
#include
int main(void)
{
struct Student
{
int number;
char name[11];
char gender[5];
double cscore;
};
struct Student stu;
printf("输入学号:");
scanf("%d", &stu.number);
getchar();
printf("输入姓名:");
gets(stu.name);
printf("输入性别:");
gets(stu.gender);
printf("输入C语言成绩:");
scanf("%lf", &stu.cscore);
printf("%-11s", "学号");
printf("%-11s", "姓名");
printf("%-11s", "性别");
printf("%-11s", "C语言成绩");
printf("\n");
printf("%-11d", stu.number);
printf("%-11s", stu.name);
printf("%-11s", stu.gender);
printf("%-11.2lf", stu.cscore);
printf("\n");
return 0;
}
#include
int date_num(int y, int m, int d)
{
int s = 0, i = 1;
while (i <= m - 1)
{
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10)
s = s + 31;
else if (i == 4 || i == 6 || i == 9 || i == 11)
s = s + 30;
else if (i == 2)
s = s + 28;
i++;
}
s = s + d;
if ((y % 4 == 0 && y % 100 != 0 || y % 400 == 0) && m >= 3)
s++;
return s;
}
int main(void)
{
int num = 0;
struct Date
{
int year;
int month;
int day;
};
struct Date date1;
printf("输入年份:");
scanf("%d", &date1.year);
printf("输入月份:");
scanf("%d", &date1.month);
printf("输入日期:");
scanf("%d", &date1.day); //日期没做纠错功能,输入时请输入合法日期
num = date_num(date1.year, date1.month, date1.day);
printf("这一天是该年的第%d天\n", num);
return 0;
}
#include
#include
#include
#define H 200
struct Student
{
int number;
char name[11];
char gender[5];
double cscore;
};
struct Student stu[H];
void add_information()
{
system("cls");
int i = 0, sel = 0;
struct Student tem_stu;
printf("\n");
printf("\t\t\t|———————————————————————————|\n");
printf("\t\t\t| 欢迎来到信息录入功能 |\n");
printf("\t\t\t|———————————————————————————|\n");
printf("\n");
for (i = 0; i < H; i++)
{
if (stu[i].number == 0)
{
printf("\t\t\t接下来请继续选择您的操作:\n");
printf("\t\t\t|------------------------|\n");
printf("\t\t\t| 1 录入学生信息 |\n");
printf("\t\t\t|------------------------|\n");
printf("\t\t\t| 2 返回主菜单 |\n");
printf("\t\t\t|------------------------|\n");
printf("\t\t\t请输入您的选择【1-2】:");
scanf("%d", &sel);
while (sel < 1 || sel>2)
{
printf("\t\t\t输入错误,请重新输入您的选择【1-2】:");
scanf("%d", &sel);
}
if (sel == 1)
{
int flag = 0;
printf("\t\t\t输入学号:");
scanf("%d", &tem_stu.number);
getchar();
printf("\t\t\t输入姓名:");
gets(tem_stu.name);
printf("\t\t\t输入性别:");
gets(tem_stu.gender);
printf("\t\t\t输入C语言成绩:");
scanf("%lf", &tem_stu.cscore);
for (i = 0; i < H; i++)
{
if (tem_stu.number == stu[i].number)
{
printf("\t\t\t该学号已存在!信息添加失败!!!\n");
flag = 1;
break;
}
}
if (flag == 0)
{
for (i = 0; i < H; i++)
{
if (stu[i].number == 0)
{
stu[i] = tem_stu;
printf("\t\t\t信息添加成功!!!\n\n");
break;
}
}
}
}
else if (sel == 2)
{
return;
}
}
}
}
void show_information()
{
system("cls");
int i = 0;
printf("\n");
printf("\t\t\t|———————————————————————————|\n");
printf("\t\t\t| 欢迎来到信息显示功能 |\n");
printf("\t\t\t|———————————————————————————|\n");
printf("\n");
printf("\t\t\t全部名单:\n");
printf("\t\t\t|-------------------------------------------|\n");
printf("\t\t\t| %-12s", "学号");
printf("%-10s", "姓名");
printf("%-10s", "性别");
printf("%-10s|", "C语言成绩");
printf("\n");
printf("\t\t\t|-------------------------------------------|\n");
for (i = 0; i < H; i++)
{
if (stu[i].number != 0)
{
printf("\t\t\t| %-12d", stu[i].number);
printf("%-10s", stu[i].name);
printf("%-10s", stu[i].gender);
printf("%-10.2lf|", stu[i].cscore);
printf("\n");
printf("\t\t\t|-------------------------------------------|\n");
}
else
break;
}
printf("\n\t\t\t不及格名单:\n");
printf("\t\t\t|-------------------------------------------|\n");
printf("\t\t\t| %-12s", "学号");
printf("%-10s", "姓名");
printf("%-10s", "性别");
printf("%-10s|", "C语言成绩");
printf("\n");
printf("\t\t\t|-------------------------------------------|\n");
for (i = 0; i < H; i++)
{
if (stu[i].number != 0)
{
if (stu[i].cscore < 60)
{
printf("\t\t\t| %-12d", stu[i].number);
printf("%-10s", stu[i].name);
printf("%-10s", stu[i].gender);
printf("%-10.2lf|", stu[i].cscore);
printf("\n");
printf("\t\t\t|-------------------------------------------|\n");
}
}
else
break;
}
printf("\t\t\t");
system("pause");
}
void menu()
{
int sel = 0;
system("cls");
printf("\t\t\t****************** 欢迎您!尊贵的Sniper ****************\n\n");
printf("\t\t\t请选择您的操作:\n");
printf("\t\t\t|———————————————————————————|\n");
printf("\t\t\t| 1 信息录入 |\n");
printf("\t\t\t|------------------------------------------------------|\n");
printf("\t\t\t| 2 信息显示 |\n");
printf("\t\t\t|------------------------------------------------------|\n");
printf("\t\t\t| 0 退出 |\n");
printf("\t\t\t|———————————————————————————|\n");
printf("\t\t\t请输入您的选择【0-2】:");
scanf("%d", &sel);
getchar();
while (sel < 0 || sel>2)
{
printf("\t\t\t输入错误,请重新输入你的选择【0-2】:");
scanf("%d", &sel);
}
switch (sel)
{
case 1:
add_information();
menu();
break;
case 2:
show_information();
menu();
break;
case 0:
exit(0);
}
}
int main(void)
{
system("color 70");
menu();
return 0;
}
#include
#include
#include
struct student
{
int xh;
char xm[11];
char xb[3];
double cscore;
double gscore;
double yscore;
};
struct student stu[5], jh;
int main(void)
{
int i = 0, j = 0;
for (i = 0; i <= 4; i++)
{
if (stu[i].xh == 0)
{
printf("输入第%d名学生的信息\n", i + 1);
printf("学号:");
scanf("%d", &stu[i].xh);
getchar();
printf("姓名:");
gets(stu[i].xm);
printf("性别:");
gets(stu[i].xb);
printf("C语言成绩:");
scanf("%lf", &stu[i].cscore);
printf("高数成绩:");
scanf("%lf", &stu[i].gscore);
printf("英语成绩:");
scanf("%lf", &stu[i].yscore);
printf("\n");
}
}
printf("所有学生信息:\n");
printf("%-11s", "学号");
printf("%-11s", "姓名");
printf("%-11s", "性别");
printf("%-11s", "C语言成绩");
printf("%-11s", "高数成绩");
printf("%-11s", "英语成绩");
printf("\n");
for (i = 0; i <= 4; i++)
{
printf("%-11d", stu[i].xh);
printf("%-11s", stu[i].xm);
printf("%-11s", stu[i].xb);
printf("%-11.2lf", stu[i].cscore);
printf("%-11.2lf", stu[i].gscore);
printf("%-11.2lf", stu[i].yscore);
printf("\n");
}
printf("\n");
double sum_c = 0, sum_g = 0, sum_y = 0, ave_c, ave_g, ave_y; /*六个量分别表示C程.高数.英语的全班总分及平均分*/
double zf[5] = {
0 }, t = 0; /*数组zf存储每个人的总分*/
for (i = 0; i <= 4; i++)
{
zf[i] = stu[i].cscore + stu[i].gscore + stu[i].yscore;
sum_c += stu[i].cscore;
sum_g += stu[i].gscore;
sum_y += stu[i].yscore;
}
ave_c = sum_c / 5;
ave_g = sum_g / 5;
ave_y = sum_y / 5;
printf("C语言平均成绩为:%.2lf\n", ave_c);
printf("高数平均成绩为:%.2lf\n", ave_g);
printf("英语平均成绩为:%.2lf\n", ave_y);
for (j = 0; j <= 3; j++)
{
for (i = 0; i <= 3 - j; i++)
{
if (zf[i] > zf[i + 1])
{
t = zf[i];
zf[i] = zf[i + 1];
zf[i + 1] = t;
}
}
}
printf("\n");
printf("总成绩分最高的学生信息:\n");
printf("%-11s", "学号");
printf("%-11s", "姓名");
printf("%-11s", "性别");
printf("%-11s", "C语言成绩");
printf("%-11s", "高数成绩");
printf("%-11s", "英语成绩");
printf("%-11s", "总成绩");
printf("\n");
for (i = 0; i <= 4; i++)
{
if (stu[i].cscore + stu[i].gscore + stu[i].yscore == zf[4])
{
printf("%-11d", stu[i].xh);
printf("%-11s", stu[i].xm);
printf("%-11s", stu[i].xb);
printf("%-11.2lf", stu[i].cscore);
printf("%-11.2lf", stu[i].gscore);
printf("%-11.2lf", stu[i].yscore);
printf("%-11.2lf", zf[4]);
printf("\n");
}
}
for (j = 0; j <= 3; j++)
{
for (i = 0; i <= 3 - j; i++)
{
if (stu[i].cscore < stu[i + 1].cscore)
{
jh = stu[i];
stu[i] = stu[i + 1];
stu[i + 1] = jh;
}
}
}
printf("\n");
printf("按C语言成绩进行由高到低排序:\n");
printf("%-11s", "学号");
printf("%-11s", "姓名");
printf("%-11s", "性别");
printf("%-11s", "C语言成绩");
printf("%-11s", "高数成绩");
printf("%-11s", "英语成绩");
printf("\n");
for (i = 0; i <= 4; i++)
{
printf("%-11d", stu[i].xh);
printf("%-11s", stu[i].xm);
printf("%-11s", stu[i].xb);
printf("%-11.2lf", stu[i].cscore);
printf("%-11.2lf", stu[i].gscore);
printf("%-11.2lf", stu[i].yscore);
printf("\n");
}
return 0;
}
#include
#include
int main(void)
{
FILE* fp;
FILE* fq;
int n = 0;
char ch;
fp = fopen("data.txt", "r"); //需要提前创建源程序当前目录下的data.txt文件,否则会文件打开失败
fq = fopen("res.txt", "w");
if (fp == NULL)
{
printf("文件data.txt打开失败,请在源程序目录下手动创建该文件!\n");
exit(0);
}
while (!feof(fp)) //feof()函数的功能是检测流上的文件结束符,如果文件结束,则返回非0值;如果文件未结束,则返回0值
{
ch = fgetc(fp);
if (ch == '$')
n++;
}
fprintf(fq, "%d", n); //将n的结果写入到res.txt文档中
printf("%d\n", n); //将n的结果显示到屏幕中
fclose(fp);
fclose(fq);
return 0;
}
#include
#include
int main(void)
{
FILE* fp;
int n = 0;
char ch;
fp = fopen("xhar2.txt", "r"); //需要提前创建源程序当前目录下的data.txt文件,否则会文件打开失败
if (fp == NULL)
{
printf("文件xhar2.txt打开失败,请在源程序目录下手动创建该文件!\n");
exit(0);
}
while (!feof(fp))
{
n++;
ch = fgetc(fp);
printf("%c", ch);
if (n == 10)
break;
}
fclose(fp);
return 0;
}
#include
#include
#include
int main(void)
{
FILE* fa;
FILE* fb;
FILE* fc;
int n = 0;
char ch, ck;
fa = fopen("a.txt", "a");
fb = fopen("b.txt", "r"); //"b.txt"文档需要提前手动创建
if (fb == NULL)
{
printf("文件b.txt打开失败,请在源程序目录下手动创建该文件!\n");
exit(0);
}
while ((ch = fgetc(fb)) != EOF)
{
fputc(ch, fa);
}
fclose(fa); //关闭以"a"文件操作方式打开的a.txt
fa = fopen("a.txt", "r"); //再以"r"文件操作方式重新打开a.txt
fc = fopen("c.txt", "w");
while ((ck = fgetc(fa)) != EOF)
{
fputc(ck, fc);
}
fclose(fc);
fclose(fa);
fclose(fb);
printf("运行成功,打开文件查看吧!\n");
return 0;
}
#include
#include
#include
#define N 200
struct Student
{
char name[15];
char address[30];
char phone_number[15];
};
struct Student stu[N];
int main(void)
{
int i = 0, sel = 0, M = 0;
for (i = 0; i < N; i++)
{
printf("输入第%d个学生的信息:\n", i + 1);
printf("姓名:");
gets(stu[i].name);
printf("地址:");
gets(stu[i].address);
printf("联系方式:");
gets(stu[i].phone_number);
printf("---------------------\n");
printf("是否继续输入?\n");
printf("1 是 2 否\n");
printf("输入你的选择:");
scanf("%d", &sel);
getchar();
while (sel < 1 || sel>2)
{
printf("输入错误,请重新输入:");
scanf("%d", &sel);
getchar();
}
printf("---------------------\n");
if (sel == 1)
continue;
else if (sel == 2)
{
M = i + 1;
break;
}
}
FILE* fp;
fp = fopen("class.txt", "w");
if (fp == NULL)
{
printf("文件打开失败!");
exit(0);
}
fprintf(fp, "%-9s%-28s%-15s\n", "姓名", "地址", "联系方式");
for (i = 0; i < M; i++)
{
fprintf(fp, "%-9s%-28s%-15s\n", stu[i].name, stu[i].address, stu[i].phone_number);
}
//如果需要从文件中读取信息可以先利用rewind(fp)重置文件指针,再通过fscanf()函数读取,用法与fprintf()类似
fclose(fp);
printf("信息保存成功!请打开文件class.txt查看!\n");
return 0;
}
#include
#include
int main(void)
{
FILE* fp, * fq;
int sum = 0, i = 0, a = 0, b = 0, t = 0;
fp = fopen("data.txt", "r");
fq = fopen("result.txt", "w");
if (fp == NULL || fq == NULL)
{
printf("文件data.txt打开失败,请在源程序目录下手动创建该文件!\n");
exit(0);
}
fscanf(fp, "%d", &a);
fscanf(fp, "%d", &b);
if (a > b)//a与b的值交换,必须保证计算中a小b大
{
t = a;
a = b;
b = t;
}
for (i = a; i <= b; i++)
{
sum += i;
}
fprintf(fq, "%d\n", sum);//输出到文件中
fclose(fp);
fclose(fq);
printf("信息保存成功!请打开文件he.txt查看!\n");
return 0;
}
#include
#include
int main(void)
{
char s[100], c[100], t[100];
FILE* fp;
if ((fp = fopen("string.txt", "w")) == NULL)
{
printf("文件string.txt打开失败,请在源程序目录下手动创建该文件!\n");
exit(0);
}
fscanf(stdin, "%s%s%s", s, c, t); //空格和回车一样都被作为一个字符串结束的标志
fprintf(fp, "%s %s %s", s, c, t);
fclose(fp);
printf("信息保存成功!请打开文件class.txt查看!\n");
return 0;
}
#include
#include
#include
int main(void)
{
FILE* fp, * fq;
char E[5000];
int sum = 0, i = 0;
fp = fopen("English.txt", "r");
fq = fopen("result.txt", "w");
if (fp == NULL || fq == NULL)
{
printf("文件English.txt打开失败,请在源程序目录下手动创建该文件!\n");
exit(0);
}
fgets(E, 5000, fp);
for (i = 0; i <= strlen(E) - 1; i++)
{
if (E[i] >= 65 && E[i] <= 90 || E[i] >= 97 && E[i] <= 122)
if (!(E[i + 1] >= 65 && E[i + 1] <= 90 || E[i + 1] >= 97 && E[i + 1] <= 122))
if (E[i + 1] != '-')
sum++;
}
fprintf(fq, "单词个数是%d\n", sum);
fclose(fp);
fclose(fq);
printf("信息保存成功!请打开文件result.txt查看!\n");
return 0;
}
g c d ( a , b ) = g c d ( b , a m o d b ) {gcd(a,b)}={gcd(b,a \bmod b)} gcd(a,b)=gcd(b,amodb)
#include
int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main(void)
{
int x = 0, y = 0;
printf("Input two positive integers:");
scanf("%d%d", &x, &y);
printf("Output their greatest common divisor:%d", gcd(x, y));
return 0;
}
第一行输入一个非负整数A。
第二行输入一个非负整数B。
A、B的长度不大于1000。
输出A+B的值。不要有多余的前导零。
1234567891011121314151617181920123456789
5974994545148254725545124662176431426461421426416
num1: 1234567891011121314151617181920123456789
num2: 5974994545148254725545124662176431426461421426416
num1Len:40
num2Len:49
sum: 5974994546382822616556245976328048608381544883205
#include
#include
#include
#define N 1000
int main(void)
{
int A = 0, B = 0, max = 0, i = 0;
char a[N];
char b[N];
//以字符串形式输入加数和被加数
printf("Please input two non-negative integers(less than 1000 digits):\n");
scanf("%s", a);
scanf("%s", b);
A = strlen(a);
B = strlen(b);
printf("num1:\t%s\n", a);
printf("num2:\t%s\n", b);
printf("num1Len:%d\n", A); //输出非负整数num1的长度
printf("num2Len:%d\n", B); //输出非负整数num2的长度
max = A > B ? A : B; //取num1和num2较大一个数的位数为max
char* c = (char*)malloc(sizeof(char) * max);
char* d = (char*)malloc(sizeof(char) * max);
int* s = (int*)malloc(sizeof(int) * (max + 1)); //max+1作为存放结果数组的长度
for (i = max; i >= 0; i--)
s[i] = 0; //初始化整型数组s,用来存放结果
//准备数据存放位置
for (i = 0; i <= max - A - 1; i++) //字符数组c以字符形式存放整数num1
c[i] = 48; //并且以字符0占据空位
for (i = max - A; i <= max - 1; i++) //数组从左至右,对应权值从高到低
c[i] = a[i - max + A]; //逐位赋值
for (i = 0; i <= max - B - 1; i++) //字符数组d以字符形式存放整数num1
d[i] = 48; //并且以字符0占据空位
for (i = max - B; i <= max - 1; i++) //数组从左至右,对应权值从高到低
d[i] = b[i - max + B]; //逐位赋值
//按照加法竖式算法对数据进行运算
for (i = max - 1; i >= 0; i--)
{
s[max - 1 - i] = (c[i] - 48) + (d[i] - 48); //逐位相加并赋值给数组s的各个元素,暂时不处理进位
} //数组s从左至右对应的权值为从低到高
for (i = max - 1; i >= 0; i--)
{
if (s[max - 1 - i] >= 10)
{
s[max - 1 - i] = s[max - 1 - i] % 10; //处理进位
s[max - i]++;
}
}
//对结果进行输出
printf(" sum:\t");
if (s[max] != 0) //如果数组s最后一个元素有来自低位的进位 ,则输出
{
for (i = max; i >= 0; i--)
{
printf("%d", s[i]);
}
}
else if (s[max] == 0) //如果数组s最后一个元素没有来自低位的进位 ,则不输出
{
for (i = max - 1; i >= 0; i--)
{
printf("%d", s[i]);
}
}
return 0;
}
第一行输入一个非负整数A。
第二行输入一个非负整数B。
A、B的长度不大于1000。
输出A×B的值。不要有多余的前导零。
9876543210123456789103
15656489879654321156632157
num1: 9876543210123456789103
num2: 15656489879654321156632157
num1Len:22
num2Len:26
product: 154631998815266502735860414063580937236496985171
#include
#include
#include
#define N 1000
int main(void)
{
int A = 0, B = 0, i = 0, j = 0;
char a[N];
char b[N];
//以字符串形式输入乘数和被乘数
printf("Please input two non-negative integers(less than 1000 digits):\n");
scanf("%s", a);
scanf("%s", b);
A = strlen(a);
B = strlen(b);
printf("num1:\t %s\n", a);
printf("num2:\t %s\n", b);
printf("num1Len:%d\n", A); //输出非负整数num1的长度
printf("num2Len:%d\n", B); //输出非负整数num2的长度
//为数据准备存放位置
int* e = (int*)malloc(4 * A);
for (i = 0; i < A; i++) //把以字符串形式存放的num1转为整型数组形式存放
e[i] = a[i] - 48;
int* f = (int*)malloc(4 * B); //把以字符串形式存放的num2转为整型数组形式存放
for (i = 0; i < B; i++)
f[i] = b[i] - 48;
int* t = (int*)malloc(4 * (A + B)); //申请整型数组t存放num1和num2相乘的结果,长度为A+B
for (i = 0; i <= A + B - 1; i++)
t[i] = 0;
int** s = (int**)malloc(sizeof(int*) * B); //二维数组s为乘法竖式运算过程中的数据提供存放位置
for (i = 0; i < B; i++) //s为B行A+B-1列的二维数组
s[i] = (int*)malloc(sizeof(int) * (A + B - 1));
for (i = 0; i <= B - 1; i++)
{
for (j = 0; j <= A + B - 2; j++)
{
s[i][j] = 0; //二维数组s所有元素初始化为0
}
}
//按照乘法竖式算法对数据进行运算
for (i = 0; i <= B - 1; i++)
{
for (j = A + B - 2 - i; j >= B - 1 - i; j--) //按照乘法竖式相乘算法对num1和num2进行处理
{
s[i][j] = e[i + j - B + 1] * f[B - 1 - i];
}
}
for (j = 0; j <= A + B - 2; j++)
{
for (i = 0; i <= B - 1; i++)
{
t[j + 1] = t[j + 1] + s[i][j]; //按照乘法竖式算法继续处理
}
}
for (j = A + B - 1; j >= 1; j--)
{
if (t[j] >= 10)
{
t[j - 1] = t[j - 1] + t[j] / 10; //处理进位问题
t[j] = t[j] % 10;
}
}
//对结果进行输出
printf("product: ");
for (i = 0; i <= A + B - 1; i++)
{
if (t[i] == 0 && i != A + B - 1)
continue;
else if (t[i] == 0 && i == A + B - 1)
printf("0");
else if (t[i] != 0)
{
for (; i <= A + B - 1; i++)
{
printf("%d", t[i]);
}
break;
}
}
return 0;
}
#include
//jiu表示酒的数量
//hua表示遇见花的次数
//dian表示遇见店的次数
//yujian每次遇见的是店或花 店记为0,遇花记为1
int sum = 0;//表示所有符合题意的组合数量,这里可以等价写为 int sum;
void f(int jiu, int hua, int dian, int yujian)
{
if (hua + dian == 15)//某种组合结束
{
if (jiu == 0 && hua == 10 && dian == 5 && yujian == 1)
sum++;
}
else
{
f(jiu - 1, hua + 1, dian + 0, 1); //某次遇见花
f(jiu * 2, hua + 0, dian + 1, 0); //某次遇见店
}
}
int main(void)
{
f(2, 0, 0, 1);
//最后一个实参表示遇见,因为没有出门,所以这个值不存在,那就随便写
printf("%d\n", sum);
return 0;
}
#include
int ti[11] = {
0 }; //用来存储每道题的回答情况0或者1,0表示错误,1表示正确
//提醒:数组ti的编号实际用1-10
//score表示当前得分,num是当前第num道题目
void f(int score, int num)
{
if (num == 11)//10道题目全部做完
{
if (score == 100)
{
int i = 0;
for (i = 1; i <= 10; i++)
printf("%d", ti[i]);
printf("\n");
}
}
else //10道题目没有做完
{
ti[num] = 1; //讨论第num道题目的正确情况
f(score * 2, num + 1);
ti[num] = 0; //讨论第num道题目的错误情况
f(score - num, num + 1);
}
}
int main(void)
{
f(10, 1);//从基础分10分开始,从第1题开始
return 0;
}
输入包含两个整数 N(N≤10000)和R(2≤R≤16)。
注意:N有可能是负整数。
输出一行,表示转换后的数。
742035030 16
2C3A8E56
#include
int main(void)
{
int t = 0, N = 0, R = 0, i = 0, j = 0;
char s[1000] = {
'\0' };
scanf("%d%d", &N, &R);
if (N == 0)
printf("0");
else if (N != 0)
{
if (N < 0)
printf("-");
N = N > 0 ? N : -N;
while (N != 0)
{
t = N % R;
if (t >= 0 && t <= 9)
s[i] = 48 + t;
else if (t >= 10 && t <= 15)
s[i] = 55 + t;
i++;
N = N / R;
}
for (j = i - 1; j >= 0; j--)
printf("%c", s[j]);
}
return 0;
}
输入:x = 121
输出:true
输入:x = -121
输出:false
解释:从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
输入:x = 10
输出:false
解释:从右向左读, 为 01 。因此它不是一个回文数。
#include
#include
bool isPalindrome(int x)
{
if (x < 0)
return false;
else if (x == 0)
return true;
else
{
int i = 0, j = 0, a[100] = {
0 };
while (x > 0)
{
a[i] = x % 10;
i++;
x = x / 10;
}
for (j = 0; j <= (i - 1) / 2; j++)
{
if (a[j] != a[i - 1 - j])
return false;
}
return true;
}
}
int main(void)
{
int x = 0;
printf("Input x: ");
scanf("%d", &x);
printf("%d", isPalindrome(x));
return 0;
}
输入一行一个正整数n。
输出第一行一个正整数,表示得到一个回文数的最少变换次数。
接下来一行,输出变换过程,相邻的数之间用"—>"连接。输出格式可以参见样例。
保证最后生成的数在int范围内。
349
3
349--->1292--->4213--->7337
#include
#include
int isPalindrome(int x) //如果传入的正整数是回文数则返回1,否则返回0
{
if (x < 0)
return 0;
else if (x == 0)
return 1;
else
{
int i = 0, j = 0, a[100] = {
0 };
while (x > 0)
{
a[i] = x % 10;
i++;
x = x / 10;
}
for (j = 0; j <= (i - 1) / 2; j++)
{
if (a[j] != a[i - 1 - j])
return 0;
}
return 1;
}
}
int fz(int x) //返回翻转后的正整数
{
int a[100] = {
0 }, i = 0, y = 0, j = 0;
while (x > 0)
{
a[i] = x % 10;
i++;
x = x / 10;
}
i--;
for (j = 0; j <= i; j++)
{
y += a[j] * pow(10, i - j);
}
return y;
}
int main(void)
{
int k = 0, i = 0, a[100] = {
0 }, j = 0;
scanf("%d", &k);
while (isPalindrome(k) == 0)
{
a[i] = k;
k += fz(k);
i++;
}
printf("%d\n", i);
for (j = 0; j < i; j++)
{
printf("%d--->", a[j]);
}
printf("%d", k);
return 0;
}
输入一个正整数n,表示布丁的数量。
每行输出一个由’0’和’1’组成的字符串,表示一种可行的吃布丁的顺序。其中0表示原味,1表示抹茶味。 请将可行的每种方案按字典序升序输入。
3
001
010
011
100
101
110
对于100%的数据,保证1≤n≤20;
#include
using namespace std;
void f(int j, int n, int* a)
{
if (j == n) //如果吃的布丁的个数达到了n个,这时就代表一种情况结束了
{
int flag = 0;
for (int i = 0; i <= n - 3; i++)
{
//遍历该情况,如果存在连续三个相同的布丁,则这种情况flag取值变为1,并且不输出这种情况
if (a[i] == a[i + 1] && a[i] == a[i + 2] && a[i + 1] == a[i + 2])
flag = 1;
}
if (flag == 0) //如果flag取值依然为0,就代表这种情况符合要求,可以输出
{
for (int i = 0; i < n; i++)
{
cout << a[i];
}
cout << endl;
}
}
else
{
a[j] = 0; //讨论第j个布丁是原味的情况
f(j + 1, n, a); //先讨论0的情况,再讨论1的情况是为了按照字典序输出
a[j] = 1; //讨论第j个布丁是抹茶味的情况
f(j + 1, n, a);
}
}
int main()
{
int n = 0;
cin >> n;
int* p = new int[n];
f(0, n, p); //初始情况:吃了0个布丁,还有n个,数组p用来表示吃的情况
delete[] p;
return 0;
}