C语言 BIT
数字金字塔
#include <stdio.h> int main() { int i, j; int n; char maxLetr; char ch[26]; for(i=0; i<26; i++) { ch[i] = ‘A‘ + i; } int x; scanf("%d,%c", &x, &maxLetr); getchar(); // 吸收回车 for(i=0; i<26; i++) { if(ch[i] == ‘A‘+x-1) { n = i + 1; } } printf(" "); for(i=1; i<=n; i++) { for(j=0; j<n-i+1; j++) { printf(" "); } for(j=0; j<i; j++) { if(j<n-2){ printf("%c ",ch[j]); }else{ printf("%c ",ch[j]); } } for(j=0; j<i-1; j++) { if(j>n-2){ printf(" %c",ch[i-j-2]); }else{ printf("%c ",ch[i-j-2]); } } printf("\n"); } return 0; }
有一种特殊偿数,它每一位上的数字都两两不相同。我们现在需理共出四位数中某一区间内的这类困成。
#include <stdio.h> main() { int a, b, counter, i, number1, number2, number3, number4; do { scanf( "%d%d", &a, &b ); if ( (a == 0) && (b == 0) ) break; if ( a >= b ) printf( "Error\n" ); else{ if ( (a < 1000) || (b < 1000) ) printf( "Error\n" ); else{ for ( i = a, counter = 0; i <= b; i++ ) { number1 = i / 1000; number2 = i / 100 % 10; number3 = i / 10 % 10; number4 = i % 10; if ( (number1 != number2) && (number1 != number3) && (number1 != number4) && (number2 != number3) && (number2 != number4) && (number3 != number4) ) { if ( (i % 2) == 0 ) { printf( "%d ", i ); counter++; } } } printf( "\n" ); printf( "counter=%d\n", counter ); } } }while ( (a != 0) && (b != 0) ); return 0; }
输入n.A,输出如下例(n=3)所示的字母x
#include<stdio.h> #include<time.h> char arr[1000]; void output(int n, char ch){ int count = 0; int tot = n-1; int i, j; while(count != tot+2){ arr[++count] = ch++; } for(i = n; i >= 2; --i){ for(j = 0; j < n-i; ++j) printf(" "); printf("%c", arr[n-i+1]); for(j = 0; j < 2*(i-1)-1; ++j) printf(" "); printf("%c", arr[n-i+1]); printf("\n"); } for(j = 0; j < n-1; ++j) printf(" "); printf("%c", arr[n-i+1]); printf("\n"); for(i = 2; i <= n; ++i){ for(j = 0; j < n-i; ++j) printf(" "); printf("%c", arr[n-i+1]); for(j = 0; j < 2*(i-1)-1; ++j) printf(" "); printf("%c", arr[n-i+1]); printf("\n"); } } int main(){ int i; char ch; scanf("%d %c", &i, &ch); output(i, ch); return 0; }
输入一个字符串,将其中的所有字母反转,但是特殊符导位重不变,例如,
#include <stdio.h> #include <stdlib.h> #include <string.h> void Reverse(char str[]); int main() { char str[100]; printf("Input a string:");//Good bye gets(str); Reverse(str); } void Reverse(char str[]){ int n=strlen(str); char mm[100]; int nn[100]={0}; strcpy(mm,str); int i; char temp; for(i=0;i<n;i++){ if(str[i]>‘A‘&&str[i]<‘Z‘){ continue; }else if(str[i]>‘a‘&&str[i]<‘z‘){ continue; }else{ nn[i]=1; } } for(i=0;i<(n/2)+1;i++){ temp=str[i]; str[i]=str[n-i-1]; str[n-i-1]=temp; } for(i=0;i<n;i++){ if(nn[i]==1){ printf("%c",mm[i]); }else{ printf("%c",str[i]); } } }