C语言easy
PAGE
PAGE 26
Problem 1 表格问题#includevoid output(const int* a) { printf("%d %d %d\n",a[0], a[1], a[2]); printf("%d %d %d\n",a[3], a[4], a[5]);}int ok(const int* a) { int used[7] = {0};//used[i]的值为0:i尚未被用;1:i已被使用 int i; for (i = 1; i < 5; i++) { if ( used[ a[i] ] ) return 0; used[ a[i] ] = 1; } //for循环结束意味着a[i]互不相同 return a[1] < a[2] && a[3] < a[4] && a[0] < a[3] && a[1] < a[4];}int main() { int n = 0, i, rank; int table[5][6];//存放按照字典序排序的5种表格 int a[6]; //存放1种表格a[0] = 1;a[5] = 6; for (a[1] = 2; a[1] <= 5; a[1]++) for (a[2] = 2; a[2] <= 5; a[2]++) for (a[3] = 2; a[3] <= 5; a[3]++) for (a[4] = 2; a[4] <= 5; a[4]++) if ( ok(a) ) { for (i = 0; i < 6; i++) table[n][i] = a[i]; n++; //行下标加1 } while (scanf("%d",&rank)==1) output( table[rank - 1] );return 0;}Problem 2 奇妙的数字#includeint digitSum(int n) { int sum = 0; while (n > 0) { sum += n % 10; n /= 10; } return sum;}int main() { int answer[10];//存放前10个满足条件的数 int n, i; for (n=0,i=1; n < 10; i++) { if ( 0 == digitSum(i) % 17 && 0 == digitSum(i + 1) % 17) { answer[n++] = i; } } while (scanf("%d",&i)==1) { printf("%d\n",answer[i - 1]); } return 0;}Problem 5 求绝对值//版本1#includeint main() { char line[1002]; while (scanf("%s",line)!=EOF) { if ( '-' == line[0] ) printf("%s\n",line+1); else printf("%s\n",line); } return 0;}//版本2#includeint main(){ char ch; while(scanf("%c", &ch)!=EOF) { if(ch!='-') printf("%c",ch); } return 0;}Problem 6 求最大公约数#includeint gcd(int m,int n)//辗转相除法{ int r=m%n; while(r) { m=n; n=r; r=m%n; } return n;}int main(){int M,N;while(scanf("%d%d",&M,&N)==2){