| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 545 人关注过本帖
标题:请教C语言算法
只看楼主 加入收藏
fengsjack
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2010-6-9
结帖率:100%
收藏
 问题点数:0 回复次数:8 
请教C语言算法
如果 第一天 给1个金币 第二天和第三天每天给2个金币  (第四,第五,第六天)给3个金币,以此类推,输入天数,求共给了多少金币



搜索更多相关主题的帖子: 算法 C语言 
2010-06-09 20:20
韩明海
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:253
专家分:749
注 册:2010-4-3
收藏
得分:0 
算法说不明白,还是写程序吧
int main()
{
    int i=0,j=1,n1=1,n2=0,sum=0;
    scanf("%d",&n2);
    while(n1<=n2)
    {
        while(i<j&&n1<=n2)
        {
            sum=sum+j;
            n1++;
            i++;
            printf("%d\n",sum);
        }
        i=0;
        j++;
        printf("test\n");
     }
printf("sum = %d\n",sum);
return 0;
2010-06-09 20:41
韩明海
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:253
专家分:749
注 册:2010-4-3
收藏
得分:0 
LZ太吝啬了,帮你写程序要花时间精力的
2010-06-09 20:42
fengsjack
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2010-6-9
收藏
得分:0 
。。新手有分吗,可以追加吗?
2010-06-09 20:56
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
收藏
得分:0 
#include <stdio.h>

int main(void)
{
    int a[5050];
    int days, sum;

    scanf("%d", &days);
    for (i = 1, k = 0; i < 100; i++)
    {
       for (j = 0; j < i; j++)
       {
           a[k++] = i;
       }
    }
    sum = 0;
    for (i = 0; i < days-1; i++)
       sum += a[days];
    printf("%d", sum);
    return 0;
}

[ 本帖最后由 BlueGuy 于 2010-6-9 21:46 编辑 ]

我就是真命天子,顺我者生,逆我者死!
2010-06-09 21:30
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
收藏
得分:0 
请别重复发帖,

我就是真命天子,顺我者生,逆我者死!
2010-06-09 21:32
fengsjack
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2010-6-9
收藏
得分:0 
程序有错吧,输入1输出是2
2010-06-09 21:43
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
收藏
得分:0 
回复 7楼 fengsjack
程序代码:
一:类的定义风格
代码1:不良的写法
class Employee
{ 
    public Employee(String n, double s, int year, int month, int day)
    { 
    name = n;
    salary = s;
    GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
    // GregorianCalendar uses 0 for January
    hireDay = calendar.getTime();
    }

    private String name;
    public String getName()
    { 
    return name;
    }
   
    private double salary;
    public double getSalary()
    { 
    return salary;
    }

    private Date hireDay;
    public Date getHireDay()
    { 
    return hireDay;
    }
    public void raiseSalary(double byPercent)
    { 
    double raise = salary * byPercent / 100;
    salary += raise;
    }

}

代码2:良好的写法
class Employee
{  

    private String name;
    private double salary;
    private Date hireDay;

    public Employee(String n, double s, int year, int month, int day)
    { 
    name = n;
    salary = s;
    GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
    // GregorianCalendar uses 0 for January
    hireDay = calendar.getTime();
    }

    public String getName()
    { 
    return name;
    }

    public double getSalary()
    { 
    return salary;
    }

    public Date getHireDay()
    { 
    return hireDay;
    }

    public void raiseSalary(double byPercent)
    { 
    double raise = salary * byPercent / 100;
    salary += raise;
    }
}

评:代码1 成员变量布局混乱,想到哪写到哪,很难整体把握类的功能。

二:实例域的类型
代码1:不良的写法
class Employee
{ 
    public String name;
    public double salary;
    public Date hireDay;
    public Employee(String n, double s, int year, int month, int day)
    { 
    name = n;
    salary = s;
    GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
    // GregorianCalendar uses 0 for January
    hireDay = calendar.getTime();
    }

    public String getName()
    { 
    return name;
    }

    public double getSalary()
    { 
    return salary;
    }

    public Date getHireDay()
    { 
    return hireDay;
    }

    public void raiseSalary(double byPercent)
    { 
    double raise = salary * byPercent / 100;
    salary += raise;
    }
}

代码2:良好的写法
class Employee
{ 
   private String name;
   private double salary;
   private Date hireDay;

   public Employee(String n, double s, int year, int month, int day)
   { 
      name = n;
      salary = s;
      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
         // GregorianCalendar uses 0 for January
      hireDay = calendar.getTime();
   }

   public String getName()
   { 
      return name;
   }

   public double getSalary()
   { 
      return salary;
   }

   public Date getHireDay()
   { 
      return hireDay;
   }

   public void raiseSalary(double byPercent)
   { 
      double raise = salary * byPercent / 100;
      salary += raise;
   }

}

评:代码1 用public标记实例域,是一种极不提倡的做法。public 数据域允许程序的任何方法
    对其进行读取和修改。这就完全破坏了封装。

三:   
代码1:不好的写法
class Employee
{ 
   private String name;
   private double salary;
   private Date hireDay;

   public Employee(String n, double s, int year, int month, int day)
   { 
      name = n;
      salary = s;
      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
         // GregorianCalendar uses 0 for January
      hireDay = calendar.getTime();
   }

   public String getName()
   { 
      return name;
   }

   public double getSalary()
   { 
      return salary;
   }

   public Date getHireDay()
   { 
      return hireDay;
   }

   public void raiseSalary(double byPercent)
   { 
      double raise = salary * byPercent / 100;
      salary += raise;
   }

代码2:良好的写法
class Employee
{ 
   private String name;
   private double salary;
   private Date hireDay;

   public Employee(String n, double s, int year, int month, int day)
   { 
      name = n;
      salary = s;
      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
         // GregorianCalendar uses 0 for January
      hireDay = calendar.getTime();
   }

   public String getName()
   { 
      return name;
   }

   public double getSalary()
   { 
      return salary;
   }

   public Date getHireDay()
   { 
      return hireDay;
   }

   public void raiseSalary(double byPercent)
   { 
      double raise = this.salary * byPercent / 100;
      this.salary += raise;
   }
   
评:代码2 用隐式参数 this 将实例域与局部变量明显的区分开来了,便于代码的阅读。
   
四:静态方法与静态常量
代码1:不良的写法
class Employee
{ 
  ......
  private static int nextId = 1;

 
  public static int getNextId()
  {
      return nextId;
  } 
}
......
Employee harry = new Employee();
int n = harry.getNextId();

代码2:良好的写法
class Employee
{ 
  ......
  private static int nextId = 1;

 
  public static int getNextId()
  {
      return nextId;
  } 
}
......
int n = Employee.getNextId();
评:静态类成员不属于任何独立的类,它属于类。代码2 用harry.getNextId() 代替
    Employee.getNextId(), 这种方式很容易造成混淆,getNextId()方法计算的结果
    与 harry 毫无关系

   
五:默认域的初始化
代码1:不良的写法
class Employee
{

 
   private int id;
   private String name;
   private double salary;
   // the default constructor
   public Employee()
   {
    
   }
   ......
}

代码2:良好的写法
class Employee
{
   private int id;
   private String name;
   private double salary;
   // the default constructor
   public Employee()
   {
       name = "";
       id = 0;
       salary = 0;
   }
   ......
}
评:代码1 在构造器中没有显示的给域赋予明确的初值, 会影响代码的可读性。

六:参数名
代码1:不良的写法

public Employee(String n, double s)
{
    name = n;
    salary = s;
}

代码2:良好的写法
public Employee(String aName, double aSalary)
{
    name = aSalary;
    salary = aName;
}

评:代码1 参数名过短,只能通过阅读代码才能了解参数n和参数s的含义。代码2 给
    每个参数前面加了一个前缀"a", 这样就代码就很清晰了。每一个读者一眼就能够
    看懂参数的含义。

七:阻止继承
代码1:不好的写法
class Employee
{ 
    ......
   private String name;
   private double salary;
   private Date hireDay;

   public String getName()
   { 
      return name;
   }
}  

代码2:良好的写法
class Employee
{ 
   ......
   private String name;
   private double salary;
   private Date hireDay;

   public final String getName()
   { 
      return name;
   }
} 

评:代码2 将类声明为  final, 可以确保它们不会在子类中改变语义。防止多态的滥用。

八:将公共操作和域放在超类
代码1:不良的写法
abstract class Person
{ 
   ......
}

class Employee extends Person
{ 
   private String name;
   ......
}

代码2:良好的写法
abstract class Person
{ 
   private String name;
}

class Employee extends Person
{ 
   ......
}

评:应该尽可能的将公共操作和域放在超类中,这样就可以最大限度的节省代码量。

九:滥用继承
代码1:不良的写法
class Contractor extends Employee
{
    private double hourlyWage;
}


代码2:良好的写法
class  Employee
{
    ......
}

class Contractor
{
    private double hourlyWage;
}

评:除非所有继承的方法都有意义,否则不要使用继承

十:强制类型转换
代码1:不良的写法
class Employee
{ 
   public Employee(String n, double s, int year, int month, int day)
   { 
      name = n;
      salary = s;
      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
      hireDay = calendar.getTime();
   }
   ......
}

class Manager extends Employee
{ 

 
   ......
   public void setBonus(double b)
   { 
      bonus = b;
   }

   private double bonus;
}

Employee p = (Manager)manager;
p.setBonus();
......

代码2:良好的写法
class Employee
{ 
   public void setBonus(double b)
   { 
      bonus = b;
   }
   ......
}

class Manager extends Employee
{ 

 
   ......
   private double bonus;
}

Employee p = new Employee();
p.setBonus();

评:如果超类对象需要调用子类对象的方法,那么就应该检查一下超类的设计。重新设计
    一下超类,添加相应的方法才是正确的选择


[ 本帖最后由 BlueGuy 于 2010-8-4 22:05 编辑 ]

我就是真命天子,顺我者生,逆我者死!
2010-06-09 21:45
anyixiao
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-6-9
收藏
得分:0 
int main()
{
    int i=0,j=1,n1=1,n2=0,sum=0;
    scanf("%d",&n2);
    while(n1<=n2)
    {
        while(i<j&&n1<=n2)
        {
            sum=sum+j;
            n1++;
            i++;
            printf("%d\n",sum);
        }
        i=0;
        j++;
        printf("test\n");
     }
printf("sum = %d\n",sum);
return 0;
2010-06-09 23:06
快速回复:请教C语言算法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045444 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved