朋友专升本出了两个C语言的题目,希望大家帮助解决。

zeusguitar 2005-05-19 11:08:30
朋友专升本出了两个C语言的题目,不是很难,以前是程序员现在做市场了,什么都不会了,希望大家帮助。直接提供源码吧,最好是大家测试过的。
1、编写一个程序,算出从100到100000之间,各位数字之和为5的整数个数。
题目解析:所谓的各位数字之和为5的整数的意思就是像113这样的一个整数,所有位上的数 字加起来等于5 (1+1+3=5),现在就是要求同学们编写一个程序,算出从100到100000之间到底有多少个符合这样要求的整数。

2、现有一个数组 int a[10],编写一个程序,执行该程序后可从键盘读入数组,然后将数组中各元素从大到小排列,并显示结果。题目解析:比如,执行程序后,从键盘读入 5, 79, 32, 50, 80, 21, 90, 100, 41, 2这10个数字组成一个数组。当然,每次执行程序的时候可以输入不同的整数作为数组的元素。然后要求把这些元素按照从大到小排列然后输出,按上述的例子则应该输出 100,90,80,79,50,41,32,21,5,
...全文
271 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
mostideal 2005-05-19
  • 打赏
  • 举报
回复
来晚了。。顶了。。。
yesiloveyou 2005-05-19
  • 打赏
  • 举报
回复
只有顶的份了 /早上真该翘课/
flyingdancing2005 2005-05-19
  • 打赏
  • 举报
回复
不是10000,是100000
flyingdancing2005 2005-05-19
  • 打赏
  • 举报
回复
//No 1
#include<iostream>
using namespace std;
bool is_five(int number)
{
int sum=0;
while(number!=0)
{
sum+=number%10;
number/=10;
}
if(sum==5)
return true;
else
return false;

}


int main()
{
int count=0;
for(int i=100;i<10000;i++)
{
if(is_five(i))
{
count++;
cout<<count<<" :"<<i<<endl;
}
}
return 0;
}
xiao_xiao_zi 2005-05-19
  • 打赏
  • 举报
回复
第一题
#include <stdio.h>

int Is5(int num)
{
int temp = 0;
while (num)
{
temp+=num%10;
num/=10;
if (temp>5) return 0;
}
if (temp == 5)
{
return 1;
}
return 0;
}
int main()
{
int c = 0;
int i;
for (i = 100; i <= 10000; ++i)
{
if (Is5(i)) ++c;
}
printf("%d\n", c);
return 0;
}
fire314159 2005-05-19
  • 打赏
  • 举报
回复
对于题目一这种固定好数据的题。我觉得写个c(m,n)的算法更麻烦。呵呵。因题而异嘛。并不是通用的算法就是最好的。省事就行
fire314159 2005-05-19
  • 打赏
  • 举报
回复
#include<iostream.h>

void qsort(int *,int,int);
int partition(int *,int,int);


int main() {
int cnt;
int arr[10];

for(cnt=0;cnt<10;cnt++)
cin>>arr[cnt];

qsort(arr,0,9);
for(cnt=0;cnt<10;cnt++) {
cout<<arr[cnt]<<" ";
}
return 0;
}

void qsort(int *arr,int low,int high) {
int pro;
if(low<high) {
pro=partition(arr,low,high);
qsort(arr,low,pro-1);
qsort(arr,pro+1,high);
}
}

int partition(int *arr,int low,int high) {
int pro;
pro=arr[low];
while(low<high) {
while(low<high&&arr[high]<=pro)
high--;
arr[low]=arr[high];
while(low<high&&arr[low]>=pro)
low++;
arr[high]=arr[low];
}
arr[low]=pro;
return low;
}
--------
qsort排序。logn级的时间复杂度,普通情况下最快的排序了
piaozi2003 2005-05-19
  • 打赏
  • 举报
回复
题目一不用那么一个一个判断,就是一个排列组合问题

C(5+5-1,5)-C(2+5-1,5)=C(9,5)-C(6,5)=126-6=120

所以编写一个计算C(m,n)的函数,然后直接调用两次就可以了!
sutra 2005-05-19
  • 打赏
  • 举报
回复
更正:int lose[10]改为int lose[LENGTH],这个数组是存每位数字的
fire314159 2005-05-19
  • 打赏
  • 举报
回复
第二题要什么排序?qsort要不要?还是普通的插入排序?或者冒泡排序?实在太简单了。老兄,你朋友该不会忘得那么彻底吧?
sutra 2005-05-19
  • 打赏
  • 举报
回复
/*No.1*/
#define TOTAL 5
#define LENGTH 6
int lose[10] ;
int output()
{
int i ;
for( i = 0 ; i < LENGTH ; ++i )
printf( "%d", lose[i] ) ;
return printf( "\t" ) ;
}

int total( int sum, int idx ) {
int i, c = 0;
if( --idx == 0 ) return lose[idx] = TOTAL-sum, output() ;
for( i = sum ; i <= TOTAL; ++i )
lose[idx] = i-sum, c += total( i, idx ) ;
return c ;
}


/*No.2*/
void sort_insert( int *A, int N )
{
int j, P ;
int Tmp ;
for( P = 1; P < N; ++P ) {
Tmp = A[P] ;
for( j = P; j > 0 && A[j-1] < Tmp; --j )
A[j] = A[j-1] ;
A[j] = Tmp ;
}
}

#define COUNT 10
int main( )
{
int i, A[COUNT] ;
for( i = 0 ; i < COUNT ; ++i )
scanf( "%d", A+i ) ;
sort_insert( A, COUNT ) ;
for( i = 0 ; i < COUNT ; ++i )
printf( "%d\t", A[i] ) ;

return printf( "\ntotal:\t%d\n", total( 0, LENGTH ) ) ;
}

fire314159 2005-05-19
  • 打赏
  • 举报
回复
#include<iostream.h>

int main() {
int low=100,high=100000;
int cnt;
int temp;
int count=0;

while(low<=high) {
temp=0;
cnt=low;
if(cnt/100000!=0) {
temp+=cnt/100000;
cnt%=100000;
}
if(cnt/10000!=0) {
temp+=cnt/10000;
cnt%=10000;
}
if(cnt/1000!=0) {
temp+=cnt/1000;
cnt%=1000;
}
if(cnt/100!=0) {
temp+=cnt/100;
cnt%=100;
}
if(cnt/10!=0) {
temp+=cnt/10;
cnt%=10;
}
if(cnt!=0)
temp+=cnt;
if(temp==5)
count++;
low++;
}
cout<<"there are "<<count<<endl;
return 0;
}
lzwei3842 2005-05-19
  • 打赏
  • 举报
回复
顶一下了
magicknight 2005-05-19
  • 打赏
  • 举报
回复
//第二题
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAXLINES 10

int get_lines(char *lines[] );
void sort(char *p[], int n );
void print_strings(char *p[], int n );

char *lines[MAXLINES];

int main(void )
{
int number_of_lines;

number_of_lines = get_lines(lines );

if (number_of_lines < 0 )
{
puts("Memory allocation erro");
exit(-1);
}

sort(lines, number_of_lines );
print_strings(lines, number_of_lines );
return 0;
}

int get_lines(char *lines[] )
{
int n = 0;
char buffer[80];
puts ("Enter one line at time; enter a blank when done." );
while ((n < MAXLINES) && (gets(buffer) != 0 ) && (buffer[0] != '\0'))
{
if ((lines[n] = (char *)malloc(strlen(buffer) + 1 )) == NULL)
return -1;
strcpy(lines[n++], buffer );
}
return n;
}

void sort(char *p[], int n )
{
int a, b;
char *tmp;

for (a = 1; a < n; a++ )
{
for (b = 0; b < n-1; b++ )
{
if (strcmp(p[b], p[b+1] ) > 0 )
{
tmp = p[b];
p[b] = p[b+1];
p[b+1] = tmp;
}
}
}
}

void print_strings(char *p[], int n )
{
int count;

for(count = 0; count < n; count++ )
printf("%s\n", p[count]);
}
zhangyu21101213 2005-05-19
  • 打赏
  • 举报
回复
这是第一题的:
int qushu(long i)
{ int n;
if(i>=10)
n=i%10+qushu(i/10);
else n=i;
return n ;
}
main()
{ long i ;
int a,b=0;
for(i=100;i<100000;i++)
{ a=qushu(i);
if (a==5)
printf("the number(%d) is %ld .", ++b,i);
}
}
hblinlin 2005-05-19
  • 打赏
  • 举报
回复
mark一下
yuanyou 2005-05-19
  • 打赏
  • 举报
回复
mark
flyingdancing2005 2005-05-19
  • 打赏
  • 举报
回复
//第二题
#include<iostream.h>
template<class Datatype>
void QuickSort(Datatype a[],int low,int high)
{
int i=low,j=high;
Datatype temp=a[low];

while(i<j)
{
while(i<j && temp>=a[j])j--;
if(i<j)
{
a[i]=a[j];
i++;
}


while(i<j && a[i]>temp)i++;
if(i<j)
{
a[j]=a[i];
j--;
}

}
a[i]=temp;

if(low<i)QuickSort(a,low,i-1);
if(i<high)QuickSort(a,j+1,high);
}

int main()
{
int a[10];
for(int i=0;i<10;i++)
{
cout<<"please input the "<<i+1<<" number: ";
cin>>a[i];
}
cout<<"排序前:"<<endl;
for( i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;


QuickSort(a,0,9);
cout<<"排序后:"<<endl;
for( i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;


return 0;
}
1.数字排列 2.奖金分配问题 3.已知条件求解整数 4.输入日期判断第几天 5.输入整数进行排序 6.用*号显示字母C的图案 7.显示特殊图案 8.打印九九口诀 9.输国际象棋棋盘 10.打印楼梯并按条件打印笑脸 11.经典兔子问题 12.判断素数 13.水仙花数问题 14.正整数分解质因数 15.学习成绩划分 16.正整数求其最大公约数和最小公倍数 17.统计英文字母/空格/数字个数 18.求s=a+aa+aaa+aa...a的值 19.求解"完数" 20.球体自由落下物理问题 21.猴子吃桃问题 22.乒乓球比赛抽签问题 23.打印菱形图案 24.分数数列求和 25.求1+2!+3!+...+20!的和 26.利用递归方法求5! 27.将输入字符以相反顺序打印 28.岁数问题 29.求解正整数位数 30.判断回文数 31.星期几猜测游戏 32.改变文本颜色 33.学习gotoxy()与clrscr()函数 34.练习函数调用 35.设置文本颜色 36.求100之内的素数 37.对10个数进行排序 38.求3*3矩阵对角线元素之和 39.数字插入数组重新排序 40.将一个数组逆序输 41.static定义静态变量用法 42.使用auto定义变量用法 43.使用static的另一用法 44.使用external的用法 45.使用register定义变量方法 46.宏#define命令练习(1) 47.宏#define命令练习(2) 48.宏#define命令练习(3) 49.#if #ifdef和#ifndef的综合应用 50.#include 的应用练习 51.学习使用按位与 & 52.学习使用按位或 | 53.学习使用按位异或 ^ 54.取一个整数从右端开始的4~7位。 55.学习使用按位取反~ 56.用circle画圆形 57.学用line画直线 58.用rectangle画方形 59.画图综合例子 60.画图综合例子2 61.打印杨辉三角形 62.学习putpixel画点 63.画椭圆ellipse 64.利用ellipse and rectangle画图 65.画个最优美的图案 66.输入3个数字按大小顺序输 67.输入数组交换元素重新输 68.多个整数后移位置问题 69.圆圈报数问题 70.计算一个字符串长度 71.编写输入/输函数 72.创建链表 73.反向输链表 74.连接两个链表 75.算一道简单题目 76.调用函数求1/2+1/4+...+1/n 77.填空练习(指向指针的指针) 78.找到年龄最大的人 79.字符串排序 80.海滩猴子分桃 81.已知公式条件求数字 82.八进制转换为十进制 83.求0-7所能组成的奇数个数 84.由两个素数之和表示的偶数 85.判断一个素数能被几个9整除 86.两个字符串连接程序 87.结构体变量传递 88.读取数字的整数值并打印该值个数的* 89.数据加密 90.专升本一题 91.时间函数举例1 92.时间函数举例2 93.时间函数举例3 94.一个猜数游戏 95.家庭财务管理小程序 96.计算字符串中子串现的次数 97.输入字符并保存到磁盘 98.字符串转换成大写字母并输保存 99.文件操作应用1 100.文件操作应用2

69,370

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧