ret = mktime(&info); if( ret == -1 ) { printf("Error: unable to make time using mktime\n"); } else { strftime(buffer, sizeof(buffer), "%c", &info ); printf(buffer); }
#include<stdio.h> intconsnum(int a, int n); intmain(){ int a,time,i,sum; sum = 0; printf("Please input a:"); scanf("%d",&a); printf("Please input n:"); scanf("%d",&time); printf("Sum="); for(i=0;i<time;i++){ printf("%d",consnum(a,i+1)); if(i+1!=time)printf("+"); elseprintf("\n"); sum += consnum(a,i+1); } printf("Sum=%d",sum); } intconsnum(int a, int n){ int i,sum; sum =0; for(i=1;i<=n;i++){ sum += a * pow(10,i-1); } return sum; }
#include<stdio.h> intCountDigit(int number, int digit); intmain() { int num, digit; printf("Input m,n:\n"); scanf("%d,%d", &num, &digit); num = CountDigit(num, digit); printf("%d\n", num); } intCountDigit(int number, int digit) { int temp; int ans = 0; do { temp = number % 10; if (temp == digit)ans += 1; number /= 10; } while (number > 0); return ans; }
#include<stdio.h> #include<math.h> intmain() { int num; int i = 0; printf("请输入一个整数:"); scanf("%d",&num); while(num != num % (int)pow(10,i)){ i++; } num = num /(pow(10,i-1)); printf("该整数以%d打头!\n",num); }
第五题
编程求解汉诺塔问题。
汉诺塔(Hanoi)是必须用递归方法才能解决的经典问题。它来自于印度神话。上帝创造世界时作了三根金刚石柱子,在第一根柱子上从下往上按大小顺序摞着64片黄金圆盘,如图7-3所示。上帝命令婆罗门把圆盘从下面开始按大小顺序重新摆放到第二根柱子上,并且规定,每次只能移动一个圆盘,在小圆盘上不能放大圆盘。有人预言说,这件事完成时宇宙会在一瞬间闪电式毁灭,也有人相信婆罗门至今仍在一刻不停地搬动着圆盘。
**输入格式要求:"%d" 提示信息:“Input the number of disks:”
**输出格式要求:“Steps of moving %d disks from A to B by means of C:\n” “Move %d: from %c to %c\n”
程序运行示例如下:
Input the number of disks:3
Steps of moving 3 disks from A to B by means of C:
Move 1: from A to B
Move 2: from A to C
Move 1: from B to C
Move 3: from A to B
Move 1: from C to A
Move 2: from C to B
Move 1: from A to B
#include<stdio.h> voidHanoi(int n, char a, char b, char c); voidMove(int n, char a, char b); intmain() { int n; printf("Input the number of disks:"); scanf("%d", &n); printf("Steps of moving %d disks from A to B by means of C:\n", n); Hanoi(n, 'A', 'B', 'C'); /*调用递归函数Hanoi()将n个圆盘借助于C由A移动到B*/ return0; } /* 函数功能:用递归方法将n个圆盘借助于柱子c从源柱子a移动到目标柱子b上 */ voidHanoi(int n, char a, char b, char c) { if (n == 1) { Move(n, a, b); /* 将第n个圆盘由a移到b */ } else { Hanoi(n - 1, a, c, b); /* 递归调用Hanoi(),将第n-1个圆盘借助于b由a移动到c*/ Move(n, a, b); /* 第n个圆盘由a移到b */ Hanoi(n - 1, c, b, a); /*递归调用Hanoi(),将第n-1个圆盘借助于a由c移动到b*/ } } /* 函数功能: 将第n个圆盘从源柱子a移到目标柱子b上 */ voidMove(int n, char a, char b) { printf("Move %d: from %c to %c\n", n, a, b); }
第六题
根据最大公约数的如下3条性质,采用递归法编写计算最大公约数的函数Gcd(),在主函数中调用该函数计算并输出从键盘任意输入的两正整数的最大公约数。
性质1 如果a>b,则a和b与a-b和b的最大公约数相同,即Gcd(a, b) = Gcd(a-b, b)
性质2 如果b>a,则a和b与a和b-a的最大公约数相同,即Gcd(a, b) = Gcd(a, b-a)
性质3 如果a=b,则a和b的最大公约数与a值和b值相同,即Gcd(a, b) = a = b
要求如下:
(1)从键盘任意输入的两整数
主函数调用Gcd()函数,并输出两整数的最大公约数。
(2)Gcd函数原型为:
int Gcd(int a, int b);
如果输入的数不是正整数,则返回-1,否则,返回两个数的最大公约数。
(3)**输入提示信息格式要求:“Input a,b:”
输入格式:"%d,%d"
**输出提示信息要求:
若输入不是正整数,则输出"Input number should be positive!\n"
否则输出"Greatest Common Divisor of %d and %d is %d\n"
注:不允许使用goto语句
#include<stdio.h> intGcd(int a,int b) { if(a == b)return a; elseif (a > b)return Gcd(a-b,b); elseif (b > a)return Gcd(a,b-a); } intmain() { int a,b; printf("Input a,b:"); scanf("%d,%d",&a,&b); if(a<1||b<1)printf("Input number should be positive!\n"); else { printf("Greatest Common Divisor of %d and %d is %d\n",a,b,Gcd(a,b)); } }