程序3-4竖式问题
#include<stdio.h> #include<string.h> int main() { char a[30], b[113]; int count = 0; scanf("%s", a); for(int abc = 111;abc <= 999; abc++) for(int de = 11;de <= 99 ; de++) { int x = abc*(de%10); int y = abc*(de/10); int z = abc*de; sprintf(b, "%d%d%d%d%d", abc, de, x, y, z); int ok = 1; for(int i = 0;i < strlen(b);i++) if(strchr(a, b[i]) == NULL) ok = 0; if(ok) { printf("<%d>\n", ++count); printf("%5d\nX%4d\n-----\n%5d\n%4d\n-----\n%5d\n\n", abc, de, x, y, z); } } printf("the number of solutions = %d\n", count); return 0; }
1.sprintf:输入至字符串数组中(字符串数组是char型)并覆盖上一次输入
2.strlen:返回字符串数组中\0前的字符(输入输出的有效字符)
3.strchr(字符串, 字符); 在字符串里找所需的字符 找到的话输出包括字符本身以及后面的所有字符 如果找不到就返回NULL
4.getchar(); getchar返回的是int型 只要成功输入了就返回int型的字符(变成数字)或者数字 如果输入失败了 就返回EOF
经常用 while((i = getchar() != EOF) 循环来输入字符串(i是int型!) 好处是不用建立数组来储存 而是每一个字符一一进入循环 一一进行循环的操作 可以用puchar来输出
5.用所有带str的函数 都要加string.h的头文件

浙公网安备 33010602011771号